Fix friend online status in message view not updating

This commit is contained in:
Nutcake 2023-05-29 16:48:53 +02:00
parent 22cae71602
commit 1a5114a217
5 changed files with 33 additions and 14 deletions

View file

@ -249,6 +249,9 @@ class MessagingClient extends ChangeNotifier {
} else {
_sortedFriendsCache[sIndex] = friend;
}
if (friend.id == selectedFriend?.id) {
selectedFriend = friend;
}
_sortFriendsCache();
}

View file

@ -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}) {

View file

@ -83,7 +83,7 @@ class FriendListTile extends StatelessWidget {
MaterialPageRoute(
builder: (context) => ChangeNotifierProvider<MessagingClient>.value(
value: mClient,
child: MessagesList(friend: friend),
child: MessagesList(),
),
),
);

View file

@ -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<StatefulWidget> createState() => _MessagesListState();
@ -54,21 +52,23 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
@override
Widget build(BuildContext context) {
final sessions = widget.friend.userStatus.activeSessions;
final appBarColor = Theme.of(context).colorScheme.surfaceVariant;
return Consumer<MessagingClient>(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<MessagesList> 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<MessagesList> with SingleTickerProviderSt
),
),
MessageInputBar(
recipient: widget.friend,
recipient: friend,
disabled: cache == null || cache.error != null,
onMessageSent: () {
setState(() {});

View file

@ -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'