2024-06-23 04:29:07 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:floor/floor.dart';
|
|
|
|
import 'package:solian/models/message.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart' as sqflite;
|
|
|
|
|
|
|
|
part 'history.g.dart';
|
|
|
|
|
|
|
|
@entity
|
|
|
|
class LocalMessage {
|
|
|
|
@primaryKey
|
|
|
|
final int id;
|
|
|
|
|
|
|
|
final Message data;
|
|
|
|
final int channelId;
|
|
|
|
|
|
|
|
LocalMessage(this.id, this.data, this.channelId);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemoteMessageConverter extends TypeConverter<Message, String> {
|
|
|
|
@override
|
|
|
|
Message decode(String databaseValue) {
|
|
|
|
return Message.fromJson(jsonDecode(databaseValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String encode(Message value) {
|
|
|
|
return jsonEncode(value.toJson());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@dao
|
|
|
|
abstract class LocalMessageDao {
|
2024-06-23 05:27:21 +00:00
|
|
|
@Query('SELECT COUNT(id) FROM LocalMessage WHERE channelId = :channelId')
|
|
|
|
Future<int?> countByChannel(int channelId);
|
|
|
|
|
|
|
|
@Query('SELECT * FROM LocalMessage WHERE channelId = :channelId ORDER BY id DESC')
|
2024-06-23 04:29:07 +00:00
|
|
|
Future<List<LocalMessage>> findAllByChannel(int channelId);
|
|
|
|
|
2024-06-23 05:27:21 +00:00
|
|
|
@Query('SELECT * FROM LocalMessage WHERE channelId = :channelId ORDER BY id DESC LIMIT 1')
|
|
|
|
Future<LocalMessage?> findLastByChannel(int channelId);
|
|
|
|
|
2024-06-23 04:29:07 +00:00
|
|
|
@Insert(onConflict: OnConflictStrategy.replace)
|
|
|
|
Future<void> insert(LocalMessage m);
|
|
|
|
|
|
|
|
@Insert(onConflict: OnConflictStrategy.replace)
|
|
|
|
Future<void> insertBulk(List<LocalMessage> m);
|
|
|
|
|
2024-06-23 05:27:21 +00:00
|
|
|
@Update(onConflict: OnConflictStrategy.replace)
|
|
|
|
Future<void> update(LocalMessage person);
|
|
|
|
|
|
|
|
@Query('DELETE FROM LocalMessage WHERE id = :id')
|
|
|
|
Future<void> delete(int id);
|
|
|
|
|
|
|
|
@Query('DELETE FROM LocalMessage WHERE channelId = :channelId')
|
|
|
|
Future<List<LocalMessage>> deleteByChannel(int channelId);
|
|
|
|
|
|
|
|
@Query('DELETE FROM LocalMessage')
|
2024-06-23 04:29:07 +00:00
|
|
|
Future<void> wipeLocalMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverters([RemoteMessageConverter])
|
|
|
|
@Database(version: 1, entities: [LocalMessage])
|
|
|
|
abstract class MessageHistoryDb extends FloorDatabase {
|
|
|
|
LocalMessageDao get localMessages;
|
|
|
|
}
|