nc-photos/app/lib/entity/collection/adapter/memory.dart

57 lines
2.1 KiB
Dart

import 'package:nc_photos/account.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/collection.dart';
import 'package:nc_photos/entity/collection/adapter.dart';
import 'package:nc_photos/entity/collection/adapter/adapter_mixin.dart';
import 'package:nc_photos/entity/collection/content_provider/memory.dart';
import 'package:nc_photos/entity/collection_item.dart';
import 'package:nc_photos/entity/collection_item/basic_item.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/entity/pref.dart';
class CollectionMemoryAdapter
with
CollectionAdapterReadOnlyTag,
CollectionAdapterUnremovableTag,
CollectionAdapterUnshareableTag
implements CollectionAdapter {
CollectionMemoryAdapter(this._c, this.account, this.collection)
: _provider = collection.contentProvider as CollectionMemoryProvider;
@override
Stream<List<CollectionItem>> listItem() async* {
final date = DateTime(_provider.year, _provider.month, _provider.day);
final dayRange = _c.pref.getMemoriesRangeOr();
final from = date.subtract(Duration(days: dayRange));
final to = date.add(Duration(days: dayRange + 1));
final files = await FileSqliteDbDataSource(_c).listByDate(
account, from.millisecondsSinceEpoch, to.millisecondsSinceEpoch);
yield files
.where((f) => file_util.isSupportedFormat(f))
.map((f) => BasicCollectionFileItem(f))
.toList();
}
@override
Future<CollectionItem> adaptToNewItem(CollectionItem original) async {
if (original is CollectionFileItem) {
return BasicCollectionFileItem(original.file);
} else {
throw UnsupportedError("Unsupported type: ${original.runtimeType}");
}
}
@override
bool isItemDeletable(CollectionItem item) => true;
@override
bool isPermitted(CollectionCapability capability) =>
_provider.capabilities.contains(capability);
final DiContainer _c;
final Account account;
final Collection collection;
final CollectionMemoryProvider _provider;
}