Files
workavia-mail-front/lib/features/mailbox/data/local/state_cache_manager.dart
T
2025-04-14 04:01:05 -07:00

34 lines
1.6 KiB
Dart

import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:tmail_ui_user/features/caching/clients/state_cache_client.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
import 'package:tmail_ui_user/features/mailbox/data/extensions/state_cache_extension.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_cache.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/state_type.dart';
class StateCacheManager {
final StateCacheClient _stateCacheClient;
StateCacheManager(this._stateCacheClient);
Future<State?> getState(AccountId accountId, UserName userName, StateType stateType) async {
final stateKey = TupleKey(stateType.name, accountId.asString, userName.value).encodeKey;
final stateCache = await _stateCacheClient.getItem(stateKey);
return stateCache?.toState();
}
Future<void> saveState(AccountId accountId, UserName userName, StateCache stateCache) async {
final stateKey = TupleKey(stateCache.type.name, accountId.asString, userName.value).encodeKey;
return await _stateCacheClient.insertItem(stateKey, stateCache);
}
Future<void> deleteState(AccountId accountId, UserName userName, StateType stateType) async {
final stateKey = TupleKey(stateType.name, accountId.asString, userName.value).encodeKey;
return await _stateCacheClient.deleteItem(stateKey);
}
Future<void> closeStateHiveCacheBox() => _stateCacheClient.closeBox();
}