Files
workavia-mail-front/lib/features/login/data/local/account_cache_manager.dart
T
dab246 876cc368b0 Use TupleKey(ObjectKey-AccountId-UserName) to store data to hive
(cherry picked from commit e0ace535d5f3af71eb13598d92524caf2c6d9bbf)
2023-05-08 13:03:51 +07:00

33 lines
1.3 KiB
Dart

import 'package:core/utils/app_logger.dart';
import 'package:model/account/personal_account.dart';
import 'package:tmail_ui_user/features/caching/account_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/extensions/account_cache_extensions.dart';
import 'package:tmail_ui_user/features/login/data/extensions/personal_account_extension.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
class AccountCacheManager {
final AccountCacheClient _accountCacheClient;
AccountCacheManager(this._accountCacheClient);
Future<PersonalAccount> getSelectedAccount() async {
try {
final allAccounts = await _accountCacheClient.getAll();
return allAccounts.firstWhere((account) => account.isSelected)
.toAccount();
} catch (e) {
logError('AccountCacheManager::getSelectedAccount(): $e');
throw NotFoundAuthenticatedAccountException();
}
}
Future<void> setSelectedAccount(PersonalAccount account) {
log('AccountCacheManager::setSelectedAccount(): $_accountCacheClient');
return _accountCacheClient.insertItem(account.id, account.toCache());
}
Future<void> deleteSelectedAccount(String accountId) {
log('AccountCacheManager::deleteSelectedAccount(): $accountId');
return _accountCacheClient.deleteItem(accountId);
}
}