🐛 Bug fixes on duplicate message

This commit is contained in:
LittleSheep 2024-06-23 19:13:07 +08:00
parent d0cd75d653
commit 8036930084
3 changed files with 27 additions and 2 deletions

View File

@ -18,9 +18,11 @@ class ChatHistoryController {
}
Future<void> getMessages(Channel channel, String scope) async {
isLoading.value = true;
totalHistoryCount.value =
await database.syncMessages(channel, scope: scope);
await syncHistory(channel);
isLoading.value = false;
}
Future<void> 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 {

View File

@ -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<AccountProvider>().notifications.clear();
Get.find<AccountProvider>().notificationUnread.value = 0;
final chatHistory = ChatHistoryController();
chatHistory.initialize().then((_) async {
await chatHistory.database.localMessages.wipeLocalMessages();
});
storage.deleteAll();
}

View File

@ -324,6 +324,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
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<ChannelChatScreen> {
channel: _channel!,
onSent: (Message item) {
setState(() {
_chatController.receiveMessage(item);
_chatController.addTemporaryMessage(item);
});
},
onReset: () {