TF-2177 Update account cache when store new account

Signed-off-by: dab246 <tdvu@linagora.com>
(cherry picked from commit 3fff318bbe66a5f4387c25d18e0d2a9480719d72)
This commit is contained in:
dab246
2023-10-24 14:08:54 +07:00
committed by Dat H. Pham
parent 6a0b3a7bc4
commit fc4a4792c3
4 changed files with 109 additions and 6 deletions
@@ -3,6 +3,7 @@ import 'package:core/utils/app_logger.dart';
import 'package:model/account/personal_account.dart';
import 'package:tmail_ui_user/features/caching/clients/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/list_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';
@@ -23,15 +24,26 @@ class AccountCacheManager {
}
}
Future<void> setCurrentAccount(PersonalAccount account) async {
log('AccountCacheManager::setCurrentAccount(): $account');
final accountCacheExist = await _accountCacheClient.isExistTable();
if (accountCacheExist) {
await _accountCacheClient.clearAllData();
Future<void> setCurrentAccount(PersonalAccount newAccount) async {
log('AccountCacheManager::setCurrentAccount(): $newAccount');
final newAccountCache = newAccount.toCache();
final allAccounts = await _accountCacheClient.getAll();
log('AccountCacheManager::setCurrentAccount::allAccounts(): $allAccounts');
if (allAccounts.isNotEmpty) {
final newAllAccounts = allAccounts
.unselected()
.removeDuplicated()
.whereNot((account) => account.accountId == newAccountCache.accountId)
.toList();
if (newAllAccounts.isNotEmpty) {
await _accountCacheClient.clearAllData();
await _accountCacheClient.updateMultipleItem(newAllAccounts.toMap());
}
}
return _accountCacheClient.insertItem(account.id, account.toCache());
return _accountCacheClient.insertItem(newAccountCache.id, newAccountCache);
}
Future<void> deleteCurrentAccount(String hashId) {
log('AccountCacheManager::deleteCurrentAccount(): $hashId');
return _accountCacheClient.deleteItem(hashId);