Fix dir collection not populating correctly when offline

This commit is contained in:
Ming Ming 2024-08-20 01:19:50 +08:00
parent cb62b76d87
commit 64a4fa825f
2 changed files with 50 additions and 15 deletions

View file

@ -298,13 +298,40 @@ class CollectionItemsController {
Future<void> _load() async {
try {
List<CollectionItem>? 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) {

View file

@ -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 = <AlbumItem>[];
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<File>().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 = <AlbumItem>[];
final c = KiwiContainer().resolve<DiContainer>();
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(),