From bf3f4e560d10402d9f58a74281be19cef8491de2 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 21 Feb 2023 09:25:31 -0600 Subject: [PATCH] chore(mobile): Improve reliability of asset loading and indexing (#1813) * chore(mobile): Improve reliability of asset loading and indexing * chore: add comments * chore: remove log * fix: put back box open sequence --- .../backup/providers/backup.provider.dart | 26 ++++++++++++------- mobile/lib/modules/home/views/home_page.dart | 2 +- mobile/lib/routing/auth_guard.dart | 2 ++ mobile/lib/shared/services/asset.service.dart | 6 +++++ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/mobile/lib/modules/backup/providers/backup.provider.dart b/mobile/lib/modules/backup/providers/backup.provider.dart index daad2c4fd3..ac40fbd304 100644 --- a/mobile/lib/modules/backup/providers/backup.provider.dart +++ b/mobile/lib/modules/backup/providers/backup.provider.dart @@ -587,6 +587,7 @@ class BackupNotifier extends StateNotifier { log.warning("WARNING [resumeBackup] failed to acquireLock"); return; } + await Future.wait([ Hive.openBox(hiveBackupInfoBox), Hive.openBox(duplicatedAssetsBox), @@ -597,16 +598,21 @@ class BackupNotifier extends StateNotifier { Set selectedAlbums = state.selectedBackupAlbums; Set excludedAlbums = state.excludedBackupAlbums; if (albums != null) { - selectedAlbums = _updateAlbumsBackupTime( - selectedAlbums, - albums.selectedAlbumIds, - albums.lastSelectedBackupTime, - ); - excludedAlbums = _updateAlbumsBackupTime( - excludedAlbums, - albums.excludedAlbumsIds, - albums.lastExcludedBackupTime, - ); + if (selectedAlbums.isNotEmpty) { + selectedAlbums = _updateAlbumsBackupTime( + selectedAlbums, + albums.selectedAlbumIds, + albums.lastSelectedBackupTime, + ); + } + + if (excludedAlbums.isNotEmpty) { + excludedAlbums = _updateAlbumsBackupTime( + excludedAlbums, + albums.excludedAlbumsIds, + albums.lastExcludedBackupTime, + ); + } } final Box backgroundBox = Hive.box(backgroundBackupInfoBox); state = state.copyWith( diff --git a/mobile/lib/modules/home/views/home_page.dart b/mobile/lib/modules/home/views/home_page.dart index 86296ae702..ca3bc4b2d6 100644 --- a/mobile/lib/modules/home/views/home_page.dart +++ b/mobile/lib/modules/home/views/home_page.dart @@ -67,7 +67,7 @@ class HomePage extends HookConsumerWidget { ); void reloadAllAsset() { - ref.read(assetProvider.notifier).getAllAsset(); + ref.watch(assetProvider.notifier).getAllAsset(); } Widget buildBody() { diff --git a/mobile/lib/routing/auth_guard.dart b/mobile/lib/routing/auth_guard.dart index 53693ea1de..4e907a768d 100644 --- a/mobile/lib/routing/auth_guard.dart +++ b/mobile/lib/routing/auth_guard.dart @@ -11,6 +11,8 @@ class AuthGuard extends AutoRouteGuard { @override void onNavigation(NavigationResolver resolver, StackRouter router) async { try { + // temporary fix for race condition that the _apiService + // get called before accessToken is set var userInfoHiveBox = await Hive.openBox(userInfoBox); var accessToken = userInfoHiveBox.get(accessTokenKey); _apiService.setAccessToken(accessToken); diff --git a/mobile/lib/shared/services/asset.service.dart b/mobile/lib/shared/services/asset.service.dart index 724cc53b20..25c19b1a80 100644 --- a/mobile/lib/shared/services/asset.service.dart +++ b/mobile/lib/shared/services/asset.service.dart @@ -35,6 +35,12 @@ class AssetService { /// Returns `null` if the server state did not change, else list of assets Future?, String?>> getRemoteAssets({String? etag}) async { try { + // temporary fix for race condition that the _apiService + // get called before accessToken is set + var userInfoHiveBox = await Hive.openBox(userInfoBox); + var accessToken = userInfoHiveBox.get(accessTokenKey); + _apiService.setAccessToken(accessToken); + final Pair, String?>? remote = await _apiService.assetApi.getAllAssetsWithETag(eTag: etag); if (remote == null) {