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:
@@ -26,4 +26,26 @@ extension AccountCacheExtension on AccountCache {
|
|||||||
apiUrl: apiUrl,
|
apiUrl: apiUrl,
|
||||||
userName: userName != null ? UserName(userName!) : null);
|
userName: userName != null ? UserName(userName!) : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AccountCache unselected() {
|
||||||
|
return AccountCache(
|
||||||
|
id,
|
||||||
|
authenticationType,
|
||||||
|
isSelected: false,
|
||||||
|
accountId: accountId,
|
||||||
|
apiUrl: apiUrl,
|
||||||
|
userName: userName
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
AccountCache emptyId() {
|
||||||
|
return AccountCache(
|
||||||
|
'',
|
||||||
|
authenticationType,
|
||||||
|
isSelected: false,
|
||||||
|
accountId: accountId,
|
||||||
|
apiUrl: apiUrl,
|
||||||
|
userName: userName
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:core/utils/app_logger.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/extensions/account_cache_extensions.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
||||||
|
|
||||||
|
extension ListAccountCacheExtension on List<AccountCache> {
|
||||||
|
List<AccountCache> unselected() => map((account) => account.unselected()).toList();
|
||||||
|
|
||||||
|
List<AccountCache> removeDuplicated() {
|
||||||
|
final listAccountId = map((account) => account.accountId).whereNotNull().toSet();
|
||||||
|
log('ListAccountCacheExtension::removeDuplicated:listAccountId: $listAccountId');
|
||||||
|
retainWhere((account) => listAccountId.remove(account.accountId));
|
||||||
|
log('ListAccountCacheExtension::removeDuplicated:listAccount: $this');
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, AccountCache> toMap() {
|
||||||
|
return {
|
||||||
|
for (var account in this)
|
||||||
|
account.id: account
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import 'package:core/utils/app_logger.dart';
|
|||||||
import 'package:model/account/personal_account.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/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/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/data/extensions/personal_account_extension.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.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 {
|
Future<void> setCurrentAccount(PersonalAccount newAccount) async {
|
||||||
log('AccountCacheManager::setCurrentAccount(): $account');
|
log('AccountCacheManager::setCurrentAccount(): $newAccount');
|
||||||
final accountCacheExist = await _accountCacheClient.isExistTable();
|
final newAccountCache = newAccount.toCache();
|
||||||
if (accountCacheExist) {
|
final allAccounts = await _accountCacheClient.getAll();
|
||||||
await _accountCacheClient.clearAllData();
|
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) {
|
Future<void> deleteCurrentAccount(String hashId) {
|
||||||
log('AccountCacheManager::deleteCurrentAccount(): $hashId');
|
log('AccountCacheManager::deleteCurrentAccount(): $hashId');
|
||||||
return _accountCacheClient.deleteItem(hashId);
|
return _accountCacheClient.deleteItem(hashId);
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/extensions/list_account_cache_extensions.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('AccountCache test', () {
|
||||||
|
test('removeDuplicated should remove duplicate accountId', () async {
|
||||||
|
final account1 = AccountCache(
|
||||||
|
'1',
|
||||||
|
'oidc',
|
||||||
|
isSelected: true,
|
||||||
|
accountId: '1',
|
||||||
|
userName: '1',
|
||||||
|
apiUrl: 'https://example.com/jmap'
|
||||||
|
);
|
||||||
|
final account2 = AccountCache(
|
||||||
|
'2',
|
||||||
|
'oidc',
|
||||||
|
isSelected: true,
|
||||||
|
accountId: '1',
|
||||||
|
userName: '1',
|
||||||
|
apiUrl: 'https://example.com/jmap'
|
||||||
|
);
|
||||||
|
final account3 = AccountCache(
|
||||||
|
'3',
|
||||||
|
'basic',
|
||||||
|
isSelected: true,
|
||||||
|
accountId: '2',
|
||||||
|
userName: '2',
|
||||||
|
apiUrl: 'https://example.com/jmap'
|
||||||
|
);
|
||||||
|
final account4 = AccountCache(
|
||||||
|
'4',
|
||||||
|
'basic',
|
||||||
|
isSelected: true,
|
||||||
|
accountId: '2',
|
||||||
|
userName: '2',
|
||||||
|
apiUrl: 'https://example.com/jmap'
|
||||||
|
);
|
||||||
|
|
||||||
|
final result = [account1, account2, account3, account4].removeDuplicated();
|
||||||
|
expect(result, equals([account1, account3]));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user