diff --git a/lib/constant.dart b/lib/constant.dart index 169bf5db..3fb704e3 100644 --- a/lib/constant.dart +++ b/lib/constant.dart @@ -17,17 +17,29 @@ Map groupCount(Map> values) { return map; } -Map groupItem(Map> values) { +Map groupItem(Map> values, {int Function(T) key}) { var map = {}; for (var element in values.keys) { if (values[element] == null) { continue; } - map["$element"] = values[element].first; + map["$element"] = key == null ? values[element].first : values[element].lMax(key); } return map; } +T max(Iterable list, int Function(T) key) { + T tt; + for (T t in list) { + if (tt == null) { + tt = t; + } + if (key(tt) < key(t)) { + tt = t; + } + } + return tt; +} extension ListExtension on Iterable { @@ -35,12 +47,18 @@ extension ListExtension on Iterable { return groupBy(this, key); } + T lMax(int Function(T) key) { + return max(this, key); + } + } extension MapExtension on Map> { Map get mGroupCount => groupCount(this); - Map get mGroupItem => groupItem(this); + Map mGroupItem({int Function(T) key}) { + return groupItem(this, key: key); + } } diff --git a/lib/im/chat_details_page.dart b/lib/im/chat_details_page.dart index 05b8c9b5..a127db06 100644 --- a/lib/im/chat_details_page.dart +++ b/lib/im/chat_details_page.dart @@ -77,6 +77,10 @@ class _ChatDetailsPage extends State refreshState(); }); refreshState(); + + Future.delayed(const Duration(milliseconds: 1000), () { + jumpToBottom(); + }); } refreshState() { @@ -94,9 +98,6 @@ class _ChatDetailsPage extends State loadMessageList(); - Future.delayed(Duration.zero, () { - jumpToBottom(); - }); } void jumpToBottom() { diff --git a/lib/im/database/hx_database.dart b/lib/im/database/hx_database.dart index 7d4cbfc1..e0338bb7 100644 --- a/lib/im/database/hx_database.dart +++ b/lib/im/database/hx_database.dart @@ -60,7 +60,7 @@ class HxDatabase { return Future.value([]); } String sql = - 'SELECT * FROM Message WHERE toId = ? OR fromId = ? GROUP BY toId,fromId ORDER BY time DESC'; + 'SELECT * FROM (SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC) mm GROUP BY mm.toId,mm.fromId'; return db.rawQuery(sql, [userId, userId]).then((value) { return value.map((e) { debugPrint("Message: ${e}"); @@ -71,13 +71,14 @@ class HxDatabase { }); } - Future> queryUList(userId) { + Future> queryUList(userId, {int page = 1, int pageSize = 10}) { if (db == null) { return Future.value([]); } + int start = (page - 1) * pageSize; String sql = - 'SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC'; - return db.rawQuery(sql, [userId, userId]).then((value) { + 'SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC LIMIT ?, ?'; + return db.rawQuery(sql, [userId, userId, start, pageSize]).then((value) { return value.map((e) => Message.fromJson(e)).toList(); }, onError: (error) { debugPrint("Messageerror: $error"); diff --git a/lib/im/im_view/im_page.dart b/lib/im/im_view/im_page.dart index 455c2432..69d630aa 100644 --- a/lib/im/im_view/im_page.dart +++ b/lib/im/im_view/im_page.dart @@ -115,9 +115,7 @@ class _IMPage extends State implements OnChatMessage { debugPrint("messages: queryList"); messages = await hxDatabase.queryList(userId); - messages.forEach((element) { - debugPrint("messages: ${element.toJson()}"); - }); + userIds = messages .map((e) => e.toId != userId ? e.toId : e.fromId) .toSet() @@ -126,7 +124,7 @@ class _IMPage extends State implements OnChatMessage { List contacts = (await hxDatabase.queryImUser(userIds)) ?? []; unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); - lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem; + lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem(key: (p1) => num.parse(p1.time)); if (contacts?.isEmpty ?? true) { await queryMemberInfo(userIds); @@ -138,7 +136,7 @@ class _IMPage extends State implements OnChatMessage { return; } } - contactMap = contacts.lGroupBy((p0) => p0.mid).mGroupItem; + contactMap = contacts.lGroupBy((p0) => p0.mid).mGroupItem(); if (mounted) { setState(() {}); @@ -147,6 +145,7 @@ class _IMPage extends State implements OnChatMessage { void updateLastMessage(String userId) async { Message message = await hxDatabase.lastMessage(userId); + debugPrint("lastmessage: $userId ${message.content} ${message.toJson()}"); if (message != null) { lastMessageMap[userId] = message; refreshState(); @@ -246,7 +245,7 @@ class _IMPage extends State implements OnChatMessage { baseData.data.forEach((element) async { await hxDatabase.insertOrUpdateImUser(element.toJson()); }); - contactMap = baseData.data.lGroupBy((p0) => p0.mid).mGroupItem; + contactMap = baseData.data.lGroupBy((p0) => p0.mid).mGroupItem(); setState(() {}); } } else { @@ -423,8 +422,8 @@ class _IMPage extends State implements OnChatMessage { "toUser": contactMap[userIds[position]], }, ).then((value) { + unreadCountMap[userIds[position]] = 0; updateLastMessage(userIds[position]); - _refresh(); }); }, child: chatItem(userIds[position]),