import 'package:hive/hive.dart'; import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart'; import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart'; class EmailCacheClient extends HiveCacheClient { @override String get tableName => 'EmailCache'; @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 boxEmail = await openBox(); return boxEmail.containsKey(key); }).catchError((error) { throw error; }); } @override Future deleteItem(String key) { return Future.sync(() async { final boxEmail = await openBox(); return boxEmail.delete(key); }).catchError((error) { throw error; }); } @override Future getItem(String key) { return Future.sync(() async { final boxEmail = await openBox(); return boxEmail.get(key); }).catchError((error) { throw error; }); } @override Future> getAll() { return Future.sync(() async { final boxEmail = await openBox(); return boxEmail.values.toList(); }).catchError((error) { throw error; }); } @override Future insertItem(String key, EmailCache newObject) { return Future.sync(() async { final boxEmail = await openBox(); boxEmail.put(key, newObject); }).catchError((error) { throw error; }); } @override Future insertMultipleItem(Map mapObject) { return Future.sync(() async { final boxEmail = await openBox(); boxEmail.putAll(mapObject); }).catchError((error) { throw error; }); } @override Future updateItem(String key, EmailCache newObject) { return Future.sync(() async { final boxEmail = await openBox(); boxEmail.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 boxEmail = await openBox(); return boxEmail.deleteAll(listKey); }).catchError((error) { throw error; }); } @override Future updateMultipleItem(Map mapObject) { return Future.sync(() async { final boxEmail = await openBox(); boxEmail.putAll(mapObject); }).catchError((error) { throw error; }); } Future> getListEmailCacheByMailboxId(MailboxId mailboxId) { return Future.sync(() async { final boxEmail = await openBox(); return boxEmail.values.where((emailCache) { return emailCache.mailboxIds != null && emailCache.mailboxIds!.containsKey(mailboxId.id.value) && emailCache.mailboxIds![mailboxId.id.value] == true; }).toList(); }).catchError((error) { throw error; }); } }