TF-148 Add data layer for cleanup email cache
This commit is contained in:
@@ -15,6 +15,12 @@ extension DateTimeExtension on DateTime {
|
||||
final now = DateTime.now();
|
||||
return now.year == this.year;
|
||||
}
|
||||
|
||||
int daysBetween(DateTime from) {
|
||||
from = DateTime(from.year, from.month, from.day);
|
||||
final to = DateTime(year, month, day);
|
||||
return (to.difference(from).inHours / 24).round().abs();
|
||||
}
|
||||
}
|
||||
|
||||
extension DateTimeNullableExtension on DateTime? {
|
||||
|
||||
@@ -72,6 +72,8 @@ dependencies:
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
test: 1.16.8
|
||||
# For information on the generic Dart part of this file, see the
|
||||
# following page: https://dart.dev/tools/pub/pubspec
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:core/core.dart';
|
||||
|
||||
void main() {
|
||||
group('datetime test', () {
|
||||
|
||||
test('daysBetween should return 1 day2 when day2 > day1', () async {
|
||||
DateTime date1 = DateTime.parse("2021-10-20 12:59:12.000");
|
||||
DateTime date2 = DateTime.parse("2021-10-21 10:29:03.000");
|
||||
|
||||
expect(date2.daysBetween(date1), 1);
|
||||
});
|
||||
|
||||
test('daysBetween should return 1 days when day1 > day2', () async {
|
||||
DateTime date1 = DateTime.parse("2021-10-22 12:59:12.000");
|
||||
DateTime date2 = DateTime.parse("2021-10-21 10:29:03.000");
|
||||
|
||||
expect(date2.daysBetween(date1), 1);
|
||||
});
|
||||
|
||||
test('daysBetween should return 0 day when day1 = day2', () async {
|
||||
DateTime date1 = DateTime.parse("2021-10-22 12:59:12.000");
|
||||
DateTime date2 = DateTime.parse("2021-10-22 10:29:03.000");
|
||||
|
||||
expect(date2.daysBetween(date1), 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
import 'package:tmail_ui_user/features/cleanup/domain/model/cleanup_rule.dart';
|
||||
|
||||
abstract class CleanupDataSource {
|
||||
Future<bool> cleanEmailCache(CleanupRule cleanupRule);
|
||||
|
||||
Future<void> saveTimeCleanEmail(DateTime lastTime);
|
||||
|
||||
Future<DateTime?> getTimeCleanEmail();
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/data/datasource/cleanup_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/data/utils/cleanup_constant.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/domain/model/cleanup_rule.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/local/email_cache_manager.dart';
|
||||
|
||||
class CleanupDataSourceImpl extends CleanupDataSource {
|
||||
|
||||
final EmailCacheManager emailCacheManager;
|
||||
final SharedPreferences sharedPreferences;
|
||||
|
||||
CleanupDataSourceImpl(this.emailCacheManager, this.sharedPreferences);
|
||||
|
||||
@override
|
||||
Future<bool> cleanEmailCache(CleanupRule cleanupRule) {
|
||||
return Future.sync(() async {
|
||||
return await emailCacheManager.clean(cleanupRule);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveTimeCleanEmail(DateTime lastTime) {
|
||||
return Future.sync(() async {
|
||||
await sharedPreferences.setString(CleanupConstant.keyTimeCleanupEmail, lastTime.toIso8601String());
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DateTime?> getTimeCleanEmail() {
|
||||
return Future.sync(() async {
|
||||
final lastTime = sharedPreferences.getString(CleanupConstant.keyTimeCleanupEmail);
|
||||
return DateTime.tryParse(lastTime ?? '');
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/data/datasource/cleanup_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/domain/model/cleanup_rule.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/domain/repository/cleanup_repository.dart';
|
||||
|
||||
class CleanupRepositoryImpl extends CleanupRepository {
|
||||
|
||||
final CleanupDataSource cleanupDataSource;
|
||||
|
||||
CleanupRepositoryImpl(this.cleanupDataSource);
|
||||
|
||||
@override
|
||||
Future<void> cleanEmailCache(CleanupRule cleanupRule) async {
|
||||
final lastTimeCleanup = await cleanupDataSource.getTimeCleanEmail();
|
||||
if (lastTimeCleanup == null || !lastTimeCleanup.isToday()) {
|
||||
final isSuccess = await cleanupDataSource.cleanEmailCache(cleanupRule);
|
||||
if (isSuccess) {
|
||||
await cleanupDataSource.saveTimeCleanEmail(DateTime.now());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class CleanupConstant {
|
||||
static const keyTimeCleanupEmail = 'KEY_TIME_CLEANUP_EMAIL';
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/utc_date.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/domain/model/cleanup_rule.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/extensions/email_address_hive_cache_extension.dart';
|
||||
|
||||
@@ -31,4 +33,15 @@ extension EmailCacheExtension on EmailCache {
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
bool expireTimeCaching(CleanupRule cleanupRule) {
|
||||
final currentTime = DateTime.now();
|
||||
if (receivedAt != null) {
|
||||
final countDay = currentTime.daysBetween(receivedAt!.toLocal());
|
||||
if (countDay >= cleanupRule.cachingEmailPeriod.countDay) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'package:model/model.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:tmail_ui_user/features/caching/email_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/cleanup/domain/model/cleanup_rule.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/extensions/email_cache_extension.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/extensions/email_extension.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/extensions/list_email_cache_extension.dart';
|
||||
@@ -49,5 +50,17 @@ class EmailCacheManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<bool> clean(CleanupRule cleanupRule) async {
|
||||
final emailCacheExist = await _emailCacheClient.isExistTable();
|
||||
if (emailCacheExist) {
|
||||
final listEmailCache = await _emailCacheClient.getAll();
|
||||
final listEmailIdCacheExpire = listEmailCache
|
||||
.where((emailCache) => emailCache.expireTimeCaching(cleanupRule))
|
||||
.map((emailCache) => emailCache.id)
|
||||
.toList();
|
||||
await _emailCacheClient.deleteMultipleItem(listEmailIdCacheExpire);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user