Regression: album cover is gone after unsetting one for a dynamic album

This commit is contained in:
Ming Ming 2023-04-22 22:32:34 +08:00
parent 02107c7150
commit c62af9941b
9 changed files with 47 additions and 15 deletions

View file

@ -76,6 +76,9 @@ class CollectionItemsController {
return _dataStreamController.stream;
}
/// Peek the stream and return the current value
CollectionItemStreamData peekStream() => _dataStreamController.stream.value;
/// Add list of [files] to [collection]
Future<void> addFiles(List<FileDescriptor> files) async {
final isInited = _isDataStreamInited;

View file

@ -1,5 +1,6 @@
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:copy_with/copy_with.dart';
import 'package:logging/logging.dart';
import 'package:mutex/mutex.dart';
@ -163,6 +164,9 @@ class CollectionsController {
}) async {
try {
final c = await _mutex.protect(() async {
final found = _dataStreamController.value.data.firstWhereOrNull(
(ev) => ev.collection.compareIdentity(collection));
final item = found?.controller.peekStream();
return await EditCollection(_c)(
account,
collection,
@ -170,6 +174,7 @@ class CollectionsController {
items: items,
itemSort: itemSort,
cover: cover,
knownItems: (item?.items.isEmpty ?? true) ? null : item!.items,
);
});
_updateCollection(c, items);

View file

@ -77,26 +77,25 @@ class AlbumAutoCoverProvider extends AlbumCoverProvider {
);
}
@override
String toString() => _$toString();
@override
FileDescriptor? getCover(Album album) {
if (coverFile == null) {
try {
// use the latest file as cover
return AlbumStaticProvider.of(album)
.items
static FileDescriptor? getCoverByItems(List<AlbumItem> items) {
return items
.whereType<AlbumFileItem>()
.map((e) => e.file)
.where((element) =>
file_util.isSupportedFormat(element) &&
(element.hasPreview ?? false))
.sorted(compareFileDateTimeDescending)
.first;
} catch (_) {
return null;
.firstOrNull;
}
@override
String toString() => _$toString();
@override
FileDescriptor? getCover(Album album) {
if (coverFile == null) {
// use the latest file as cover
return getCoverByItems(AlbumStaticProvider.of(album).items);
} else {
return coverFile;
}

View file

@ -57,6 +57,7 @@ abstract class CollectionAdapter {
List<CollectionItem>? items,
CollectionItemSort? itemSort,
OrNull<FileDescriptor>? cover,
List<CollectionItem>? knownItems,
});
/// Remove [items] from this collection and return the removed count

View file

@ -79,6 +79,7 @@ class CollectionAlbumAdapter implements CollectionAdapter {
List<CollectionItem>? items,
CollectionItemSort? itemSort,
OrNull<FileDescriptor>? cover,
List<CollectionItem>? knownItems,
}) async {
assert(name != null || items != null || itemSort != null || cover != null);
final newItems = items?.run((items) => items
@ -106,6 +107,10 @@ class CollectionAlbumAdapter implements CollectionAdapter {
items: newItems,
itemSort: itemSort,
cover: cover,
knownItems: knownItems
?.whereType<AlbumAdaptedCollectionItem>()
.map((e) => e.albumItem)
.toList(),
);
return collection.copyWith(
name: name,

View file

@ -77,6 +77,7 @@ class CollectionNcAlbumAdapter implements CollectionAdapter {
List<CollectionItem>? items,
CollectionItemSort? itemSort,
OrNull<FileDescriptor>? cover,
List<CollectionItem>? knownItems,
}) async {
assert(name != null);
if (items != null || itemSort != null || cover != null) {

View file

@ -24,6 +24,7 @@ mixin CollectionReadOnlyAdapter implements CollectionAdapter {
List<CollectionItem>? items,
CollectionItemSort? itemSort,
OrNull<FileDescriptor>? cover,
List<CollectionItem>? knownItems,
}) {
throw UnsupportedError("Operation not supported");
}

View file

@ -26,6 +26,7 @@ class EditAlbum {
List<AlbumItem>? items,
CollectionItemSort? itemSort,
OrNull<FileDescriptor>? cover,
List<AlbumItem>? knownItems,
}) async {
_log.info(
"[call] Edit album ${album.name}, name: $name, items: $items, itemSort: $itemSort, cover: $cover");
@ -49,8 +50,9 @@ class EditAlbum {
}
if (cover != null) {
if (cover.obj == null) {
final coverFile = _getCoverFile(knownItems);
newAlbum = newAlbum.copyWith(
coverProvider: const AlbumAutoCoverProvider(),
coverProvider: AlbumAutoCoverProvider(coverFile: coverFile),
);
} else {
newAlbum = newAlbum.copyWith(
@ -65,5 +67,13 @@ class EditAlbum {
return newAlbum;
}
FileDescriptor? _getCoverFile(List<AlbumItem>? items) {
if (items?.isEmpty ?? true) {
return null;
} else {
return AlbumAutoCoverProvider.getCoverByItems(items!);
}
}
final DiContainer _c;
}

View file

@ -19,6 +19,11 @@ class EditCollection {
/// - Sort [items] (set [items] and/or [itemSort])
/// - Set album [cover]
///
/// Optionally you may provide a list of known collection items. If
/// [knownItems] is not null, it may be used as a hint for the implementors
/// when updating the underlying collection (e.g., setting the latest item as
/// cover image)
///
/// \* To add files to a collection, use [AddFileToCollection] instead
Future<Collection> call(
Account account,
@ -27,12 +32,14 @@ class EditCollection {
List<CollectionItem>? items,
CollectionItemSort? itemSort,
OrNull<FileDescriptor>? cover,
List<CollectionItem>? knownItems,
}) =>
CollectionAdapter.of(_c, account, collection).edit(
name: name,
items: items,
itemSort: itemSort,
cover: cover,
knownItems: knownItems,
);
final DiContainer _c;