Skip to content

Commit 248d019

Browse files
committed
🚸 Chat list sorted by last message
#58
1 parent fb99696 commit 248d019

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/state/chats/chatState.dart

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ class ChatState extends AppState {
2727
String _channelName;
2828
Query messageQuery;
2929

30+
/// Contains list of chat messages on main chat screen
31+
/// List is sortBy mesage timeStamp
32+
/// Last message will be display on the bottom of screen
3033
List<ChatMessage> get messageList {
3134
if (_messageList == null) {
3235
return null;
3336
} else {
34-
_messageList.sort((x, y) => DateTime.parse(x.createdAt)
37+
_messageList.sort((x, y) => DateTime.parse(y.createdAt)
3538
.toLocal()
36-
.compareTo(DateTime.parse(y.createdAt).toLocal()));
37-
_messageList.reversed;
38-
_messageList = _messageList.reversed.toList();
39-
return List.from(_messageList);
39+
.compareTo(DateTime.parse(x.createdAt).toLocal()));
40+
return _messageList;
4041
}
4142
}
4243

44+
/// Contain list of users who have chat history with logged in user
4345
List<ChatMessage> get chatUserList {
4446
if (_chatUserList == null) {
4547
return null;
@@ -86,9 +88,9 @@ class ChatState extends AppState {
8688
var data = remoteConfig.getString('FcmServerKey');
8789
if (data != null && data.isNotEmpty) {
8890
serverToken = jsonDecode(data)["key"];
89-
}
90-
else{
91-
cprint("Please configure Remote config in firebase", errorIn: "getFCMServerKey");
91+
} else {
92+
cprint("Please configure Remote config in firebase",
93+
errorIn: "getFCMServerKey");
9294
}
9395
}
9496

@@ -110,6 +112,19 @@ class ChatState extends AppState {
110112
_chatUserList.add(model);
111113
});
112114
}
115+
_chatUserList.sort((x, y) {
116+
if (x.createdAt != null && y.createdAt != null) {
117+
return DateTime.parse(y.createdAt)
118+
.compareTo(DateTime.parse(x.createdAt));
119+
} else {
120+
if(x.createdAt != null){
121+
return 0;
122+
}
123+
else{
124+
return 1;
125+
}
126+
}
127+
});
113128
} else {
114129
_chatUserList = null;
115130
}
@@ -255,8 +270,9 @@ class ChatState extends AppState {
255270
_messageList = null;
256271
notifyListeners();
257272
}
273+
258274
/// [Warning] do not call super.dispose() from this method
259-
/// If this method called here it will dipose chat state data
275+
/// If this method called here it will dipose chat state data
260276
// super.dispose();
261277
}
262278

0 commit comments

Comments
 (0)