2023-07-17 01:00:16 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2023-07-27 18:17:52 +08:00
|
|
|
|
2023-07-17 01:00:16 +08:00
|
|
|
import 'package:together_mobile/database/box_type.dart';
|
2023-07-27 18:17:52 +08:00
|
|
|
import 'package:together_mobile/models/init_get_it.dart';
|
|
|
|
import 'package:together_mobile/models/user_model.dart';
|
|
|
|
import 'package:together_mobile/utils/app_dir.dart';
|
2023-07-17 01:00:16 +08:00
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
class HiveDatabase {
|
|
|
|
static bool _isInitialised = false;
|
2023-07-17 01:00:16 +08:00
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
static Future<void> init() async {
|
|
|
|
if (_isInitialised) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-23 17:16:31 +08:00
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
List<int> encryptionKeyUint8List = await _getEncryptKey();
|
2023-08-11 23:02:31 +08:00
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
await Hive.initFlutter(await getBoxDir());
|
2023-07-17 01:00:16 +08:00
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
Box<ChatSetting> chatSettingBox =
|
|
|
|
await Hive.openBox<ChatSetting>('chat_setting');
|
|
|
|
|
|
|
|
final openedChats =
|
|
|
|
chatSettingBox.values.where((element) => element.isOpen);
|
|
|
|
|
|
|
|
for (var chatBox in openedChats) {
|
2023-09-23 17:16:31 +08:00
|
|
|
await Hive.openBox<MessageT>(
|
2023-09-10 11:30:20 +08:00
|
|
|
'message_${chatBox.contactId}',
|
|
|
|
encryptionCipher: HiveAesCipher(encryptionKeyUint8List),
|
|
|
|
compactionStrategy: (entries, deletedEntries) => entries > 200,
|
|
|
|
);
|
2023-10-01 18:40:14 +08:00
|
|
|
await Hive.openBox<int>('msg_index_${chatBox.contactId}');
|
2023-09-10 11:30:20 +08:00
|
|
|
}
|
|
|
|
|
2023-10-01 18:40:14 +08:00
|
|
|
await Hive.openBox<AttachmentProgress>('attachment_send');
|
|
|
|
await Hive.openBox<AttachmentProgress>('attachment_receive');
|
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
_isInitialised = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void registerAdapter() {
|
|
|
|
Hive.registerAdapter(ChatSettingAdapter());
|
|
|
|
Hive.registerAdapter(MessageTAdapter());
|
2023-10-01 18:40:14 +08:00
|
|
|
Hive.registerAdapter(AttachmentProgressAdapter());
|
2023-09-10 11:30:20 +08:00
|
|
|
}
|
|
|
|
|
2023-09-23 17:16:31 +08:00
|
|
|
static Future<Box<MessageT>> openMessageBox(String contactId) async {
|
2023-09-10 11:30:20 +08:00
|
|
|
final encryptionKeyUint8List = await _getEncryptKey();
|
2023-09-23 17:16:31 +08:00
|
|
|
late Box<MessageT> messageTBox;
|
|
|
|
try {
|
|
|
|
messageTBox = Hive.box<MessageT>('message_$contactId');
|
|
|
|
} catch (e) {
|
|
|
|
messageTBox = await Hive.openBox<MessageT>(
|
|
|
|
'message_$contactId',
|
|
|
|
encryptionCipher: HiveAesCipher(encryptionKeyUint8List),
|
|
|
|
compactionStrategy: (entries, deletedEntries) => entries > 200,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return messageTBox;
|
2023-07-17 01:00:16 +08:00
|
|
|
}
|
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
static Future<List<int>> _getEncryptKey() async {
|
|
|
|
final id = getIt.get<UserAccount>().id;
|
|
|
|
const secureStorage = FlutterSecureStorage();
|
|
|
|
final encryptionKeyString = await secureStorage.read(key: 'encryptKey:$id');
|
|
|
|
if (encryptionKeyString == null) {
|
|
|
|
final key = Hive.generateSecureKey();
|
|
|
|
await secureStorage.write(
|
|
|
|
key: 'encryptKey:$id',
|
|
|
|
value: base64Encode(key),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
String? key = await secureStorage.read(key: 'encryptKey:$id');
|
|
|
|
final encryptionKeyUint8List = base64Url.decode(key!);
|
2023-07-17 01:00:16 +08:00
|
|
|
|
2023-09-10 11:30:20 +08:00
|
|
|
return encryptionKeyUint8List;
|
2023-07-17 01:00:16 +08:00
|
|
|
}
|
|
|
|
|
2023-09-16 11:33:57 +08:00
|
|
|
static Future<void> close() async {
|
2023-09-10 11:30:20 +08:00
|
|
|
_isInitialised = false;
|
2023-09-16 11:33:57 +08:00
|
|
|
await Hive.close();
|
2023-09-10 11:30:20 +08:00
|
|
|
}
|
2023-07-17 01:00:16 +08:00
|
|
|
}
|