Browse Source

发消息修改

wr_202303
zsw 4 months ago
parent
commit
c88cee55ff
  1. 24
      lib/constant.dart
  2. 7
      lib/im/chat_details_page.dart
  3. 9
      lib/im/database/hx_database.dart
  4. 13
      lib/im/im_view/im_page.dart

24
lib/constant.dart

@ -17,17 +17,29 @@ Map<String, int> groupCount<S, T>(Map<S, List<T>> values) {
return map; return map;
} }
Map<String, T> groupItem<S, T>(Map<S, List<T>> values) { Map<String, T> groupItem<S, T>(Map<S, List<T>> values, {int Function(T) key}) {
var map = <String, T>{}; var map = <String, T>{};
for (var element in values.keys) { for (var element in values.keys) {
if (values[element] == null) { if (values[element] == null) {
continue; continue;
} }
map["$element"] = values[element].first; map["$element"] = key == null ? values[element].first : values[element].lMax(key);
} }
return map; return map;
} }
T max<T>(Iterable<T> 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<S, T> on Iterable<T> { extension ListExtension<S, T> on Iterable<T> {
@ -35,12 +47,18 @@ extension ListExtension<S, T> on Iterable<T> {
return groupBy(this, key); return groupBy(this, key);
} }
T lMax(int Function(T) key) {
return max(this, key);
}
} }
extension MapExtension<S, T> on Map<S, List<T>> { extension MapExtension<S, T> on Map<S, List<T>> {
Map<String, int> get mGroupCount => groupCount(this); Map<String, int> get mGroupCount => groupCount(this);
Map<String, T> get mGroupItem => groupItem(this); Map<String, T> mGroupItem({int Function(T) key}) {
return groupItem(this, key: key);
}
} }

7
lib/im/chat_details_page.dart

@ -77,6 +77,10 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
refreshState(); refreshState();
}); });
refreshState(); refreshState();
Future.delayed(const Duration(milliseconds: 1000), () {
jumpToBottom();
});
} }
refreshState() { refreshState() {
@ -94,9 +98,6 @@ class _ChatDetailsPage extends State<ChatDetailsPage>
loadMessageList(); loadMessageList();
Future.delayed(Duration.zero, () {
jumpToBottom();
});
} }
void jumpToBottom() { void jumpToBottom() {

9
lib/im/database/hx_database.dart

@ -60,7 +60,7 @@ class HxDatabase {
return Future.value(<Message>[]); return Future.value(<Message>[]);
} }
String sql = 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 db.rawQuery(sql, [userId, userId]).then((value) {
return value.map((e) { return value.map((e) {
debugPrint("Message: ${e}"); debugPrint("Message: ${e}");
@ -71,13 +71,14 @@ class HxDatabase {
}); });
} }
Future<List<Message>> queryUList(userId) { Future<List<Message>> queryUList(userId, {int page = 1, int pageSize = 10}) {
if (db == null) { if (db == null) {
return Future.value(<Message>[]); return Future.value(<Message>[]);
} }
int start = (page - 1) * pageSize;
String sql = String sql =
'SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC'; 'SELECT * FROM Message WHERE toId = ? OR fromId = ? ORDER BY time DESC LIMIT ?, ?';
return db.rawQuery(sql, [userId, userId]).then((value) { return db.rawQuery(sql, [userId, userId, start, pageSize]).then((value) {
return value.map((e) => Message.fromJson(e)).toList(); return value.map((e) => Message.fromJson(e)).toList();
}, onError: (error) { }, onError: (error) {
debugPrint("Messageerror: $error"); debugPrint("Messageerror: $error");

13
lib/im/im_view/im_page.dart

@ -115,9 +115,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
debugPrint("messages: queryList"); debugPrint("messages: queryList");
messages = await hxDatabase.queryList(userId); messages = await hxDatabase.queryList(userId);
messages.forEach((element) {
debugPrint("messages: ${element.toJson()}");
});
userIds = messages userIds = messages
.map((e) => e.toId != userId ? e.toId : e.fromId) .map((e) => e.toId != userId ? e.toId : e.fromId)
.toSet() .toSet()
@ -126,7 +124,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
List<ImUser> contacts = (await hxDatabase.queryImUser(userIds)) ?? []; List<ImUser> contacts = (await hxDatabase.queryImUser(userIds)) ?? [];
unreadCountMap = await hxDatabase.messageUnreadCount(userIds, userId); 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) { if (contacts?.isEmpty ?? true) {
await queryMemberInfo(userIds); await queryMemberInfo(userIds);
@ -138,7 +136,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
return; return;
} }
} }
contactMap = contacts.lGroupBy((p0) => p0.mid).mGroupItem; contactMap = contacts.lGroupBy((p0) => p0.mid).mGroupItem();
if (mounted) { if (mounted) {
setState(() {}); setState(() {});
@ -147,6 +145,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
void updateLastMessage(String userId) async { void updateLastMessage(String userId) async {
Message message = await hxDatabase.lastMessage(userId); Message message = await hxDatabase.lastMessage(userId);
debugPrint("lastmessage: $userId ${message.content} ${message.toJson()}");
if (message != null) { if (message != null) {
lastMessageMap[userId] = message; lastMessageMap[userId] = message;
refreshState(); refreshState();
@ -246,7 +245,7 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
baseData.data.forEach((element) async { baseData.data.forEach((element) async {
await hxDatabase.insertOrUpdateImUser(element.toJson()); await hxDatabase.insertOrUpdateImUser(element.toJson());
}); });
contactMap = baseData.data.lGroupBy((p0) => p0.mid).mGroupItem; contactMap = baseData.data.lGroupBy((p0) => p0.mid).mGroupItem();
setState(() {}); setState(() {});
} }
} else { } else {
@ -423,8 +422,8 @@ class _IMPage extends State<IMPage> implements OnChatMessage {
"toUser": contactMap[userIds[position]], "toUser": contactMap[userIds[position]],
}, },
).then((value) { ).then((value) {
unreadCountMap[userIds[position]] = 0;
updateLastMessage(userIds[position]); updateLastMessage(userIds[position]);
_refresh();
}); });
}, },
child: chatItem(userIds[position]), child: chatItem(userIds[position]),

Loading…
Cancel
Save