nc-photos/lib/entity/album/provider.dart

223 lines
5.3 KiB
Dart
Raw Normal View History

2021-06-24 18:26:56 +02:00
import 'dart:collection';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.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';
import 'package:nc_photos/entity/file.dart';
2021-06-24 18:26:56 +02:00
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/or_null.dart';
2021-08-06 19:11:00 +02:00
import 'package:nc_photos/type.dart';
2021-06-24 18:26:56 +02:00
abstract class AlbumProvider with EquatableMixin {
const AlbumProvider();
2021-08-06 19:11:00 +02:00
factory AlbumProvider.fromJson(JsonObj json) {
2021-06-24 18:26:56 +02:00
final type = json["type"];
final content = json["content"];
switch (type) {
case AlbumStaticProvider._type:
return AlbumStaticProvider.fromJson(content.cast<String, dynamic>());
case AlbumDirProvider._type:
return AlbumDirProvider.fromJson(content.cast<String, dynamic>());
2021-06-24 18:26:56 +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-24 18:26:56 +02:00
String getType() {
if (this is AlbumStaticProvider) {
return AlbumStaticProvider._type;
} else if (this is AlbumDirProvider) {
return AlbumDirProvider._type;
2021-06-24 18:26:56 +02:00
} else {
throw StateError("Unknwon subtype");
}
}
return {
"type": getType(),
2021-06-27 17:35:40 +02:00
"content": toContentJson(),
2021-06-24 18:26:56 +02:00
};
}
2021-06-27 17:35:40 +02:00
@protected
2021-08-06 19:11:00 +02:00
JsonObj toContentJson();
2021-06-27 17:35:40 +02:00
2021-06-24 18:26:56 +02:00
@override
toString({bool isDeep = false});
2021-06-27 17:35:40 +02:00
/// Return the date time associated with the latest item, or null
2021-07-23 22:05:57 +02:00
DateTime? get latestItemTime;
2021-06-27 17:35:40 +02:00
AlbumProvider copyWith();
2021-06-24 18:26:56 +02:00
static final _log = Logger("entity.album.provider.AlbumProvider");
}
abstract class AlbumProviderBase extends AlbumProvider {
AlbumProviderBase({
DateTime? latestItemTime,
}) : latestItemTime = latestItemTime?.toUtc();
@override
toString({bool isDeep = false}) {
return "$runtimeType {"
"latestItemTime: $latestItemTime, "
"}";
}
@override
toContentJson() {
return {
if (latestItemTime != null)
"latestItemTime": latestItemTime!.toUtc().toIso8601String(),
};
}
@override
AlbumProviderBase copyWith({
OrNull<DateTime>? latestItemTime,
});
@override
get props => [
latestItemTime,
];
@override
final DateTime? latestItemTime;
}
class AlbumStaticProvider extends AlbumProviderBase {
2021-06-24 18:26:56 +02:00
AlbumStaticProvider({
DateTime? latestItemTime,
2021-07-23 22:05:57 +02:00
required List<AlbumItem> items,
}) : items = UnmodifiableListView(items),
super(
latestItemTime: latestItemTime,
);
2021-06-24 18:26:56 +02:00
2021-08-06 19:11:00 +02:00
factory AlbumStaticProvider.fromJson(JsonObj json) {
2021-06-24 18:26:56 +02:00
return AlbumStaticProvider(
latestItemTime: json["latestItemTime"] == null
? null
: DateTime.parse(json["latestItemTime"]),
2021-06-24 18:26:56 +02:00
items: (json["items"] as List)
.map((e) => AlbumItem.fromJson(e.cast<String, dynamic>()))
.toList(),
);
}
factory AlbumStaticProvider.of(Album parent) =>
(parent.provider as AlbumStaticProvider);
2021-06-24 18:26:56 +02:00
@override
toString({bool isDeep = false}) {
final itemsStr =
isDeep ? items.toReadableString() : "List {length: ${items.length}}";
return "$runtimeType {"
"super: ${super.toString(isDeep: isDeep)}, "
2021-06-24 18:26:56 +02:00
"items: $itemsStr, "
"}";
}
2021-06-27 17:35:40 +02:00
@override
toContentJson() {
return {
...super.toContentJson(),
2021-06-27 17:35:40 +02:00
"items": items.map((e) => e.toJson()).toList(),
};
}
@override
2021-07-23 22:05:57 +02:00
AlbumStaticProvider copyWith({
OrNull<DateTime>? latestItemTime,
2021-07-23 22:05:57 +02:00
List<AlbumItem>? items,
}) {
2021-06-27 17:35:40 +02:00
return AlbumStaticProvider(
latestItemTime:
latestItemTime == null ? this.latestItemTime : latestItemTime.obj,
2021-06-27 17:35:40 +02:00
items: items ?? this.items,
);
}
2021-06-24 18:26:56 +02:00
@override
get props => [
...super.props,
2021-06-24 18:26:56 +02:00
items,
];
2021-06-27 17:35:40 +02:00
/// Immutable list of items. Modifying the list will result in an error
final List<AlbumItem> items;
static const _type = "static";
}
abstract class AlbumDynamicProvider extends AlbumProviderBase {
AlbumDynamicProvider({
2021-07-23 22:05:57 +02:00
DateTime? latestItemTime,
}) : super(latestItemTime: latestItemTime);
2021-06-24 18:26:56 +02:00
}
2021-06-27 17:35:40 +02:00
class AlbumDirProvider extends AlbumDynamicProvider {
AlbumDirProvider({
2021-07-23 22:05:57 +02:00
required this.dirs,
DateTime? latestItemTime,
}) : super(
latestItemTime: latestItemTime,
);
2021-08-06 19:11:00 +02:00
factory AlbumDirProvider.fromJson(JsonObj json) {
return AlbumDirProvider(
2021-06-27 17:35:40 +02:00
latestItemTime: json["latestItemTime"] == null
? null
: DateTime.parse(json["latestItemTime"]),
dirs: (json["dirs"] as List)
.map((e) => File.fromJson(e.cast<String, dynamic>()))
.toList(),
);
}
@override
toString({bool isDeep = false}) {
return "$runtimeType {"
2021-06-27 17:35:40 +02:00
"super: ${super.toString(isDeep: isDeep)}, "
"dirs: ${dirs.map((e) => e.path).toReadableString()}, "
"}";
}
@override
2021-06-27 17:35:40 +02:00
toContentJson() {
return {
2021-06-27 17:35:40 +02:00
...super.toContentJson(),
"dirs": dirs.map((e) => e.toJson()).toList(),
};
}
2021-06-27 17:35:40 +02:00
@override
AlbumDirProvider copyWith({
OrNull<DateTime>? latestItemTime,
List<File>? dirs,
2021-06-27 17:35:40 +02:00
}) {
return AlbumDirProvider(
latestItemTime:
latestItemTime == null ? this.latestItemTime : latestItemTime.obj,
dirs: dirs ?? this.dirs,
2021-06-27 17:35:40 +02:00
);
}
@override
get props => [
...super.props,
dirs,
];
final List<File> dirs;
static const _type = "dir";
}