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';
|
2022-10-15 16:29:18 +02:00
|
|
|
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';
|
2023-02-23 15:49:17 +01:00
|
|
|
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>());
|
2022-10-15 16:29:18 +02:00
|
|
|
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
|
|
|
|
2022-10-15 16:29:18 +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 {
|
|
|
|
AlbumAutoCoverProvider({
|
|
|
|
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
|
|
|
|
: File.fromJson(json["coverFile"].cast<String, dynamic>()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
|
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
|
|
|
|
get props => [
|
|
|
|
coverFile,
|
|
|
|
];
|
|
|
|
|
|
|
|
@override
|
|
|
|
_toContentJson() {
|
|
|
|
return {
|
2021-07-23 22:05:57 +02:00
|
|
|
if (coverFile != null) "coverFile": coverFile!.toJson(),
|
2021-06-26 13:51:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
final File? 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 {
|
|
|
|
AlbumManualCoverProvider({
|
|
|
|
required this.coverFile,
|
|
|
|
});
|
|
|
|
|
|
|
|
factory AlbumManualCoverProvider.fromJson(JsonObj json) {
|
|
|
|
return AlbumManualCoverProvider(
|
|
|
|
coverFile: File.fromJson(json["coverFile"].cast<String, dynamic>()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-12-08 16:39:13 +01:00
|
|
|
String toString() => _$toString();
|
2021-08-13 22:18:35 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
getCover(Album album) => coverFile;
|
|
|
|
|
|
|
|
@override
|
|
|
|
get props => [
|
|
|
|
coverFile,
|
|
|
|
];
|
|
|
|
|
|
|
|
@override
|
|
|
|
_toContentJson() {
|
|
|
|
return {
|
|
|
|
"coverFile": coverFile.toJson(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
final File coverFile;
|
|
|
|
|
|
|
|
static const _type = "manual";
|
|
|
|
}
|
2022-10-15 16:29:18 +02:00
|
|
|
|
|
|
|
/// Cover selected when building a Memory album
|
2022-12-08 16:39:13 +01:00
|
|
|
@toString
|
2022-10-15 16:29:18 +02:00
|
|
|
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();
|
2022-10-15 16:29:18 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
getCover(Album album) => coverFile;
|
|
|
|
|
|
|
|
@override
|
|
|
|
get props => [
|
|
|
|
coverFile,
|
|
|
|
];
|
|
|
|
|
|
|
|
@override
|
|
|
|
_toContentJson() => {
|
|
|
|
"coverFile": coverFile.toJson(),
|
|
|
|
};
|
|
|
|
|
|
|
|
final FileDescriptor coverFile;
|
|
|
|
|
|
|
|
static const _type = "memory";
|
|
|
|
}
|