Skip to content

Commit

Permalink
TF-2310 Upgrade hive database to version 10
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 authored and hoangdat committed Apr 15, 2024
1 parent 017c713 commit 5eb75ee
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
4 changes: 4 additions & 0 deletions lib/features/base/upgradeable/upgrade_database_steps.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

abstract class UpgradeDatabaseSteps {
Future<void> onUpgrade(int oldVersion, int newVersion);
}
17 changes: 17 additions & 0 deletions lib/features/base/upgradeable/upgrade_hive_database_steps_v10.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import 'package:tmail_ui_user/features/base/upgradeable/upgrade_database_steps.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';

class UpgradeHiveDatabaseStepsV10 extends UpgradeDatabaseSteps {

final CachingManager _cachingManager;

UpgradeHiveDatabaseStepsV10(this._cachingManager);

@override
Future<void> onUpgrade(int oldVersion, int newVersion) async {
if (oldVersion > 0 && oldVersion < newVersion && newVersion == 10) {
await _cachingManager.clearLoginRecentData();
}
}
}
17 changes: 17 additions & 0 deletions lib/features/base/upgradeable/upgrade_hive_database_steps_v7.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import 'package:tmail_ui_user/features/base/upgradeable/upgrade_database_steps.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';

class UpgradeHiveDatabaseStepsV7 extends UpgradeDatabaseSteps {

final CachingManager _cachingManager;

UpgradeHiveDatabaseStepsV7(this._cachingManager);

@override
Future<void> onUpgrade(int oldVersion, int newVersion) async {
if (oldVersion > 0 && oldVersion < newVersion && newVersion == 7) {
await _cachingManager.clearAll();
}
}
}
22 changes: 13 additions & 9 deletions lib/features/caching/caching_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:tmail_ui_user/features/caching/clients/hive_cache_version_client
import 'package:tmail_ui_user/features/caching/clients/mailbox_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/new_email_hive_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/opened_email_hive_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/recent_login_url_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/recent_login_username_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/recent_search_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/session_hive_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/state_cache_client.dart';
Expand All @@ -26,6 +28,8 @@ class CachingManager {
final StateCacheClient _stateCacheClient;
final EmailCacheClient _emailCacheClient;
final RecentSearchCacheClient _recentSearchCacheClient;
final RecentLoginUrlCacheClient _recentLoginUrlCacheClient;
final RecentLoginUsernameCacheClient _recentLoginUsernameCacheClient;
final AccountCacheClient _accountCacheClient;
final FcmCacheClient _fcmCacheClient;
final FirebaseRegistrationCacheClient _firebaseRegistrationCacheClient;
Expand All @@ -43,6 +47,8 @@ class CachingManager {
this._stateCacheClient,
this._emailCacheClient,
this._recentSearchCacheClient,
this._recentLoginUrlCacheClient,
this._recentLoginUsernameCacheClient,
this._accountCacheClient,
this._fcmCacheClient,
this._firebaseRegistrationCacheClient,
Expand Down Expand Up @@ -110,15 +116,6 @@ class CachingManager {
], eagerError: true);
}

Future<void> onUpgradeCache(int oldVersion, int newVersion) async {
log('CachingManager::onUpgradeCache():oldVersion $oldVersion | newVersion: $newVersion');
await clearData();
if (oldVersion > 0 && oldVersion < newVersion && newVersion == 7) {
await clearAll();
}
await storeCacheVersion(newVersion);
}

Future<bool> storeCacheVersion(int newVersion) async {
log('CachingManager::storeCacheVersion()');
return _hiveCacheVersionClient.storeVersion(newVersion);
Expand All @@ -138,4 +135,11 @@ class CachingManager {
_fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName),
]);
}

Future<void> clearLoginRecentData() async {
await Future.wait([
_recentLoginUrlCacheClient.clearAllData(),
_recentLoginUsernameCacheClient.clearAllData(),
]);
}
}
2 changes: 1 addition & 1 deletion lib/features/caching/config/cache_version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

class CacheVersion {
static const int hiveDBVersion = 9;
static const int hiveDBVersion = 10;
}
10 changes: 7 additions & 3 deletions lib/features/caching/config/hive_cache_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v10.dart';
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v7.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
import 'package:tmail_ui_user/features/caching/config/cache_version.dart';
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
Expand Down Expand Up @@ -60,9 +62,11 @@ class HiveCacheConfig {
final oldVersion = await cachingManager.getLatestVersion() ?? 0;
const newVersion = CacheVersion.hiveDBVersion;
log('HiveCacheConfig::onUpgradeDatabase():oldVersion: $oldVersion | newVersion: $newVersion');
if (oldVersion != newVersion) {
await cachingManager.onUpgradeCache(oldVersion, newVersion);
}

await UpgradeHiveDatabaseStepsV7(cachingManager).onUpgrade(oldVersion, newVersion);
await UpgradeHiveDatabaseStepsV10(cachingManager).onUpgrade(oldVersion, newVersion);

await cachingManager.storeCacheVersion(newVersion);
}

Future<void> initializeEncryptionKey() async {
Expand Down
2 changes: 2 additions & 0 deletions lib/main/bindings/local/local_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class LocalBindings extends Bindings {
Get.find<StateCacheClient>(),
Get.find<EmailCacheClient>(),
Get.find<RecentSearchCacheClient>(),
Get.find<RecentLoginUrlCacheClient>(),
Get.find<RecentLoginUsernameCacheClient>(),
Get.find<AccountCacheClient>(),
Get.find<FcmCacheClient>(),
Get.find<FirebaseRegistrationCacheClient>(),
Expand Down

0 comments on commit 5eb75ee

Please sign in to comment.