TF-1068 implement data layer save recent login username & get all login username
This commit is contained in:
@@ -11,6 +11,7 @@ import 'package:tmail_ui_user/features/login/data/model/account_cache.dart';
|
|||||||
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
|
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/model/encryption_key_cache.dart';
|
import 'package:tmail_ui_user/features/login/data/model/encryption_key_cache.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/model/recent_login_url_cache.dart';
|
import 'package:tmail_ui_user/features/login/data/model/recent_login_url_cache.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/model/recent_login_username_cache.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
|
import 'package:tmail_ui_user/features/login/data/model/token_oidc_cache.dart';
|
||||||
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart';
|
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_cache.dart';
|
||||||
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_rights_cache.dart';
|
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_rights_cache.dart';
|
||||||
@@ -76,6 +77,7 @@ class HiveCacheConfig {
|
|||||||
Hive.registerAdapter(EncryptionKeyCacheAdapter());
|
Hive.registerAdapter(EncryptionKeyCacheAdapter());
|
||||||
Hive.registerAdapter(AuthenticationInfoCacheAdapter());
|
Hive.registerAdapter(AuthenticationInfoCacheAdapter());
|
||||||
Hive.registerAdapter(RecentLoginUrlCacheAdapter());
|
Hive.registerAdapter(RecentLoginUrlCacheAdapter());
|
||||||
|
Hive.registerAdapter(RecentLoginUsernameCacheAdapter());
|
||||||
}
|
}
|
||||||
|
|
||||||
Future closeHive() async {
|
Future closeHive() async {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/model/recent_login_username_cache.dart';
|
||||||
|
|
||||||
|
class RecentLoginUsernameCacheClient extends HiveCacheClient<RecentLoginUsernameCache> {
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get tableName => 'RecentLoginUsernameCache';
|
||||||
|
}
|
||||||
@@ -12,4 +12,5 @@ class CachingConstants {
|
|||||||
static const int ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY = 10;
|
static const int ENCRYPTION_KEY_HIVE_CACHE_IDENTIFY = 10;
|
||||||
static const int AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY = 11;
|
static const int AUTHENTICATION_INFO_HIVE_CACHE_IDENTIFY = 11;
|
||||||
static const int RECENT_LOGIN_URL_HIVE_CACHE_IDENTITY = 12;
|
static const int RECENT_LOGIN_URL_HIVE_CACHE_IDENTITY = 12;
|
||||||
|
static const int RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY = 13;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import 'package:tmail_ui_user/features/caching/recent_login_username_cache_client.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/datasource/login_username_datasource.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/data/model/recent_login_username_cache.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/extensions/list_recent_login_username_extension.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/model/recent_login_username.dart';
|
||||||
|
|
||||||
|
class LoginUsernameDataSourceImpl implements LoginUsernameDataSource {
|
||||||
|
final RecentLoginUsernameCacheClient _recentLoginUsernameCacheClient;
|
||||||
|
|
||||||
|
LoginUsernameDataSourceImpl(this._recentLoginUsernameCacheClient);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<RecentLoginUsername>> getAllRecentLoginUsernamesLatest({int? limit, String? pattern}) {
|
||||||
|
return Future.sync(() async {
|
||||||
|
final listRecentUsernameCache = await _recentLoginUsernameCacheClient.getAll();
|
||||||
|
final listValidRecentUsername = listRecentUsernameCache
|
||||||
|
.where((recentCache) => _filterRecentLoginUsernameCache(recentCache, pattern))
|
||||||
|
.map((recentCache) => recentCache.toRecentLoginUsername())
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
listValidRecentUsername.sortByCreationDate();
|
||||||
|
|
||||||
|
final newLimit = limit ?? 5;
|
||||||
|
|
||||||
|
return listValidRecentUsername.length > newLimit
|
||||||
|
? listValidRecentUsername.sublist(0, newLimit)
|
||||||
|
: listValidRecentUsername;
|
||||||
|
}).catchError((error) {
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> saveLoginUsername(RecentLoginUsername recentLoginUsername) {
|
||||||
|
return Future.sync(() async {
|
||||||
|
if (await _recentLoginUsernameCacheClient
|
||||||
|
.isExistItem(recentLoginUsername.username)) {
|
||||||
|
await _recentLoginUsernameCacheClient.updateItem(recentLoginUsername.username,
|
||||||
|
recentLoginUsername.toRecentLoginUsernameCache());
|
||||||
|
} else {
|
||||||
|
await _recentLoginUsernameCacheClient.insertItem(recentLoginUsername.username,
|
||||||
|
recentLoginUsername.toRecentLoginUsernameCache());
|
||||||
|
}
|
||||||
|
}).catchError((error) {
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _filterRecentLoginUsernameCache(
|
||||||
|
RecentLoginUsernameCache recentLoginUsernameCache,
|
||||||
|
String? pattern
|
||||||
|
) {
|
||||||
|
if (pattern == null || pattern.trim().isEmpty) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return recentLoginUsernameCache.matchUsername(pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/extensions/date_time_extension.dart';
|
||||||
|
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/model/recent_login_username.dart';
|
||||||
|
|
||||||
|
part 'recent_login_username_cache.g.dart';
|
||||||
|
|
||||||
|
@HiveType(typeId: CachingConstants.RECENT_LOGIN_USERNAME_HIVE_CACHE_IDENTITY)
|
||||||
|
class RecentLoginUsernameCache extends HiveObject with EquatableMixin {
|
||||||
|
|
||||||
|
@HiveField(0)
|
||||||
|
final String username;
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
final DateTime creationDate;
|
||||||
|
|
||||||
|
RecentLoginUsernameCache(this.username, this.creationDate);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [username, creationDate];
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RecentLoginUsernameExtension on RecentLoginUsername {
|
||||||
|
RecentLoginUsernameCache toRecentLoginUsernameCache() {
|
||||||
|
return RecentLoginUsernameCache(username, creationDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RecentLoginUsernameCacheExtension on RecentLoginUsernameCache {
|
||||||
|
RecentLoginUsername toRecentLoginUsername() {
|
||||||
|
return RecentLoginUsername(username, creationDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool matchUsername(String pattern) {
|
||||||
|
return username.toLowerCase().contains(pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ListRecentLoginUsernameCacheExtension on List<RecentLoginUsernameCache> {
|
||||||
|
void sortByCreationDate() {
|
||||||
|
sort((recentUsername1, recentUsername2) {
|
||||||
|
return recentUsername1.creationDate.compareToSort(recentUsername2.creationDate, false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import 'package:tmail_ui_user/features/login/data/datasource/login_username_datasource.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/model/recent_login_username.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/repository/login_username_repository.dart';
|
||||||
|
|
||||||
|
class LoginUsernameRepositoryImpl implements LoginUsernameRepository {
|
||||||
|
final LoginUsernameDataSource loginUsernameDatasource;
|
||||||
|
|
||||||
|
LoginUsernameRepositoryImpl(this.loginUsernameDatasource);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<RecentLoginUsername>> getAllRecentLoginUsernameLatest({int? limit, String? pattern}) {
|
||||||
|
return loginUsernameDatasource.getAllRecentLoginUsernamesLatest(limit: limit, pattern: pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> saveLoginUsername(RecentLoginUsername recentLoginUsername) {
|
||||||
|
return loginUsernameDatasource.saveLoginUsername(recentLoginUsername);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
class RecentLoginUsername with EquatableMixin {
|
||||||
|
final String username;
|
||||||
|
final DateTime creationDate;
|
||||||
|
|
||||||
|
RecentLoginUsername(this.username, this.creationDate);
|
||||||
|
|
||||||
|
factory RecentLoginUsername.now(String username) => RecentLoginUsername(username, DateTime.now());
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [ username, creationDate ];
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import 'package:core/presentation/state/failure.dart';
|
||||||
|
import 'package:core/presentation/state/success.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/model/recent_login_username.dart';
|
||||||
|
|
||||||
|
class GetAllRecentLoginUsernameLatestSuccess extends UIState {
|
||||||
|
|
||||||
|
final List<RecentLoginUsername> listRecentLoginUsername;
|
||||||
|
|
||||||
|
GetAllRecentLoginUsernameLatestSuccess(this.listRecentLoginUsername);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [listRecentLoginUsername];
|
||||||
|
}
|
||||||
|
|
||||||
|
class GetAllRecentLoginUsernameLatestFailure extends FeatureFailure {
|
||||||
|
final dynamic exception;
|
||||||
|
|
||||||
|
GetAllRecentLoginUsernameLatestFailure(this.exception);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [exception];
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import 'package:core/presentation/state/failure.dart';
|
||||||
|
import 'package:core/presentation/state/success.dart';
|
||||||
|
|
||||||
|
class SaveRecentLoginUsernameSuccess extends UIState {
|
||||||
|
|
||||||
|
SaveRecentLoginUsernameSuccess();
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
class SaveRecentLoginUsernameFailed extends FeatureFailure {
|
||||||
|
final dynamic exception;
|
||||||
|
|
||||||
|
SaveRecentLoginUsernameFailed(this.exception);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [exception];
|
||||||
|
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/repository/login_username_repository.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/state/get_all_recent_login_username_state.dart';
|
||||||
|
|
||||||
|
class GetAllRecentLoginUsernameOnMobileInteractor {
|
||||||
|
final LoginUsernameRepository loginUsernameRepository;
|
||||||
|
|
||||||
|
GetAllRecentLoginUsernameOnMobileInteractor(this.loginUsernameRepository);
|
||||||
|
|
||||||
|
Future<Either<Failure, Success>> execute({int? limit, String? pattern}) async {
|
||||||
|
try {
|
||||||
|
final listRecent = await loginUsernameRepository.getAllRecentLoginUsernameLatest(
|
||||||
|
limit: limit, pattern: pattern);
|
||||||
|
return Right(GetAllRecentLoginUsernameLatestSuccess(listRecent));
|
||||||
|
} catch (exception) {
|
||||||
|
return Left(GetAllRecentLoginUsernameLatestFailure(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import 'package:core/core.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/model/recent_login_username.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/repository/login_username_repository.dart';
|
||||||
|
import 'package:tmail_ui_user/features/login/domain/state/save_recent_login_username_state.dart';
|
||||||
|
|
||||||
|
class SaveLoginUsernameOnMobileInteractor {
|
||||||
|
final LoginUsernameRepository loginUsernameRepository;
|
||||||
|
|
||||||
|
SaveLoginUsernameOnMobileInteractor(this.loginUsernameRepository);
|
||||||
|
|
||||||
|
Stream<Either<Failure, Success>> execute(RecentLoginUsername recentLoginUsername) async* {
|
||||||
|
try {
|
||||||
|
await loginUsernameRepository.saveLoginUsername(recentLoginUsername);
|
||||||
|
yield Right(SaveRecentLoginUsernameSuccess());
|
||||||
|
} catch(exception) {
|
||||||
|
yield Left(SaveRecentLoginUsernameFailed(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user