Files
workavia-mail-front/lib/features/mailbox/data/local/mailbox_cache_manager.dart
T
dab246 5b01715dcd TF-1810 Move all hive clients to a separate folder
(cherry picked from commit c72e16b576835b7748e20ab3e5cce5e1f505a65f)
2023-06-05 11:02:20 +07:00

62 lines
2.6 KiB
Dart

import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
import 'package:tmail_ui_user/features/caching/clients/mailbox_cache_client.dart';
import 'package:tmail_ui_user/features/mailbox/data/extensions/list_mailbox_cache_extension.dart';
import 'package:tmail_ui_user/features/mailbox/data/extensions/list_mailbox_extension.dart';
import 'package:tmail_ui_user/features/mailbox/data/extensions/list_mailbox_id_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/exceptions/spam_report_exception.dart';
class MailboxCacheManager {
final MailboxCacheClient _mailboxCacheClient;
MailboxCacheManager(this._mailboxCacheClient);
Future<List<Mailbox>> getAllMailbox(AccountId accountId, UserName userName) async {
final mailboxCacheList = await _mailboxCacheClient.getListByTupleKey(accountId.asString, userName.value);
return mailboxCacheList.toMailboxList();
}
Future<void> update(
AccountId accountId,
UserName userName, {
List<Mailbox>? updated,
List<Mailbox>? created,
List<MailboxId>? destroyed
}) async {
final mailboxCacheExist = await _mailboxCacheClient.isExistTable();
if (mailboxCacheExist) {
final updatedCacheMailboxes = updated?.toMapCache(accountId, userName) ?? {};
final createdCacheMailboxes = created?.toMapCache(accountId, userName) ?? {};
final destroyedCacheMailboxes = destroyed?.toCacheKeyList(accountId, userName) ?? [];
await Future.wait([
_mailboxCacheClient.updateMultipleItem(updatedCacheMailboxes),
_mailboxCacheClient.insertMultipleItem(createdCacheMailboxes),
_mailboxCacheClient.deleteMultipleItem(destroyedCacheMailboxes)
]);
} else {
final createdCacheMailboxes = created?.toMapCache(accountId, userName) ?? {};
await _mailboxCacheClient.insertMultipleItem(createdCacheMailboxes);
}
return Future.value();
}
Future<Mailbox> getSpamMailbox(AccountId accountId, UserName userName) async {
final mailboxCachedList = await _mailboxCacheClient.getListByTupleKey(accountId.asString, userName.value);
final listSpamMailboxCached = mailboxCachedList
.toMailboxList()
.where((mailbox) => mailbox.role == PresentationMailbox.roleSpam)
.toList();
if (listSpamMailboxCached.isNotEmpty) {
return listSpamMailboxCached.first;
} else {
throw NotFoundSpamMailboxCachedException();
}
}
}