TF-571 Implement AccountRepository with HiveAccountDataSource
This commit is contained in:
committed by
Dat H. Pham
parent
f8e7429751
commit
ff6e6d8878
@@ -0,0 +1,22 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:model/account/account.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/datasource/account_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.dart';
|
||||
|
||||
class HiveAccountDatasourceImpl extends AccountDatasource {
|
||||
final AccountCacheManager _accountCacheManager;
|
||||
|
||||
HiveAccountDatasourceImpl(this._accountCacheManager);
|
||||
|
||||
@override
|
||||
Future<Account> getCurrentAccount() {
|
||||
return _accountCacheManager.getSelectedAccount();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setCurrentAccount(Account newCurrentAccount) {
|
||||
log('HiveAccountDatasourceImpl::setCurrentAccount(): $newCurrentAccount');
|
||||
log('HiveAccountDatasourceImpl::setCurrentAccount(): $_accountCacheManager');
|
||||
return _accountCacheManager.setSelectedAccount(newCurrentAccount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'package:model/account/account.dart';
|
||||
import 'package:model/account/authentication_type.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
||||
|
||||
extension AccountCacheExtension on AccountCache {
|
||||
AuthenticationType fromAuthenticationTypeString() {
|
||||
if (authenticationType == 'basic') {
|
||||
return AuthenticationType.basic;
|
||||
} else if (authenticationType == 'oidc') {
|
||||
return AuthenticationType.oidc;
|
||||
} else {
|
||||
return AuthenticationType.none;
|
||||
}
|
||||
}
|
||||
|
||||
Account toAccount() {
|
||||
final authenticationType = fromAuthenticationTypeString();
|
||||
return Account(id, authenticationType, isSelected: isSelected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:model/account/account.dart';
|
||||
import 'package:model/account/authentication_type.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
||||
|
||||
extension AccountExtensions on Account {
|
||||
AccountCache toCache() {
|
||||
return AccountCache(id, authenticationType.asString(), isSelected: isSelected);
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
|
||||
|
||||
part 'account_cache.g.dart';
|
||||
|
||||
@HiveType(typeId: CachingConstants.ACCOUNT_HIVE_CACHE_IDENTIFY)
|
||||
class AccountCache extends HiveObject with EquatableMixin {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
@HiveField(1)
|
||||
final String authenticationType;
|
||||
|
||||
@HiveField(2)
|
||||
final bool isSelected;
|
||||
|
||||
AccountCache(this.id, this.authenticationType, {required this.isSelected});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [id, authenticationType, isSelected];
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:model/account/account.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/datasource/account_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
||||
|
||||
class AccountRepositoryImpl extends AccountRepository {
|
||||
|
||||
final AccountDatasource _accountDatasource;
|
||||
|
||||
AccountRepositoryImpl(this._accountDatasource);
|
||||
|
||||
@override
|
||||
Future<Account> getCurrentAccount() {
|
||||
return _accountDatasource.getCurrentAccount();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setCurrentAccount(Account newCurrentAccount) {
|
||||
log('AccountRepositoryImpl::setCurrentAccount(): $newCurrentAccount');
|
||||
return _accountDatasource.setCurrentAccount(newCurrentAccount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user