Make friend status auto-update when receiving message

This commit is contained in:
Nutcake 2023-05-06 19:45:54 +02:00
parent 61fa2b1f16
commit c949bbc1c2
5 changed files with 34 additions and 3 deletions

View file

@ -196,6 +196,25 @@ class MessagingClient extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
void _updateFriend(Friend friend) {
_friendsCache[friend.id] = friend;
final sIndex = _sortedFriendsCache.indexWhere((element) => element.id == friend.id);
if (sIndex == -1) {
_sortedFriendsCache.add(friend);
} else {
_sortedFriendsCache[sIndex] = friend;
}
_sortFriendsCache();
}
Future<void> updateFriendStatus(String userId) async {
final friend = getAsFriend(userId);
if (friend == null) return;
final newStatus = await UserApi.getUserStatus(_apiClient, userId: userId);
_updateFriend(friend.copyWith(userStatus: newStatus));
notifyListeners();
}
MessageCache? getUserMessageCache(String userId) => _messageCache[userId]; MessageCache? getUserMessageCache(String userId) => _messageCache[userId];
static Future<void> backgroundCheckUnreads(Map<String, dynamic>? inputData) async { static Future<void> backgroundCheckUnreads(Map<String, dynamic>? inputData) async {
@ -325,6 +344,7 @@ class MessagingClient extends ChangeNotifier {
cache.addMessage(message); cache.addMessage(message);
if (message.senderId != selectedFriend?.id) { if (message.senderId != selectedFriend?.id) {
addUnread(message); addUnread(message);
updateFriendStatus(message.senderId);
} }
notifyListeners(); notifyListeners();
break; break;

View file

@ -28,6 +28,20 @@ class Friend extends Comparable {
); );
} }
Friend copyWith({
String? id, String? username, String? ownerId, UserStatus? userStatus, UserProfile? userProfile,
FriendStatus? friendStatus, DateTime? latestMessageTime}) {
return Friend(
id: id ?? this.id,
username: username ?? this.username,
ownerId: ownerId ?? this.ownerId,
userStatus: userStatus ?? this.userStatus,
userProfile: userProfile ?? this.userProfile,
friendStatus: friendStatus ?? this.friendStatus,
latestMessageTime: latestMessageTime ?? this.latestMessageTime,
);
}
Map toMap({bool shallow=false}) { Map toMap({bool shallow=false}) {
return { return {
"id": id, "id": id,

View file

@ -1,5 +1,4 @@
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.dart';
import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart'; import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/models/friend.dart'; import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/message.dart'; import 'package:contacts_plus_plus/models/message.dart';

View file

@ -5,7 +5,6 @@ import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.dart';
import 'package:contacts_plus_plus/models/message.dart'; import 'package:contacts_plus_plus/models/message.dart';
import 'package:contacts_plus_plus/widgets/messages/message_state_indicator.dart'; import 'package:contacts_plus_plus/widgets/messages/message_state_indicator.dart';
import 'package:contacts_plus_plus/widgets/messages/messages_list.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';

View file

@ -7,7 +7,6 @@ import 'package:contacts_plus_plus/models/session.dart';
import 'package:contacts_plus_plus/widgets/generic_avatar.dart'; import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart'; import 'package:contacts_plus_plus/widgets/messages/messages_session_header.dart';
import 'package:contacts_plus_plus/widgets/messages/message_state_indicator.dart'; import 'package:contacts_plus_plus/widgets/messages/message_state_indicator.dart';
import 'package:contacts_plus_plus/widgets/messages/messages_list.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';