nc-photos/app/lib/entity/album/cover_provider.dart

151 lines
3.7 KiB
Dart
Raw Normal View History

2022-07-25 13:51:52 +08:00
import 'package:collection/collection.dart';
2021-06-26 19:51:13 +08:00
import 'package:equatable/equatable.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/entity/album.dart';
2021-07-05 15:54:01 +08:00
import 'package:nc_photos/entity/album/item.dart';
2021-06-26 19:51:13 +08:00
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/file_descriptor.dart';
2021-06-26 19:51:13 +08:00
import 'package:nc_photos/entity/file_util.dart' as file_util;
2022-12-16 23:01:04 +08:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_common/type.dart';
2022-12-08 23:39:13 +08:00
import 'package:to_string/to_string.dart';
part 'cover_provider.g.dart';
2021-06-26 19:51:13 +08:00
2022-12-16 23:01:04 +08:00
@npLog
2021-06-26 19:51:13 +08:00
abstract class AlbumCoverProvider with EquatableMixin {
const AlbumCoverProvider();
2021-08-07 01:11:00 +08:00
factory AlbumCoverProvider.fromJson(JsonObj json) {
2021-06-26 19:51:13 +08:00
final type = json["type"];
final content = json["content"];
switch (type) {
case AlbumAutoCoverProvider._type:
return AlbumAutoCoverProvider.fromJson(content.cast<String, dynamic>());
2021-08-14 04:18:35 +08:00
case AlbumManualCoverProvider._type:
return AlbumManualCoverProvider.fromJson(
content.cast<String, dynamic>());
2021-06-26 19:51:13 +08:00
default:
_log.shout("[fromJson] Unknown type: $type");
throw ArgumentError.value(type, "type");
}
}
2021-08-07 01:11:00 +08:00
JsonObj toJson() {
2021-06-26 19:51:13 +08:00
String getType() {
if (this is AlbumAutoCoverProvider) {
return AlbumAutoCoverProvider._type;
2021-08-14 04:18:35 +08:00
} else if (this is AlbumManualCoverProvider) {
return AlbumManualCoverProvider._type;
2021-06-26 19:51:13 +08:00
} else {
throw StateError("Unknwon subtype");
}
}
return {
"type": getType(),
"content": _toContentJson(),
};
}
@override
2022-12-08 23:39:13 +08:00
String toString();
2021-06-26 19:51:13 +08:00
FileDescriptor? getCover(Album album);
2021-06-26 19:51:13 +08:00
2021-08-07 01:11:00 +08:00
JsonObj _toContentJson();
2021-06-26 19:51:13 +08:00
2022-12-21 00:49:14 +08:00
static final _log = _$AlbumCoverProviderNpLog.log;
2021-06-26 19:51:13 +08:00
}
/// Cover selected automatically by us
2022-12-08 23:39:13 +08:00
@toString
2021-06-26 19:51:13 +08:00
class AlbumAutoCoverProvider extends AlbumCoverProvider {
const AlbumAutoCoverProvider({
2021-06-26 19:51:13 +08:00
this.coverFile,
});
2021-08-07 01:11:00 +08:00
factory AlbumAutoCoverProvider.fromJson(JsonObj json) {
2021-06-26 19:51:13 +08:00
return AlbumAutoCoverProvider(
coverFile: json["coverFile"] == null
? null
: FileDescriptor.fromJson(json["coverFile"].cast<String, dynamic>()),
2021-06-26 19:51:13 +08:00
);
}
static FileDescriptor? getCoverByItems(List<AlbumItem> items) {
return items
.whereType<AlbumFileItem>()
.map((e) => e.file)
.where(file_util.isSupportedFormat)
.sorted(compareFileDescriptorDateTimeDescending)
.firstOrNull;
}
2021-06-26 19:51:13 +08:00
@override
2022-12-08 23:39:13 +08:00
String toString() => _$toString();
2021-06-26 19:51:13 +08:00
2021-07-24 04:05:57 +08:00
@override
FileDescriptor? getCover(Album album) {
2023-04-29 12:07:46 +08:00
if (coverFile == null && album.provider is AlbumStaticProvider) {
// use the latest file as cover
return getCoverByItems(AlbumStaticProvider.of(album).items);
2021-06-26 19:51:13 +08:00
} else {
return coverFile;
}
}
@override
List<Object?> get props => [
2021-06-26 19:51:13 +08:00
coverFile,
];
@override
JsonObj _toContentJson() {
2021-06-26 19:51:13 +08:00
return {
if (coverFile != null) "coverFile": coverFile!.toFdJson(),
2021-06-26 19:51:13 +08:00
};
}
final FileDescriptor? coverFile;
2021-06-26 19:51:13 +08:00
static const _type = "auto";
}
2021-08-14 04:18:35 +08:00
/// Cover picked by user
2022-12-08 23:39:13 +08:00
@toString
2021-08-14 04:18:35 +08:00
class AlbumManualCoverProvider extends AlbumCoverProvider {
2023-04-18 00:15:29 +08:00
const AlbumManualCoverProvider({
2021-08-14 04:18:35 +08:00
required this.coverFile,
});
factory AlbumManualCoverProvider.fromJson(JsonObj json) {
return AlbumManualCoverProvider(
2023-04-18 00:15:29 +08:00
coverFile:
FileDescriptor.fromJson(json["coverFile"].cast<String, dynamic>()),
2021-08-14 04:18:35 +08:00
);
}
@override
2022-12-08 23:39:13 +08:00
String toString() => _$toString();
2021-08-14 04:18:35 +08:00
@override
2023-04-18 00:15:29 +08:00
FileDescriptor? getCover(Album album) => coverFile;
2021-08-14 04:18:35 +08:00
@override
2023-04-18 00:15:29 +08:00
List<Object?> get props => [
2021-08-14 04:18:35 +08:00
coverFile,
];
@override
2023-04-18 00:15:29 +08:00
JsonObj _toContentJson() {
2021-08-14 04:18:35 +08:00
return {
"coverFile": coverFile.toFdJson(),
2021-08-14 04:18:35 +08:00
};
}
2023-04-18 00:15:29 +08:00
final FileDescriptor coverFile;
2021-08-14 04:18:35 +08:00
static const _type = "manual";
}