From 64a4fa825fc52a5d9f2558e99f5d3f31357854a3 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Tue, 20 Aug 2024 01:19:50 +0800 Subject: [PATCH] Fix dir collection not populating correctly when offline --- .../collection_items_controller.dart | 52 +++++++++++++++---- app/lib/use_case/populate_album.dart | 13 +++-- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/app/lib/controller/collection_items_controller.dart b/app/lib/controller/collection_items_controller.dart index b91f1975..bfa11e00 100644 --- a/app/lib/controller/collection_items_controller.dart +++ b/app/lib/controller/collection_items_controller.dart @@ -298,13 +298,40 @@ class CollectionItemsController { Future _load() async { try { List? items; - await for (final r in ListCollectionItem(_c)(account, collection)) { - items = r; - _dataStreamController.add(CollectionItemStreamData( - items: r, - rawItems: r, - hasNext: true, - )); + ExceptionEvent? originalException; + try { + await for (final r in ListCollectionItem(_c)(account, collection)) { + items = r; + _dataStreamController.add(CollectionItemStreamData( + items: r, + rawItems: r, + hasNext: true, + )); + } + } catch (e, stackTrace) { + _log.severe("[_load] Failed while ListCollectionItem, try with local", + e, stackTrace); + originalException = ExceptionEvent(e, stackTrace); + } + if (originalException != null) { + // try again with local repos + try { + await for (final r + in ListCollectionItem(_c.withLocalRepo())(account, collection)) { + items = r; + _dataStreamController.add(CollectionItemStreamData( + items: r, + rawItems: r, + hasNext: true, + )); + } + } catch (e, stackTrace) { + _log.severe( + "[_load] Failed while ListCollectionItem with local repos", + e, + stackTrace); + originalException.throwMe(); + } } if (items != null) { _dataStreamController.add(CollectionItemStreamData( @@ -312,10 +339,13 @@ class CollectionItemsController { rawItems: items, hasNext: false, )); - final newCollection = - await UpdateCollectionPostLoad(_c)(account, collection, items); - if (newCollection != null) { - onCollectionUpdated(newCollection); + if (originalException == null) { + // only update if the data is queried from remote + final newCollection = + await UpdateCollectionPostLoad(_c)(account, collection, items); + if (newCollection != null) { + onCollectionUpdated(newCollection); + } } } } catch (e, stackTrace) { diff --git a/app/lib/use_case/populate_album.dart b/app/lib/use_case/populate_album.dart index 26ef6d1e..23caec3c 100644 --- a/app/lib/use_case/populate_album.dart +++ b/app/lib/use_case/populate_album.dart @@ -1,5 +1,4 @@ import 'package:clock/clock.dart'; -import 'package:kiwi/kiwi.dart'; import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/debug_util.dart'; @@ -41,14 +40,17 @@ class PopulateAlbum { assert(album.provider is AlbumDirProvider); final provider = album.provider as AlbumDirProvider; final products = []; + var hasGood = false; + ExceptionEvent? firstException; for (final d in provider.dirs) { final stream = ScanDir(_c.fileRepo)(account, d); await for (final result in stream) { if (result is ExceptionEvent) { - _log.shout( + _log.severe( "[_populateDirAlbum] Failed while scanning dir: ${logFilename(d.path)}", result.error, result.stackTrace); + firstException = result; continue; } products.addAll((result as List).cast().map((f) => AlbumFileItem( @@ -57,8 +59,12 @@ class PopulateAlbum { file: f, ownerId: f.ownerId ?? account.userId, ))); + hasGood = true; } } + if (!hasGood && firstException != null) { + firstException.throwMe(); + } return products; } @@ -67,8 +73,7 @@ class PopulateAlbum { assert(album.provider is AlbumTagProvider); final provider = album.provider as AlbumTagProvider; final products = []; - final c = KiwiContainer().resolve(); - final files = await ListTaggedFile(c)(account, provider.tags); + final files = await ListTaggedFile(_c)(account, provider.tags); products.addAll(files.map((f) => AlbumFileItem( addedBy: account.userId, addedAt: clock.now(),