2024-06-23 10:51:49 +00:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/models/channel.dart';
|
|
|
|
import 'package:solian/models/message.dart';
|
|
|
|
import 'package:solian/providers/message/helper.dart';
|
|
|
|
import 'package:solian/providers/message/history.dart';
|
|
|
|
|
|
|
|
class ChatHistoryController {
|
|
|
|
late final MessageHistoryDb database;
|
|
|
|
|
|
|
|
final RxList<LocalMessage> currentHistory = RxList.empty(growable: true);
|
|
|
|
final RxInt totalHistoryCount = 0.obs;
|
|
|
|
|
2024-06-23 11:02:41 +00:00
|
|
|
final RxBool isLoading = false.obs;
|
|
|
|
|
2024-06-23 10:51:49 +00:00
|
|
|
initialize() async {
|
|
|
|
database = await createHistoryDb();
|
|
|
|
currentHistory.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getMessages(Channel channel, String scope) async {
|
2024-06-24 14:25:17 +00:00
|
|
|
syncHistory(channel);
|
|
|
|
|
2024-06-23 11:13:07 +00:00
|
|
|
isLoading.value = true;
|
2024-06-24 14:25:17 +00:00
|
|
|
totalHistoryCount.value = await database.syncMessages(
|
|
|
|
channel,
|
|
|
|
scope: scope,
|
|
|
|
);
|
2024-06-23 11:02:41 +00:00
|
|
|
await syncHistory(channel);
|
2024-06-23 11:13:07 +00:00
|
|
|
isLoading.value = false;
|
2024-06-23 11:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getMoreMessages(Channel channel, String scope) async {
|
|
|
|
isLoading.value = true;
|
|
|
|
totalHistoryCount.value = await database.syncMessages(
|
|
|
|
channel,
|
|
|
|
breath: 3,
|
|
|
|
scope: scope,
|
|
|
|
offset: currentHistory.length,
|
|
|
|
);
|
2024-06-23 10:51:49 +00:00
|
|
|
await syncHistory(channel);
|
2024-06-23 11:02:41 +00:00
|
|
|
isLoading.value = false;
|
2024-06-23 10:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> syncHistory(Channel channel) async {
|
|
|
|
currentHistory.replaceRange(0, currentHistory.length,
|
|
|
|
await database.localMessages.findAllByChannel(channel.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
receiveMessage(Message remote) async {
|
|
|
|
final entry = await database.receiveMessage(remote);
|
2024-06-23 11:13:07 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
);
|
2024-06-23 10:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void replaceMessage(Message remote) async {
|
|
|
|
final entry = await database.replaceMessage(remote);
|
|
|
|
currentHistory.replaceRange(
|
|
|
|
0,
|
|
|
|
currentHistory.length,
|
|
|
|
currentHistory.map((x) => x.id == entry.id ? entry : x),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void burnMessage(int id) async {
|
|
|
|
await database.burnMessage(id);
|
|
|
|
currentHistory.removeWhere((x) => x.id == id);
|
|
|
|
}
|
|
|
|
}
|