TF-2384 Create StateCacheManager
Signed-off-by: dab246 <tdvu@linagora.com> (cherry picked from commit 7ec02c363c377f8fd73454491f3e6657821cabc0)
This commit is contained in:
@@ -2,41 +2,30 @@
|
||||
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/datasource/state_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.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';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/extensions/state_cache_extension.dart';
|
||||
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
|
||||
|
||||
class StateDataSourceImpl extends StateDataSource {
|
||||
|
||||
final StateCacheClient _stateCacheClient;
|
||||
final StateCacheManager _stateCacheManager;
|
||||
final ExceptionThrower _exceptionThrower;
|
||||
|
||||
StateDataSourceImpl(this._stateCacheClient, this._exceptionThrower);
|
||||
StateDataSourceImpl(this._stateCacheManager, this._exceptionThrower);
|
||||
|
||||
@override
|
||||
Future<State?> getState(AccountId accountId, UserName userName, StateType stateType) {
|
||||
return Future.sync(() async {
|
||||
final stateKey = TupleKey(stateType.name, accountId.asString, userName.value).encodeKey;
|
||||
final stateCache = await _stateCacheClient.getItem(stateKey);
|
||||
return stateCache?.toState();
|
||||
return await _stateCacheManager.getState(accountId, userName, stateType);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveState(AccountId accountId, UserName userName, StateCache stateCache) {
|
||||
return Future.sync(() async {
|
||||
final stateCacheExist = await _stateCacheClient.isExistTable();
|
||||
final stateKey = TupleKey(stateCache.type.name, accountId.asString, userName.value).encodeKey;
|
||||
if (stateCacheExist) {
|
||||
return await _stateCacheClient.updateItem(stateKey, stateCache);
|
||||
} else {
|
||||
return await _stateCacheClient.insertItem(stateKey, stateCache);
|
||||
}
|
||||
return await _stateCacheManager.saveState(accountId, userName, stateCache);
|
||||
}).catchError(_exceptionThrower.throwException);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user