TF-123 Setup configuration hive database for caching mailbox
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
abstract class CacheClient<T> {
|
||||
|
||||
String get tableName;
|
||||
|
||||
Future<Box<T>> openTable();
|
||||
|
||||
Future<void> insertItem(String key, T newObject);
|
||||
|
||||
Future<void> insertMultipleItem(Map<String, T> mapObject);
|
||||
|
||||
Future<T?> getItem(String key);
|
||||
|
||||
Future<List<T>> getListItem();
|
||||
|
||||
Future<void> updateItem(String key, T newObject);
|
||||
|
||||
Future<void> updateMultipleItem(Map<String, T> mapObject);
|
||||
|
||||
Future<void> deleteItem(String key);
|
||||
|
||||
Future<void> deleteMultipleItem(List<String> listKey);
|
||||
|
||||
Future<bool> isExistItem(String key);
|
||||
|
||||
Future<bool> isExistTable();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
abstract class CacheConfig {
|
||||
Future initializeDatabase();
|
||||
|
||||
void registerAdapter();
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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<MailboxCache> {
|
||||
|
||||
@override
|
||||
String get tableName => 'MailboxCache';
|
||||
|
||||
@override
|
||||
Future<Box<MailboxCache>> openTable() {
|
||||
return Future.sync(() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<MailboxCache>(tableName);
|
||||
}
|
||||
return await Hive.openBox<MailboxCache>(tableName);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isExistItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
return boxMailbox.containsKey(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
return boxMailbox.delete(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MailboxCache?> getItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
return boxMailbox.get(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<MailboxCache>> getListItem() {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
return boxMailbox.values.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> insertItem(String key, MailboxCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
boxMailbox.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> insertMultipleItem(Map<String, MailboxCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
boxMailbox.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateItem(String key, MailboxCache newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
boxMailbox.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isExistTable() {
|
||||
return Future.sync(() async {
|
||||
return await Hive.boxExists(tableName);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
return boxMailbox.deleteAll(listKey);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateMultipleItem(Map<String, MailboxCache> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxMailbox = await openTable();
|
||||
boxMailbox.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<StateDao> {
|
||||
|
||||
@override
|
||||
String get tableName => 'StateCache';
|
||||
|
||||
@override
|
||||
Future<Box<StateDao>> openTable() {
|
||||
return Future.sync(() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<StateDao>(tableName);
|
||||
}
|
||||
return await Hive.openBox<StateDao>(tableName);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isExistItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
return boxState.containsKey(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
return boxState.delete(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<StateDao?> getItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
return boxState.get(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<StateDao>> getListItem() {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
return boxState.values.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> insertItem(String key, StateDao newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
boxState.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> insertMultipleItem(Map<String, StateDao> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
boxState.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateItem(String key, StateDao newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
boxState.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isExistTable() {
|
||||
return Future.sync(() async {
|
||||
return await Hive.boxExists(tableName);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
boxState.deleteAll(listKey);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateMultipleItem(Map<String, StateDao> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxState = await openTable();
|
||||
boxState.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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([
|
||||
|
||||
@@ -122,6 +122,9 @@ dependencies:
|
||||
# file_picker
|
||||
file_picker: 3.0.2+2
|
||||
|
||||
# hive
|
||||
hive: 2.0.4
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
Reference in New Issue
Block a user