Browse Source

最后一条消息顺序处理

wr_202303
wurong 4 months ago
parent
commit
cbb84aa75d
  1. 23
      lib/im/database/hx_database.dart
  2. 2
      lib/im/im_view/im_page.dart

23
lib/im/database/hx_database.dart

@ -65,11 +65,26 @@ class HxDatabase {
return (messages?.isNotEmpty ?? false) ? messages.first : null;
}
Future<List<Message>> queryList(userId) async{
Future<List<Message>> queryList() async{
await _dbIsOpen();
String sql =
'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) {
String sql = '''SELECT *
FROM `Message`
WHERE ROWID IN (
SELECT ROWID
FROM (
SELECT ROWID, `fromId`, `toId`, MAX(`time`) AS max_time
FROM `Message`
GROUP BY `fromId` || '_' || `toId`, `toId` || '_' || `fromId`
) AS grouped_messages
WHERE max_time = (
SELECT MAX(`time`)
FROM `Message`
WHERE (`fromId` = grouped_messages.`fromId` AND `toId` = grouped_messages.`toId`)
OR (`fromId` = grouped_messages.`toId` AND `toId` = grouped_messages.`fromId`)
)
)
ORDER BY `time` DESC;''';
return db.rawQuery(sql).then((value) {
return value.map((e) {
debugPrint("Message: ${e}");
return Message.fromJson(e);

2
lib/im/im_view/im_page.dart

@ -117,7 +117,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
loadMessageList() async {
SharedPreferences shared = await SharedPreferences.getInstance();
String userId = shared.getString("userId");
messages = await hxDatabase.queryList(userId);
messages = await hxDatabase.queryList();
lastMessageMap = messages.lGroupBy((p0) => p0.toId != userId ? p0.toId : p0.fromId).mGroupItem(key: (p1) => num.parse(p1.time));

Loading…
Cancel
Save