Files
workavia-mail-front/lib/features/caching/email_cache_client.dart
T

135 lines
3.4 KiB
Dart

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<EmailCache> {
@override
String get tableName => 'EmailCache';
@override
Future<Box<EmailCache>> openBox() {
return Future.sync(() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<EmailCache>(tableName);
}
return await Hive.openBox<EmailCache>(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<EmailCache?> getItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<List<EmailCache>> getAll() {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, EmailCache newObject) {
return Future.sync(() async {
final boxEmail = await openBox();
boxEmail.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, EmailCache> mapObject) {
return Future.sync(() async {
final boxEmail = await openBox();
boxEmail.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateItem(String key, EmailCache newObject) {
return Future.sync(() async {
final boxEmail = await openBox();
boxEmail.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 boxEmail = await openBox();
return boxEmail.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, EmailCache> mapObject) {
return Future.sync(() async {
final boxEmail = await openBox();
boxEmail.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
Future<List<EmailCache>> 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;
});
}
}