Skip to content

Commit

Permalink
TF-2177 Update account cache when store new account
Browse files Browse the repository at this point in the history
Signed-off-by: dab246 <tdvu@linagora.com>
  • Loading branch information
dab246 committed Oct 24, 2023
1 parent 850eab5 commit 3d626b1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
22 changes: 22 additions & 0 deletions lib/features/login/data/extensions/account_cache_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ extension AccountCacheExtension on AccountCache {
apiUrl: apiUrl,
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
);
}
}
Original file line number Diff line number Diff line change
@@ -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
};
}
}
24 changes: 18 additions & 6 deletions lib/features/login/data/local/account_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
Expand Down
46 changes: 46 additions & 0 deletions test/features/caching/accountl_cache_test.dart
Original file line number Diff line number Diff line change
@@ -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]));
});
});
}

0 comments on commit 3d626b1

Please sign in to comment.