♻️ Use drift instead for floor

This commit is contained in:
2024-09-14 00:30:33 +08:00
parent db808650e3
commit b14e55355f
24 changed files with 886 additions and 692 deletions

View File

@ -2,13 +2,14 @@ import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/event.dart';
import 'package:solian/platform.dart';
import 'package:solian/providers/message/adaptor.dart';
import 'package:solian/providers/message/events.dart';
import 'package:solian/providers/database/database.dart';
import 'package:solian/providers/database/services/messages.dart';
class ChatEventController {
late final MessageHistoryDb database;
late final MessagesFetchingProvider src;
final RxList<LocalEvent> currentEvents = RxList.empty(growable: true);
final RxList<LocalMessageEventTableData> currentEvents =
RxList.empty(growable: true);
final RxInt totalEvents = 0.obs;
final RxBool isLoading = false.obs;
@ -17,27 +18,13 @@ class ChatEventController {
String? scope;
Future<void> initialize() async {
if (!PlatformInfo.isWeb) {
database = await createHistoryDb();
}
src = Get.find();
currentEvents.clear();
}
Future<LocalEvent?> getEvent(int id) async {
Future<LocalMessageEventTableData?> getEvent(int id) async {
if (channel == null || scope == null) return null;
if (PlatformInfo.isWeb) {
final remoteRecord = await getRemoteEvent(id, channel!, scope!);
if (remoteRecord == null) return null;
return LocalEvent(
remoteRecord.id,
remoteRecord,
remoteRecord.channelId,
remoteRecord.createdAt,
);
} else {
return await database.getEvent(id, channel!, scope: scope!);
}
return await src.getEvent(id, channel!, scope: scope!);
}
Future<void> getEvents(Channel channel, String scope) async {
@ -48,7 +35,7 @@ class ChatEventController {
isLoading.value = true;
if (PlatformInfo.isWeb) {
final result = await getRemoteEvents(
final result = await src.fetchRemoteEvents(
channel,
scope,
remainDepth: 3,
@ -57,13 +44,18 @@ class ChatEventController {
totalEvents.value = result?.$2 ?? 0;
if (result != null) {
for (final x in result.$1.reversed) {
final entry = LocalEvent(x.id, x, x.channelId, x.createdAt);
final entry = LocalMessageEventTableData(
id: x.id,
channelId: x.channelId,
createdAt: x.createdAt,
data: x,
);
insertEvent(entry);
applyEvent(entry);
}
}
} else {
final result = await database.syncRemoteEvents(
final result = await src.pullRemoteEvents(
channel,
scope: scope,
);
@ -76,7 +68,7 @@ class ChatEventController {
Future<void> loadEvents(Channel channel, String scope) async {
isLoading.value = true;
if (PlatformInfo.isWeb) {
final result = await getRemoteEvents(
final result = await src.fetchRemoteEvents(
channel,
scope,
remainDepth: 3,
@ -85,13 +77,18 @@ class ChatEventController {
if (result != null) {
totalEvents.value = result.$2;
for (final x in result.$1.reversed) {
final entry = LocalEvent(x.id, x, x.channelId, x.createdAt);
final entry = LocalMessageEventTableData(
id: x.id,
channelId: x.channelId,
createdAt: x.createdAt,
data: x,
);
currentEvents.add(entry);
applyEvent(entry);
}
}
} else {
final result = await database.syncRemoteEvents(
final result = await src.pullRemoteEvents(
channel,
depth: 3,
scope: scope,
@ -105,7 +102,7 @@ class ChatEventController {
Future<bool> syncLocal(Channel channel) async {
if (PlatformInfo.isWeb) return false;
final data = await database.localEvents.findAllByChannel(channel.id);
final data = await src.listEvents(channel);
currentEvents.replaceRange(0, currentEvents.length, data);
for (final x in data.reversed) {
applyEvent(x);
@ -114,26 +111,28 @@ class ChatEventController {
}
receiveEvent(Event remote) async {
LocalEvent entry;
LocalMessageEventTableData entry;
if (PlatformInfo.isWeb) {
entry = LocalEvent(
remote.id,
remote,
remote.channelId,
remote.createdAt,
entry = LocalMessageEventTableData(
id: remote.id,
channelId: remote.channelId,
createdAt: remote.createdAt,
data: remote,
);
} else {
entry = await database.receiveEvent(remote);
entry = await src.receiveEvent(remote);
}
insertEvent(entry);
applyEvent(entry);
}
insertEvent(LocalEvent entry) {
void insertEvent(LocalMessageEventTableData entry) {
if (entry.channelId != channel?.id) return;
final idx = currentEvents.indexWhere((x) => x.data.uuid == entry.data.uuid);
final idx = currentEvents.indexWhere(
(x) => x.data!.uuid == entry.data!.uuid,
);
if (idx != -1) {
currentEvents[idx] = entry;
} else {
@ -141,36 +140,36 @@ class ChatEventController {
}
}
applyEvent(LocalEvent entry) {
void applyEvent(LocalMessageEventTableData entry) {
if (entry.channelId != channel?.id) return;
switch (entry.data.type) {
switch (entry.data!.type) {
case 'messages.edit':
final body = EventMessageBody.fromJson(entry.data.body);
final body = EventMessageBody.fromJson(entry.data!.body);
if (body.relatedEvent != null) {
final idx =
currentEvents.indexWhere((x) => x.data.id == body.relatedEvent);
currentEvents.indexWhere((x) => x.data!.id == body.relatedEvent);
if (idx != -1) {
currentEvents[idx].data.body = entry.data.body;
currentEvents[idx].data.updatedAt = entry.data.updatedAt;
currentEvents[idx].data!.body = entry.data!.body;
currentEvents[idx].data!.updatedAt = entry.data!.updatedAt;
}
}
case 'messages.delete':
final body = EventMessageBody.fromJson(entry.data.body);
final body = EventMessageBody.fromJson(entry.data!.body);
if (body.relatedEvent != null) {
currentEvents.removeWhere((x) => x.id == body.relatedEvent);
}
}
}
addPendingEvent(Event info) async {
Future<void> addPendingEvent(Event info) async {
currentEvents.insert(
0,
LocalEvent(
info.id,
info,
info.channelId,
DateTime.now(),
LocalMessageEventTableData(
id: info.id,
channelId: info.channelId,
createdAt: DateTime.now(),
data: info,
),
);
}