TF-571 Implement AccountRepository with HiveAccountDataSource

This commit is contained in:
Dat PHAM HOANG
2022-05-31 14:26:15 +07:00
committed by Dat H. Pham
parent f8e7429751
commit ff6e6d8878
7 changed files with 256 additions and 0 deletions
@@ -0,0 +1,28 @@
import 'package:core/utils/app_logger.dart';
import 'package:model/account/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/account_extensions.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
class AccountCacheManager {
final AccountCacheClient _accountCacheClient;
AccountCacheManager(this._accountCacheClient);
Future<Account> 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(Account account) {
log('AccountCacheManager::setSelectedAccount(): $_accountCacheClient');
return _accountCacheClient.insertItem(account.id, account.toCache());
}
}