♻️ 使用 SQLITE 来存储本地消息记录 #1

Merged
LittleSheep merged 6 commits from features/local-message-history into master 2024-06-23 11:13:42 +00:00
3 changed files with 27 additions and 2 deletions
Showing only changes of commit 8036930084 - Show all commits

View File

@ -18,9 +18,11 @@ class ChatHistoryController {
} }
Future<void> getMessages(Channel channel, String scope) async { Future<void> getMessages(Channel channel, String scope) async {
isLoading.value = true;
totalHistoryCount.value = totalHistoryCount.value =
await database.syncMessages(channel, scope: scope); await database.syncMessages(channel, scope: scope);
await syncHistory(channel); await syncHistory(channel);
isLoading.value = false;
} }
Future<void> getMoreMessages(Channel channel, String scope) async { Future<void> getMoreMessages(Channel channel, String scope) async {
@ -42,7 +44,23 @@ class ChatHistoryController {
receiveMessage(Message remote) async { receiveMessage(Message remote) async {
final entry = await database.receiveMessage(remote); 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 { 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:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:get/get_connect/http/src/request/request.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/account.dart';
import 'package:solian/providers/chat.dart'; import 'package:solian/providers/chat.dart';
import 'package:solian/services.dart'; import 'package:solian/services.dart';
@ -135,6 +136,11 @@ class AuthProvider extends GetConnect {
Get.find<AccountProvider>().notifications.clear(); Get.find<AccountProvider>().notifications.clear();
Get.find<AccountProvider>().notificationUnread.value = 0; Get.find<AccountProvider>().notificationUnread.value = 0;
final chatHistory = ChatHistoryController();
chatHistory.initialize().then((_) async {
await chatHistory.database.localMessages.wipeLocalMessages();
});
storage.deleteAll(); storage.deleteAll();
} }

View File

@ -324,6 +324,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
slivers: [ slivers: [
Obx(() { Obx(() {
return SliverList.builder( return SliverList.builder(
key: Key('chat-history#${_channel!.id}'),
itemCount: _chatController.currentHistory.length, itemCount: _chatController.currentHistory.length,
itemBuilder: buildHistory, itemBuilder: buildHistory,
); );
@ -382,7 +383,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
channel: _channel!, channel: _channel!,
onSent: (Message item) { onSent: (Message item) {
setState(() { setState(() {
_chatController.receiveMessage(item); _chatController.addTemporaryMessage(item);
}); });
}, },
onReset: () { onReset: () {