import 'dart:typed_data'; import 'package:hive/hive.dart'; import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart'; abstract class HiveCacheClient { String get tableName; bool get encryption => false; Future _getEncryptionKey() => HiveCacheConfig.getEncryptionKey(); Future> openBox() async { if (Hive.isBoxOpen(tableName)) { return Hive.box(tableName); } else { return Hive.openBox(tableName); } } Future> openBoxEncryption() async { final encryptionKey = await _getEncryptionKey(); if (Hive.isBoxOpen(tableName)) { return Hive.box(tableName); } else { return Hive.openBox( tableName, encryptionCipher: encryptionKey != null ? HiveAesCipher(encryptionKey) : null); } } Future insertItem(String key, T newObject) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.put(key, newObject); }).catchError((error) { throw error; }); } Future insertMultipleItem(Map mapObject) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.putAll(mapObject); }).catchError((error) { throw error; }); } Future getItem(String key) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.get(key); }).catchError((error) { throw error; }); } Future> getAll() { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.values.toList(); }).catchError((error) { throw error; }); } Future updateItem(String key, T newObject) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.put(key, newObject); }).catchError((error) { throw error; }); } Future updateMultipleItem(Map mapObject) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.putAll(mapObject); }).catchError((error) { throw error; }); } Future deleteItem(String key) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.delete(key); }).catchError((error) { throw error; }); } Future deleteMultipleItem(List listKey) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.deleteAll(listKey); }).catchError((error) { throw error; }); } Future isExistItem(String key) { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.containsKey(key); }).catchError((error) { throw error; }); } Future isExistTable() { return Future.sync(() async { return Hive.boxExists(tableName); }).catchError((error) { throw error; }); } Future deleteBox() { return Hive.deleteBoxFromDisk(tableName); } Future clearAllData() { return Future.sync(() async { final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.clear(); }).catchError((error) { throw error; }); } }