From 5ffe534883225ab2108967b92b83c9569bc1a87a Mon Sep 17 00:00:00 2001 From: zsw Date: Thu, 19 Sep 2024 21:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E6=B6=88=E6=81=AF=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/im/SocketClient.dart | 2 ++ lib/im/chat_details_page.dart | 15 +++++++++++++-- lib/im/im_view/im_page.dart | 25 +++++++++++++++++-------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/im/SocketClient.dart b/lib/im/SocketClient.dart index 80c012d8..683043da 100644 --- a/lib/im/SocketClient.dart +++ b/lib/im/SocketClient.dart @@ -44,6 +44,7 @@ class SocketClient { authRequest(shared.getString("token")); + heartbeat(); }).catchError((error) { debugPrint("socket-connect-error: $error"); reconnectTime = 1500; @@ -54,6 +55,7 @@ class SocketClient { heartbeat() { Timer.periodic(const Duration(milliseconds: 3000), (timer) { + debugPrint("heartbeat: ${DateTime.now().millisecondsSinceEpoch}"); Uint8List data = utf8.encode(jsonEncode({"heartbeat": DateTime.now().millisecondsSinceEpoch})); MsgData msgData = MsgData(to: "0", from: userId, type: MsgType.TEXT, data: data); final proto2 = Proto(5, 1, msgData.writeToBuffer()); diff --git a/lib/im/chat_details_page.dart b/lib/im/chat_details_page.dart index 4f254ed2..98eb5086 100644 --- a/lib/im/chat_details_page.dart +++ b/lib/im/chat_details_page.dart @@ -78,8 +78,17 @@ class _ChatDetailsPage extends State messages = await hxDatabase.queryUList(_toUser.mid, pageSize: 100); socketClient.addCallback(_toUser.mid, (Message message) { - messages.add(message); + messages.insert(0, message); refreshState(); + + if (scrollController.position != null) { + double offset = scrollController.position.maxScrollExtent; + debugPrint("offset: $offset"); + Future.delayed(const Duration(milliseconds: 500), () { + scrollController.animateTo(offset + 108, duration: const Duration(milliseconds: 500), curve: Curves.easeIn); + }); + } + }); refreshState(); @@ -820,7 +829,9 @@ class _ChatDetailsPage extends State if (scrollController.position != null) { double offset = scrollController.position.maxScrollExtent; debugPrint("offset: $offset"); - scrollController.animateTo(offset + 50, duration: const Duration(milliseconds: 500), curve: Curves.easeIn); + Future.delayed(const Duration(milliseconds: 500), () { + scrollController.animateTo(offset + 108, duration: const Duration(milliseconds: 500), curve: Curves.easeIn); + }); } }); diff --git a/lib/im/im_view/im_page.dart b/lib/im/im_view/im_page.dart index fbf7c6a9..fe435583 100644 --- a/lib/im/im_view/im_page.dart +++ b/lib/im/im_view/im_page.dart @@ -93,14 +93,7 @@ class _IMPage extends State implements OnChatMessage { lastMessageMap[message.fromId] = message; - sortConversation(lastMessageMap); - - debugPrint("messages_records : ${message.toJson()}"); - if (contactMap[message.fromId] == null) { - queryMemberInfo([message.fromId]); - return; - } - refreshState(); + listenerRefresh(message); }); loadMessageList(); @@ -112,6 +105,22 @@ class _IMPage extends State implements OnChatMessage { // queryMsgStats(); } + listenerRefresh(Message message) async { + SharedPreferences shared = await SharedPreferences.getInstance(); + String userId = shared.getString("userId"); + + await sortConversation(lastMessageMap); + + await queryUnreadCount(userIds, userId); + + debugPrint("messages_records : ${message.toJson()}"); + if (contactMap[message.fromId] == null) { + queryMemberInfo([message.fromId]); + return; + } + refreshState(); + } + loadMessageList() async { SharedPreferences shared = await SharedPreferences.getInstance(); String userId = shared.getString("userId");