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

193 lines
4.6 KiB
Dart
Raw Normal View History

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