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'; import 'package:nc_photos/iterable_extension.dart'; abstract class AlbumProvider with EquatableMixin { const AlbumProvider(); factory AlbumProvider.fromJson(Map json) { final type = json["type"]; final content = json["content"]; switch (type) { case AlbumStaticProvider._type: return AlbumStaticProvider.fromJson(content.cast()); default: _log.shout("[fromJson] Unknown type: $type"); throw ArgumentError.value(type, "type"); } } Map toJson() { String getType() { if (this is AlbumStaticProvider) { return AlbumStaticProvider._type; } else { throw StateError("Unknwon subtype"); } } return { "type": getType(), "content": _toContentJson(), }; } @override toString({bool isDeep = false}); Map _toContentJson(); static final _log = Logger("entity.album.provider.AlbumProvider"); } class AlbumStaticProvider extends AlbumProvider { AlbumStaticProvider({ @required List items, }) : this.items = UnmodifiableListView(items); factory AlbumStaticProvider.fromJson(Map json) { return AlbumStaticProvider( items: (json["items"] as List) .map((e) => AlbumItem.fromJson(e.cast())) .toList(), ); } @override toString({bool isDeep = false}) { final itemsStr = isDeep ? items.toReadableString() : "List {length: ${items.length}}"; return "$runtimeType {" "items: $itemsStr, " "}"; } @override get props => [ items, ]; @override _toContentJson() { return { "items": items.map((e) => e.toJson()).toList(), }; } /// Immutable list of items. Modifying the list will result in an error final List items; static const _type = "static"; }