From 803693008454dadfd065e0c878d8131811926ee5 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 23 Jun 2024 19:13:07 +0800 Subject: [PATCH] :bug: Bug fixes on duplicate message --- lib/controllers/chat_history_controller.dart | 20 +++++++++++++++++++- lib/providers/auth.dart | 6 ++++++ lib/screens/channel/channel_chat.dart | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/controllers/chat_history_controller.dart b/lib/controllers/chat_history_controller.dart index 2a97bd6..0d93047 100644 --- a/lib/controllers/chat_history_controller.dart +++ b/lib/controllers/chat_history_controller.dart @@ -18,9 +18,11 @@ class ChatHistoryController { } Future getMessages(Channel channel, String scope) async { + isLoading.value = true; totalHistoryCount.value = await database.syncMessages(channel, scope: scope); await syncHistory(channel); + isLoading.value = false; } Future getMoreMessages(Channel channel, String scope) async { @@ -42,7 +44,23 @@ class ChatHistoryController { receiveMessage(Message remote) async { final entry = await database.receiveMessage(remote); - currentHistory.add(entry); + final idx = currentHistory.indexWhere((x) => x.data.uuid == remote.uuid); + if (idx != -1) { + currentHistory[idx] = entry; + } else { + currentHistory.insert(0, entry); + } + } + + addTemporaryMessage(Message info) async { + currentHistory.insert( + 0, + LocalMessage( + info.id, + info, + info.channelId, + ), + ); } void replaceMessage(Message remote) async { diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index d58ba99..e7989fc 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:get/get.dart'; import 'package:get/get_connect/http/src/request/request.dart'; +import 'package:solian/controllers/chat_history_controller.dart'; import 'package:solian/providers/account.dart'; import 'package:solian/providers/chat.dart'; import 'package:solian/services.dart'; @@ -135,6 +136,11 @@ class AuthProvider extends GetConnect { Get.find().notifications.clear(); Get.find().notificationUnread.value = 0; + final chatHistory = ChatHistoryController(); + chatHistory.initialize().then((_) async { + await chatHistory.database.localMessages.wipeLocalMessages(); + }); + storage.deleteAll(); } diff --git a/lib/screens/channel/channel_chat.dart b/lib/screens/channel/channel_chat.dart index 8ad1d4c..3e795ea 100644 --- a/lib/screens/channel/channel_chat.dart +++ b/lib/screens/channel/channel_chat.dart @@ -324,6 +324,7 @@ class _ChannelChatScreenState extends State { slivers: [ Obx(() { return SliverList.builder( + key: Key('chat-history#${_channel!.id}'), itemCount: _chatController.currentHistory.length, itemBuilder: buildHistory, ); @@ -382,7 +383,7 @@ class _ChannelChatScreenState extends State { channel: _channel!, onSent: (Message item) { setState(() { - _chatController.receiveMessage(item); + _chatController.addTemporaryMessage(item); }); }, onReset: () {