From 1a5114a217fad59f810f7f2ac67999891a6e2d40 Mon Sep 17 00:00:00 2001 From: Nutcake Date: Mon, 29 May 2023 16:48:53 +0200 Subject: [PATCH] Fix friend online status in message view not updating --- lib/clients/messaging_client.dart | 3 +++ lib/models/friend.dart | 18 +++++++++++++++++- lib/widgets/friends/friend_list_tile.dart | 2 +- lib/widgets/messages/messages_list.dart | 22 +++++++++++----------- pubspec.yaml | 2 +- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/clients/messaging_client.dart b/lib/clients/messaging_client.dart index 0108ac5..680eedb 100644 --- a/lib/clients/messaging_client.dart +++ b/lib/clients/messaging_client.dart @@ -249,6 +249,9 @@ class MessagingClient extends ChangeNotifier { } else { _sortedFriendsCache[sIndex] = friend; } + if (friend.id == selectedFriend?.id) { + selectedFriend = friend; + } _sortFriendsCache(); } diff --git a/lib/models/friend.dart b/lib/models/friend.dart index 15ec426..97efed3 100644 --- a/lib/models/friend.dart +++ b/lib/models/friend.dart @@ -1,8 +1,10 @@ +import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/models/session.dart'; import 'package:contacts_plus_plus/models/user_profile.dart'; import 'package:flutter/material.dart'; class Friend implements Comparable { + static const _emptyId = "-1"; static const _neosBotId = "U-Neos"; final String id; final String username; @@ -12,7 +14,7 @@ class Friend implements Comparable { final FriendStatus friendStatus; final DateTime latestMessageTime; - Friend({required this.id, required this.username, required this.ownerId, required this.userStatus, required this.userProfile, + const Friend({required this.id, required this.username, required this.ownerId, required this.userStatus, required this.userProfile, required this.friendStatus, required this.latestMessageTime, }); @@ -38,6 +40,20 @@ class Friend implements Comparable { return Friend.fromMap(map); } + factory Friend.empty() { + return Friend( + id: _emptyId, + username: "", + ownerId: "", + userStatus: UserStatus.empty(), + userProfile: UserProfile.empty(), + friendStatus: FriendStatus.none, + latestMessageTime: DateTimeX.epoch + ); + } + + bool get isEmpty => id == _emptyId; + Friend copyWith({ String? id, String? username, String? ownerId, UserStatus? userStatus, UserProfile? userProfile, FriendStatus? friendStatus, DateTime? latestMessageTime}) { diff --git a/lib/widgets/friends/friend_list_tile.dart b/lib/widgets/friends/friend_list_tile.dart index 130751b..140ce31 100644 --- a/lib/widgets/friends/friend_list_tile.dart +++ b/lib/widgets/friends/friend_list_tile.dart @@ -83,7 +83,7 @@ class FriendListTile extends StatelessWidget { MaterialPageRoute( builder: (context) => ChangeNotifierProvider.value( value: mClient, - child: MessagesList(friend: friend), + child: MessagesList(), ), ), ); diff --git a/lib/widgets/messages/messages_list.dart b/lib/widgets/messages/messages_list.dart index de04404..a98a5e6 100644 --- a/lib/widgets/messages/messages_list.dart +++ b/lib/widgets/messages/messages_list.dart @@ -11,9 +11,7 @@ import 'package:provider/provider.dart'; import 'message_bubble.dart'; class MessagesList extends StatefulWidget { - const MessagesList({required this.friend, super.key}); - - final Friend friend; + const MessagesList({super.key}); @override State createState() => _MessagesListState(); @@ -54,21 +52,23 @@ class _MessagesListState extends State with SingleTickerProviderSt @override Widget build(BuildContext context) { - final sessions = widget.friend.userStatus.activeSessions; final appBarColor = Theme.of(context).colorScheme.surfaceVariant; return Consumer(builder: (context, mClient, _) { - final cache = mClient.getUserMessageCache(widget.friend.id); + final friend = mClient.selectedFriend ?? Friend.empty(); + final cache = mClient.getUserMessageCache(friend.id); + final sessions = friend.userStatus.activeSessions; + return Scaffold( appBar: AppBar( title: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ - FriendOnlineStatusIndicator(userStatus: widget.friend.userStatus), + FriendOnlineStatusIndicator(userStatus: friend.userStatus), const SizedBox( width: 8, ), - Text(widget.friend.username), - if (widget.friend.isHeadless) + Text(friend.username), + if (friend.isHeadless) Padding( padding: const EdgeInsets.only(left: 12), child: Icon( @@ -179,9 +179,9 @@ class _MessagesListState extends State with SingleTickerProviderSt message: cache.error.toString(), onRetry: () { setState(() { - mClient.deleteUserMessageCache(widget.friend.id); + mClient.deleteUserMessageCache(friend.id); }); - mClient.loadUserMessageCache(widget.friend.id); + mClient.loadUserMessageCache(friend.id); }, ); } @@ -231,7 +231,7 @@ class _MessagesListState extends State with SingleTickerProviderSt ), ), MessageInputBar( - recipient: widget.friend, + recipient: friend, disabled: cache == null || cache.error != null, onMessageSent: () { setState(() {}); diff --git a/pubspec.yaml b/pubspec.yaml index 12cc480..ba9a30b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.3.0+1 +version: 1.3.1+1 environment: sdk: '>=3.0.0'