From b88f8ec5dc45517b36484dc8596062e1f519c5d5 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 23 Jul 2021 18:39:18 +0200 Subject: [PATCH] refactor: Rename store and allow storing custom values --- lib/main.dart | 4 +-- ...dart => flutter_matrix_hive_database.dart} | 34 ++++++++++++++----- test/utils/test_client.dart | 4 +-- 3 files changed, 30 insertions(+), 12 deletions(-) rename lib/utils/matrix_sdk_extensions.dart/{flutter_famedly_sdk_hive_database.dart => flutter_matrix_hive_database.dart} (84%) diff --git a/lib/main.dart b/lib/main.dart index 9faf985a..ca989b1b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -18,7 +18,7 @@ import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:universal_html/html.dart' as html; import 'package:vrouter/vrouter.dart'; -import 'utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart'; +import 'utils/matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart'; import 'widgets/layouts/wait_for_login.dart'; import 'widgets/lock_screen.dart'; import 'widgets/matrix.dart'; @@ -46,7 +46,7 @@ void main() async { importantStateEvents: { 'im.ponies.room_emotes', // we want emotes to work properly }, - databaseBuilder: FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder, + databaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, supportedLoginTypes: { AuthenticationTypes.password, if (PlatformInfos.isMobile || PlatformInfos.isWeb) AuthenticationTypes.sso diff --git a/lib/utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart b/lib/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart similarity index 84% rename from lib/utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart rename to lib/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart index 24fec306..0580ba5a 100644 --- a/lib/utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart +++ b/lib/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart @@ -12,16 +12,39 @@ import 'package:path_provider/path_provider.dart'; import '../platform_infos.dart'; -class FlutterFamedlySdkHiveDatabase extends FamedlySdkHiveDatabase { - FlutterFamedlySdkHiveDatabase(String name, {HiveCipher encryptionCipher}) +class FlutterMatrixHiveStore extends FamedlySdkHiveDatabase { + FlutterMatrixHiveStore(String name, {HiveCipher encryptionCipher}) : super( name, encryptionCipher: encryptionCipher, ); + Box _customBox; + String get _customBoxName => '$name.box.custom'; + static bool _hiveInitialized = false; static const String _hiveCipherStorageKey = 'hive_encryption_key'; + @override + Future open() async { + await super.open(); + _customBox = await Hive.openBox( + _customBoxName, + encryptionCipher: encryptionCipher, + ); + return; + } + + @override + Future clear(int clientId) async { + await super.clear(clientId); + await _customBox.deleteAll(_customBox.keys); + await _customBox.close(); + } + + dynamic get(dynamic key) => _customBox.get(key); + Future put(dynamic key, dynamic value) => _customBox.put(key, value); + static Future hiveDatabaseBuilder( Client client) async { if (!kIsWeb && !_hiveInitialized) { @@ -59,7 +82,7 @@ class FlutterFamedlySdkHiveDatabase extends FamedlySdkHiveDatabase { } on MissingPluginException catch (_) { Logs().i('Hive encryption is not supported on this platform'); } - final db = FlutterFamedlySdkHiveDatabase( + final db = FlutterMatrixHiveStore( client.clientName, encryptionCipher: hiverCipher, ); @@ -107,9 +130,4 @@ class FlutterFamedlySdkHiveDatabase extends FamedlySdkHiveDatabase { await file.writeAsBytes(bytes); return; } - - @override - Future clear(int clientId) async { - await super.clear(clientId); - } } diff --git a/test/utils/test_client.dart b/test/utils/test_client.dart index d3c91c60..09d82b84 100644 --- a/test/utils/test_client.dart +++ b/test/utils/test_client.dart @@ -1,4 +1,4 @@ -import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart'; +import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart'; import 'package:matrix/encryption/utils/key_verification.dart'; import 'package:matrix/matrix.dart'; import 'package:matrix_api_lite/fake_matrix_api.dart'; @@ -19,7 +19,7 @@ Future prepareTestClient({ importantStateEvents: { 'im.ponies.room_emotes', // we want emotes to work properly }, - databaseBuilder: FlutterFamedlySdkHiveDatabase.hiveDatabaseBuilder, + databaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, supportedLoginTypes: { AuthenticationTypes.password, AuthenticationTypes.sso