import 'package:hive/hive.dart'; import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart'; import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart'; class MailboxCacheClient extends HiveCacheClient { @override String get tableName => 'MailboxCache'; @override Future> openBox() { 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 openBox(); return boxMailbox.containsKey(key); }).catchError((error) { throw error; }); } @override Future deleteItem(String key) { return Future.sync(() async { final boxMailbox = await openBox(); return boxMailbox.delete(key); }).catchError((error) { throw error; }); } @override Future getItem(String key) { return Future.sync(() async { final boxMailbox = await openBox(); return boxMailbox.get(key); }).catchError((error) { throw error; }); } @override Future> getAll() { return Future.sync(() async { final boxMailbox = await openBox(); return boxMailbox.values.toList(); }).catchError((error) { throw error; }); } @override Future insertItem(String key, MailboxCache newObject) { return Future.sync(() async { final boxMailbox = await openBox(); boxMailbox.put(key, newObject); }).catchError((error) { throw error; }); } @override Future insertMultipleItem(Map mapObject) { return Future.sync(() async { final boxMailbox = await openBox(); boxMailbox.putAll(mapObject); }).catchError((error) { throw error; }); } @override Future updateItem(String key, MailboxCache newObject) { return Future.sync(() async { final boxMailbox = await openBox(); 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 openBox(); return boxMailbox.deleteAll(listKey); }).catchError((error) { throw error; }); } @override Future updateMultipleItem(Map mapObject) { return Future.sync(() async { final boxMailbox = await openBox(); boxMailbox.putAll(mapObject); }).catchError((error) { throw error; }); } @override Future clearAllData() { return Future.sync(() async { final boxMailbox = await openBox(); boxMailbox.clear(); }).catchError((error) { throw error; }); } }