From b8b3cf0db05db3604f6227fb45b7d4b08670f5a1 Mon Sep 17 00:00:00 2001 From: dab246 Date: Tue, 28 Sep 2021 15:03:02 +0700 Subject: [PATCH] TF-123 Setup configuration hive database for caching mailbox --- lib/features/caching/config/cache_client.dart | 29 +++++ lib/features/caching/config/cache_config.dart | 6 + .../caching/config/hive_cache_config.dart | 29 +++++ .../caching/mailbox_cache_client.dart | 121 ++++++++++++++++++ lib/features/caching/state_cache_client.dart | 121 ++++++++++++++++++ lib/main.dart | 2 + pubspec.yaml | 3 + 7 files changed, 311 insertions(+) create mode 100644 lib/features/caching/config/cache_client.dart create mode 100644 lib/features/caching/config/cache_config.dart create mode 100644 lib/features/caching/config/hive_cache_config.dart create mode 100644 lib/features/caching/mailbox_cache_client.dart create mode 100644 lib/features/caching/state_cache_client.dart diff --git a/lib/features/caching/config/cache_client.dart b/lib/features/caching/config/cache_client.dart new file mode 100644 index 000000000..0f5ebbe31 --- /dev/null +++ b/lib/features/caching/config/cache_client.dart @@ -0,0 +1,29 @@ + +import 'package:hive/hive.dart'; + +abstract class CacheClient { + + String get tableName; + + Future> openTable(); + + Future insertItem(String key, T newObject); + + Future insertMultipleItem(Map mapObject); + + Future getItem(String key); + + Future> getListItem(); + + Future updateItem(String key, T newObject); + + Future updateMultipleItem(Map mapObject); + + Future deleteItem(String key); + + Future deleteMultipleItem(List listKey); + + Future isExistItem(String key); + + Future isExistTable(); +} \ No newline at end of file diff --git a/lib/features/caching/config/cache_config.dart b/lib/features/caching/config/cache_config.dart new file mode 100644 index 000000000..e5c508c66 --- /dev/null +++ b/lib/features/caching/config/cache_config.dart @@ -0,0 +1,6 @@ + +abstract class CacheConfig { + Future initializeDatabase(); + + void registerAdapter(); +} \ No newline at end of file diff --git a/lib/features/caching/config/hive_cache_config.dart b/lib/features/caching/config/hive_cache_config.dart new file mode 100644 index 000000000..c07632be0 --- /dev/null +++ b/lib/features/caching/config/hive_cache_config.dart @@ -0,0 +1,29 @@ +import 'dart:io'; + +import 'package:hive/hive.dart'; +import 'package:model/caching/mailbox/mailbox_rights_cache.dart'; +import 'package:model/model.dart'; +import 'package:path_provider/path_provider.dart' as pathProvider; +import 'package:tmail_ui_user/features/caching/config/cache_config.dart'; + +class HiveCacheConfig extends CacheConfig { + + Future setUp() async { + await initializeDatabase(); + registerAdapter(); + } + + @override + Future initializeDatabase() async { + Directory directory = await pathProvider.getApplicationDocumentsDirectory(); + Hive.init(directory.path); + } + + @override + void registerAdapter() { + Hive.registerAdapter(MailboxCacheAdapter()); + Hive.registerAdapter(MailboxRightsCacheAdapter()); + Hive.registerAdapter(StateDaoAdapter()); + Hive.registerAdapter(StateTypeAdapter()); + } +} \ No newline at end of file diff --git a/lib/features/caching/mailbox_cache_client.dart b/lib/features/caching/mailbox_cache_client.dart new file mode 100644 index 000000000..8b66666a2 --- /dev/null +++ b/lib/features/caching/mailbox_cache_client.dart @@ -0,0 +1,121 @@ + +import 'package:hive/hive.dart'; +import 'package:model/model.dart'; +import 'package:tmail_ui_user/features/caching/config/cache_client.dart'; + +class MailboxCacheClient extends CacheClient { + + @override + String get tableName => 'MailboxCache'; + + @override + Future> openTable() { + return Future.sync(() async { + if (Hive.isBoxOpen(tableName)) { + return Hive.box(tableName); + } + return await Hive.openBox(tableName); + }).catchError((error) { + throw error; + }); + } + + @override + Future isExistItem(String key) { + return Future.sync(() async { + final boxMailbox = await openTable(); + return boxMailbox.containsKey(key); + }).catchError((error) { + throw error; + }); + } + + @override + Future deleteItem(String key) { + return Future.sync(() async { + final boxMailbox = await openTable(); + return boxMailbox.delete(key); + }).catchError((error) { + throw error; + }); + } + + @override + Future getItem(String key) { + return Future.sync(() async { + final boxMailbox = await openTable(); + return boxMailbox.get(key); + }).catchError((error) { + throw error; + }); + } + + @override + Future> getListItem() { + return Future.sync(() async { + final boxMailbox = await openTable(); + return boxMailbox.values.toList(); + }).catchError((error) { + throw error; + }); + } + + @override + Future insertItem(String key, MailboxCache newObject) { + return Future.sync(() async { + final boxMailbox = await openTable(); + boxMailbox.put(key, newObject); + }).catchError((error) { + throw error; + }); + } + + @override + Future insertMultipleItem(Map mapObject) { + return Future.sync(() async { + final boxMailbox = await openTable(); + boxMailbox.putAll(mapObject); + }).catchError((error) { + throw error; + }); + } + + @override + Future updateItem(String key, MailboxCache newObject) { + return Future.sync(() async { + final boxMailbox = await openTable(); + boxMailbox.put(key, newObject); + }).catchError((error) { + throw error; + }); + } + + @override + Future isExistTable() { + return Future.sync(() async { + return await Hive.boxExists(tableName); + }).catchError((error) { + throw error; + }); + } + + @override + Future deleteMultipleItem(List listKey) { + return Future.sync(() async { + final boxMailbox = await openTable(); + return boxMailbox.deleteAll(listKey); + }).catchError((error) { + throw error; + }); + } + + @override + Future updateMultipleItem(Map mapObject) { + return Future.sync(() async { + final boxMailbox = await openTable(); + boxMailbox.putAll(mapObject); + }).catchError((error) { + throw error; + }); + } +} \ No newline at end of file diff --git a/lib/features/caching/state_cache_client.dart b/lib/features/caching/state_cache_client.dart new file mode 100644 index 000000000..c08f6668c --- /dev/null +++ b/lib/features/caching/state_cache_client.dart @@ -0,0 +1,121 @@ + +import 'package:hive/hive.dart'; +import 'package:model/model.dart'; +import 'package:tmail_ui_user/features/caching/config/cache_client.dart'; + +class StateCacheClient extends CacheClient { + + @override + String get tableName => 'StateCache'; + + @override + Future> openTable() { + return Future.sync(() async { + if (Hive.isBoxOpen(tableName)) { + return Hive.box(tableName); + } + return await Hive.openBox(tableName); + }).catchError((error) { + throw error; + }); + } + + @override + Future isExistItem(String key) { + return Future.sync(() async { + final boxState = await openTable(); + return boxState.containsKey(key); + }).catchError((error) { + throw error; + }); + } + + @override + Future deleteItem(String key) { + return Future.sync(() async { + final boxState = await openTable(); + return boxState.delete(key); + }).catchError((error) { + throw error; + }); + } + + @override + Future getItem(String key) { + return Future.sync(() async { + final boxState = await openTable(); + return boxState.get(key); + }).catchError((error) { + throw error; + }); + } + + @override + Future> getListItem() { + return Future.sync(() async { + final boxState = await openTable(); + return boxState.values.toList(); + }).catchError((error) { + throw error; + }); + } + + @override + Future insertItem(String key, StateDao newObject) { + return Future.sync(() async { + final boxState = await openTable(); + boxState.put(key, newObject); + }).catchError((error) { + throw error; + }); + } + + @override + Future insertMultipleItem(Map mapObject) { + return Future.sync(() async { + final boxState = await openTable(); + boxState.putAll(mapObject); + }).catchError((error) { + throw error; + }); + } + + @override + Future updateItem(String key, StateDao newObject) { + return Future.sync(() async { + final boxState = await openTable(); + boxState.put(key, newObject); + }).catchError((error) { + throw error; + }); + } + + @override + Future isExistTable() { + return Future.sync(() async { + return await Hive.boxExists(tableName); + }).catchError((error) { + throw error; + }); + } + + @override + Future deleteMultipleItem(List listKey) { + return Future.sync(() async { + final boxState = await openTable(); + boxState.deleteAll(listKey); + }).catchError((error) { + throw error; + }); + } + + @override + Future updateMultipleItem(Map mapObject) { + return Future.sync(() async { + final boxState = await openTable(); + boxState.putAll(mapObject); + }).catchError((error) { + throw error; + }); + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 9413be8a9..72df8fdc2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:get/get.dart'; +import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart'; import 'package:tmail_ui_user/main/bindings/main_bindings.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations_delegate.dart'; import 'package:tmail_ui_user/main/pages/app_pages.dart'; @@ -11,6 +12,7 @@ import 'package:tmail_ui_user/main/routes/app_routes.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await MainBindings().dependencies(); + await HiveCacheConfig().setUp(); // Disable landscape on device mobile if (Get.size.shortestSide < ResponsiveUtils.minTabletWidth) { SystemChrome.setPreferredOrientations([ diff --git a/pubspec.yaml b/pubspec.yaml index 62f93dad5..455a5b5a2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -122,6 +122,9 @@ dependencies: # file_picker file_picker: 3.0.2+2 + # hive + hive: 2.0.4 + dev_dependencies: flutter_test: sdk: flutter