74 lines
3.0 KiB
Dart
74 lines
3.0 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:core/utils/app_logger.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:hive/hive.dart';
|
|
import 'package:path_provider/path_provider.dart' as path_provider;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
|
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
|
|
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
|
|
import 'package:tmail_ui_user/features/login/data/utils/login_constant.dart';
|
|
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart';
|
|
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_rights_cache.dart';
|
|
import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart';
|
|
import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
|
|
import 'package:tmail_ui_user/features/mailbox_dashboard/data/model/recent_search_cache.dart';
|
|
import 'package:tmail_ui_user/features/thread/data/model/email_address_hive_cache.dart';
|
|
import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart';
|
|
|
|
class HiveCacheConfig {
|
|
|
|
Future setUp({String? cachePath}) async {
|
|
await initializeDatabase(databasePath: cachePath);
|
|
registerAdapter();
|
|
}
|
|
|
|
Future initializeDatabase({String? databasePath}) async {
|
|
if (databasePath != null) {
|
|
Hive.init(databasePath);
|
|
} else {
|
|
if (!GetPlatform.isWeb) {
|
|
Directory directory = await path_provider.getApplicationDocumentsDirectory();
|
|
Hive.init(directory.path);
|
|
}
|
|
}
|
|
}
|
|
|
|
static Future<Uint8List?> getEncryptionKey() async {
|
|
final sharedPreference = Get.find<SharedPreferences>();
|
|
final containsEncryptionKey = sharedPreference.containsKey(LoginConstant.keyHiveEncrypt);
|
|
if (!containsEncryptionKey) {
|
|
final key = Hive.generateSecureKey();
|
|
await sharedPreference.setString(LoginConstant.keyHiveEncrypt, base64UrlEncode(key));
|
|
}
|
|
final keyStored = sharedPreference.getString(LoginConstant.keyHiveEncrypt) ?? '';
|
|
final encryptionKey = base64Url.decode(keyStored);
|
|
log('HiveCacheConfig::getEncryptionKey(): Encryption key: $encryptionKey');
|
|
return encryptionKey;
|
|
}
|
|
|
|
static Future<bool> removeEncryptionKey() {
|
|
final sharedPreference = Get.find<SharedPreferences>();
|
|
return sharedPreference.remove(LoginConstant.keyHiveEncrypt);
|
|
}
|
|
|
|
void registerAdapter() {
|
|
Hive.registerAdapter(MailboxCacheAdapter());
|
|
Hive.registerAdapter(MailboxRightsCacheAdapter());
|
|
Hive.registerAdapter(StateCacheAdapter());
|
|
Hive.registerAdapter(StateTypeAdapter());
|
|
Hive.registerAdapter(EmailAddressHiveCacheAdapter());
|
|
Hive.registerAdapter(EmailCacheAdapter());
|
|
Hive.registerAdapter(RecentSearchCacheAdapter());
|
|
Hive.registerAdapter(TokenOidcCacheAdapter());
|
|
Hive.registerAdapter(AccountCacheAdapter());
|
|
Hive.registerAdapter(AuthenticationInfoCacheAdapter());
|
|
}
|
|
|
|
Future closeHive() async {
|
|
await Hive.close();
|
|
}
|
|
} |