TF-1068 implement clean up save recent & get all login username

This commit is contained in:
Ms Thuỷ
2022-10-24 17:43:36 +07:00
committed by Dat H. Pham
parent 00c37870b1
commit 4beb2ef146
10 changed files with 111 additions and 2 deletions
@@ -1,6 +1,7 @@
import 'package:tmail_ui_user/features/cleanup/domain/model/email_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_url_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_username_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_search_cleanup_rule.dart';
abstract class CleanupDataSource {
@@ -9,4 +10,6 @@ abstract class CleanupDataSource {
Future<void> cleanRecentSearchCache(RecentSearchCleanupRule cleanupRule);
Future<void> cleanRecentLoginUrlCache(RecentLoginUrlCleanupRule cleanupRule);
Future<void> cleanRecentLoginUsernameCache(RecentLoginUsernameCleanupRule cleanupRule);
}
@@ -1,9 +1,11 @@
import 'package:tmail_ui_user/features/cleanup/data/datasource/cleanup_datasource.dart';
import 'package:tmail_ui_user/features/cleanup/data/local/recent_login_url_cache_manager.dart';
import 'package:tmail_ui_user/features/cleanup/data/local/recent_login_username_cache_manager.dart';
import 'package:tmail_ui_user/features/cleanup/data/local/recent_search_cache_manager.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/email_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_url_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_username_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_search_cleanup_rule.dart';
import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dart';
@@ -12,11 +14,13 @@ class CleanupDataSourceImpl extends CleanupDataSource {
final EmailCacheManager emailCacheManager;
final RecentSearchCacheManager recentSearchCacheManager;
final RecentLoginUrlCacheManager recentLoginUrlCacheManager;
final RecentLoginUsernameCacheManager recentLoginUsernameCacheManager;
CleanupDataSourceImpl(
this.emailCacheManager,
this.recentSearchCacheManager,
this.recentLoginUrlCacheManager
this.recentLoginUrlCacheManager,
this.recentLoginUsernameCacheManager
);
@override
@@ -45,4 +49,13 @@ class CleanupDataSourceImpl extends CleanupDataSource {
throw error;
});
}
@override
Future<void> cleanRecentLoginUsernameCache(RecentLoginUsernameCleanupRule cleanupRule) {
return Future.sync(() async {
return await recentLoginUsernameCacheManager.clean(cleanupRule);
}).catchError((error) {
throw error;
});
}
}
@@ -0,0 +1,27 @@
import 'package:tmail_ui_user/features/caching/recent_login_username_cache_client.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_username_cleanup_rule.dart';
import 'package:tmail_ui_user/features/login/data/model/recent_login_username_cache.dart';
class RecentLoginUsernameCacheManager {
final RecentLoginUsernameCacheClient _recentLoginUsernameCacheClient;
RecentLoginUsernameCacheManager(this._recentLoginUsernameCacheClient);
Future<void> clean(RecentLoginUsernameCleanupRule cleanupRule) async {
final recentCacheExist = await _recentLoginUsernameCacheClient.isExistTable();
if (recentCacheExist) {
final listRecentUsernameCache = await _recentLoginUsernameCacheClient.getAll();
listRecentUsernameCache.sortByCreationDate();
if (listRecentUsernameCache.length > cleanupRule.storageLimit) {
final newListKeyRecent = listRecentUsernameCache
.sublist(cleanupRule.storageLimit)
.map((recent) => recent.username)
.toList();
await _recentLoginUsernameCacheClient.deleteMultipleItem(newListKeyRecent);
}
}
}
}
@@ -2,6 +2,7 @@
import 'package:tmail_ui_user/features/cleanup/data/datasource/cleanup_datasource.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/email_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_url_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_username_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_search_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/repository/cleanup_repository.dart';
@@ -25,4 +26,9 @@ class CleanupRepositoryImpl extends CleanupRepository {
Future<void> cleanRecentLoginUrlCache(RecentLoginUrlCleanupRule cleanupRule) {
return cleanupDataSource.cleanRecentLoginUrlCache(cleanupRule);
}
@override
Future<void> cleanRecentLoginUsernameCache(RecentLoginUsernameCleanupRule cleanupRule) {
return cleanupDataSource.cleanRecentLoginUsernameCache(cleanupRule);
}
}
@@ -0,0 +1,10 @@
import 'package:tmail_ui_user/features/cleanup/domain/model/cleanup_rule.dart';
class RecentLoginUsernameCleanupRule extends CleanupRule {
final int storageLimit;
RecentLoginUsernameCleanupRule({this.storageLimit = 10}) : super();
@override
List<Object?> get props => [storageLimit];
}
@@ -1,5 +1,6 @@
import 'package:tmail_ui_user/features/cleanup/domain/model/email_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_url_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_username_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_search_cleanup_rule.dart';
abstract class CleanupRepository {
@@ -8,4 +9,6 @@ abstract class CleanupRepository {
Future<void> cleanRecentSearchCache(RecentSearchCleanupRule cleanupRule);
Future<void> cleanRecentLoginUrlCache(RecentLoginUrlCleanupRule cleanupRule);
Future<void> cleanRecentLoginUsernameCache(RecentLoginUsernameCleanupRule cleanupRule);
}
@@ -0,0 +1,18 @@
import 'package:core/core.dart';
class CleanupRecentLoginUsernameCacheSuccess extends UIState {
CleanupRecentLoginUsernameCacheSuccess();
@override
List<Object> get props => [];
}
class CleanupRecentLoginUsernameCacheFailure extends FeatureFailure {
final dynamic exception;
CleanupRecentLoginUsernameCacheFailure(this.exception);
@override
List<Object> get props => [exception];
}
@@ -0,0 +1,21 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:tmail_ui_user/features/cleanup/domain/model/recent_login_username_cleanup_rule.dart';
import 'package:tmail_ui_user/features/cleanup/domain/repository/cleanup_repository.dart';
import 'package:tmail_ui_user/features/cleanup/domain/state/cleanup_recent_login_username_cache_state.dart';
class CleanupRecentLoginUsernameCacheInteractor {
final CleanupRepository cleanupRepository;
CleanupRecentLoginUsernameCacheInteractor(this.cleanupRepository);
Future<Either<Failure, Success>> execute(RecentLoginUsernameCleanupRule cleanupRule) async {
try {
await cleanupRepository.cleanRecentLoginUsernameCache(cleanupRule);
return Right<Failure, Success>(CleanupRecentLoginUsernameCacheSuccess());
} catch (e) {
return Left(CleanupRecentLoginUsernameCacheFailure(e));
}
}
}