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,133 @@
import 'package:core/utils/app_logger.dart';
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
class AccountCacheClient extends HiveCacheClient<AccountCache> {
@override
String get tableName => "AccountCache";
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxToken = await openBox();
boxToken.clear();
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.deleteAll(listKey);
}).catchError((error) {
throw error;
});
}
@override
Future<List<AccountCache>> getAll() {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<AccountCache?> getItem(String key) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, AccountCache newObject) {
log('AccountCacheClient::insertItem(): $key: $newObject');
return Future.sync(() async {
final boxToken = await openBox();
boxToken.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, AccountCache> mapObject) {
return Future.sync(() async {
final boxToken = await openBox();
boxToken.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxToken = await openBox();
return boxToken.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return await Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<Box<AccountCache>> openBox() {
return Future.sync(() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<AccountCache>(tableName);
}
return await Hive.openBox<AccountCache>(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateItem(String key, AccountCache newObject) {
return Future.sync(() async {
final boxToken = await openBox();
boxToken.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, AccountCache> mapObject) {
return Future.sync(() async {
final boxToken = await openBox();
boxToken.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
}
@@ -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);
}
}