sort and update contact list
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
erinn 2021-04-12 17:04:51 -07:00
parent eb5937d718
commit 9409cd28fa
3 changed files with 37 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:developer';
import '../errorHandler.dart';
import '../model.dart';
@ -34,6 +35,7 @@ class CwtchNotifier {
savePeerHistory: data["saveConversationHistory"],
numMessages: int.parse(data["numMessages"]),
numUnread: int.parse(data["unread"]),
lastMessageTime: DateTime.now(),//show at the top of the contact list even if no messages yet
));
break;
case "PeerStateChange":
@ -51,6 +53,7 @@ class CwtchNotifier {
case "NewMessageFromPeer":
profileCN.getProfile(data["ProfileOnion"]).contactList.getContact(data["RemotePeer"]).unreadMessages++;
profileCN.getProfile(data["ProfileOnion"]).contactList.getContact(data["RemotePeer"]).totalMessages++;
profileCN.getProfile(data["ProfileOnion"]).contactList.updateLastMessageTime(data["RemotePeer"], DateTime.now());
break;
case "AppError":
print("New App Error: $data");
@ -70,6 +73,9 @@ class CwtchNotifier {
var isOnline = data["Status"] == "Success";
profileCN.getProfile(data["ProfileOnion"]).isOnline = isOnline;
break;
case "ACNStatus":
print("acn status: $data");
break;
default:
print("unhandled event: $type");
}

View File

@ -74,7 +74,6 @@ class ProfileListState extends ChangeNotifier {
}
void add(ProfileInfoState newProfile) {
print("ProfileListState: adding " + newProfile.onion + " and notifying");
_profiles.add(newProfile);
notifyListeners();
}
@ -101,9 +100,13 @@ class ContactListState extends ChangeNotifier {
notifyListeners();
}
void updateUnreadMessages(String forOnion, int newVal) {
void updateLastMessageTime(String forOnion, DateTime newVal) {
getContact(forOnion).lastMessageTime = newVal;
_contacts.sort((ContactInfoState a, ContactInfoState b) {
return b.unreadMessages - a.unreadMessages;
if (a.lastMessageTime == null && b.lastMessageTime == null) return 0;
if (a.lastMessageTime == null) return 1;
if (b.lastMessageTime == null) return -1;
return a.lastMessageTime.compareTo(b.lastMessageTime);
});
//<todo> if(changed) {
notifyListeners();
@ -140,9 +143,9 @@ class ProfileInfoState extends ChangeNotifier {
this._online = online;
if (contactsJson != null && contactsJson != "" && contactsJson != "null") {
print("decoding " + contactsJson);
List<dynamic> contacts = jsonDecode(contactsJson);
this._contacts.addAll(contacts.map((contact) {
print(contact["lastMsgTime"]);
return ContactInfoState(
profileOnion: this.onion,
onion: contact["onion"],
@ -153,8 +156,14 @@ class ProfileInfoState extends ChangeNotifier {
isInvitation: contact["authorization"] == "unknown",
savePeerHistory: contact["saveConversationHistory"],
numMessages: contact["numMessages"],
numUnread: contact["numUnread"]);
numUnread: contact["numUnread"],
lastMessageTime: DateTime.tryParse(contact["lastMsgTime"]));
}));
// dummy set to invoke sort-on-load
if (this._contacts.num > 0) {
this._contacts.updateLastMessageTime(this._contacts._contacts.first.onion, this._contacts._contacts.first.lastMessageTime);
}
}
}
@ -204,6 +213,7 @@ class ContactInfoState extends ChangeNotifier {
String _savePeerHistory;
int _unreadMessages = 0;
int _totalMessages = 0;
DateTime _lastMessageTime;
ContactInfoState({
this.profileOnion,
@ -217,6 +227,7 @@ class ContactInfoState extends ChangeNotifier {
savePeerHistory = "DeleteHistoryConfirmed",
numMessages = 0,
numUnread = 0,
lastMessageTime = null,
}) {
this._nickname = nickname;
this._isGroup = isGroup;
@ -227,6 +238,7 @@ class ContactInfoState extends ChangeNotifier {
this._totalMessages = numMessages;
this._unreadMessages = numUnread;
this._savePeerHistory = savePeerHistory;
this._lastMessageTime = lastMessageTime;
}
get nickname => this._nickname;
@ -283,6 +295,12 @@ class ContactInfoState extends ChangeNotifier {
this._imagePath = newVal;
notifyListeners();
}
get lastMessageTime => this._lastMessageTime;
set lastMessageTime(DateTime newVal) {
this._lastMessageTime = newVal;
notifyListeners();
}
}
class MessageState extends ChangeNotifier {

View File

@ -18,6 +18,14 @@ class _MessageViewState extends State<MessageView> {
final ctrlrCompose = TextEditingController();
final focusNode = FocusNode();
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (Provider.of<ContactInfoState>(context, listen: false).unreadMessages > 0) {
Provider.of<ContactInfoState>(context, listen: false).unreadMessages = 0;
}
}
@override
void dispose() {
focusNode.dispose();