do not apply sort/group when deleting an entry

This commit is contained in:
Thibault Deckers 2020-03-10 12:36:51 +09:00
parent d37941c66f
commit 73d97f821b

View file

@ -26,10 +26,10 @@ class CollectionLens with ChangeNotifier {
}) : this.filters = filters ?? [], }) : this.filters = filters ?? [],
this.groupFactor = groupFactor ?? GroupFactor.month, this.groupFactor = groupFactor ?? GroupFactor.month,
this.sortFactor = sortFactor ?? SortFactor.date { this.sortFactor = sortFactor ?? SortFactor.date {
_subscriptions.add(source.eventBus.on<EntryAddedEvent>().listen((e) => onSourceChanged())); _subscriptions.add(source.eventBus.on<EntryAddedEvent>().listen((e) => onEntryAdded()));
_subscriptions.add(source.eventBus.on<EntryRemovedEvent>().listen((e) => onSourceChanged())); _subscriptions.add(source.eventBus.on<EntryRemovedEvent>().listen((e) => onEntryRemoved(e.entry)));
_subscriptions.add(source.eventBus.on<MetadataChangedEvent>().listen((e) => onMetadataChanged())); _subscriptions.add(source.eventBus.on<MetadataChangedEvent>().listen((e) => onMetadataChanged()));
onSourceChanged(); onEntryAdded();
} }
factory CollectionLens.empty() { factory CollectionLens.empty() {
@ -68,27 +68,46 @@ class CollectionLens with ChangeNotifier {
void sort(SortFactor sortFactor) { void sort(SortFactor sortFactor) {
this.sortFactor = sortFactor; this.sortFactor = sortFactor;
updateSections(); _applySort();
_applyGroup();
} }
void group(GroupFactor groupFactor) { void group(GroupFactor groupFactor) {
this.groupFactor = groupFactor; this.groupFactor = groupFactor;
updateSections(); _applyGroup();
} }
void updateSections() { void _applyFilters() {
_applySort(); final rawEntries = source.entries;
_filteredEntries = List.of(filters.isEmpty ? rawEntries : rawEntries.where((entry) => filters.fold(true, (prev, filter) => prev && filter.filter(entry))));
}
void _applySort() {
switch (sortFactor) {
case SortFactor.date:
_filteredEntries.sort((a, b) => b.bestDate.compareTo(a.bestDate));
break;
case SortFactor.size:
_filteredEntries.sort((a, b) => b.sizeBytes.compareTo(a.sizeBytes));
break;
case SortFactor.name:
_filteredEntries.sort((a, b) => compareAsciiUpperCase(a.title, b.title));
break;
}
}
void _applyGroup() {
switch (sortFactor) { switch (sortFactor) {
case SortFactor.date: case SortFactor.date:
switch (groupFactor) { switch (groupFactor) {
case GroupFactor.album: case GroupFactor.album:
sections = Map.unmodifiable(groupBy(_filteredEntries, (entry) => entry.directory)); sections = Map.unmodifiable(groupBy<ImageEntry, String>(_filteredEntries, (entry) => entry.directory));
break; break;
case GroupFactor.month: case GroupFactor.month:
sections = Map.unmodifiable(groupBy(_filteredEntries, (entry) => entry.monthTaken)); sections = Map.unmodifiable(groupBy<ImageEntry, DateTime>(_filteredEntries, (entry) => entry.monthTaken));
break; break;
case GroupFactor.day: case GroupFactor.day:
sections = Map.unmodifiable(groupBy(_filteredEntries, (entry) => entry.dayTaken)); sections = Map.unmodifiable(groupBy<ImageEntry, DateTime>(_filteredEntries, (entry) => entry.dayTaken));
break; break;
} }
break; break;
@ -111,46 +130,24 @@ class CollectionLens with ChangeNotifier {
notifyListeners(); notifyListeners();
} }
void _applySort() { void onEntryAdded() {
switch (sortFactor) {
case SortFactor.date:
_filteredEntries.sort((a, b) => b.bestDate.compareTo(a.bestDate));
break;
case SortFactor.size:
_filteredEntries.sort((a, b) => b.sizeBytes.compareTo(a.sizeBytes));
break;
case SortFactor.name:
_filteredEntries.sort((a, b) => compareAsciiUpperCase(a.title, b.title));
break;
}
}
// void add(ImageEntry entry) => _rawEntries.add(entry);
//
// Future<bool> delete(ImageEntry entry) async {
// final success = await ImageFileService.delete(entry);
// if (success) {
// _rawEntries.remove(entry);
// updateSections();
// }
// return success;
// }
void onSourceChanged() {
_applyFilters(); _applyFilters();
updateSections(); _applySort();
_applyGroup();
} }
void _applyFilters() { void onEntryRemoved(ImageEntry entry) {
final rawEntries = source.entries; // do not apply sort/group as section order change would surprise the user while browsing
_filteredEntries = List.of(filters.isEmpty ? rawEntries : rawEntries.where((entry) => filters.fold(true, (prev, filter) => prev && filter.filter(entry)))); _filteredEntries.remove(entry);
updateSections(); sections.forEach((key, entries) => entries.remove(entry));
notifyListeners();
} }
void onMetadataChanged() { void onMetadataChanged() {
_applyFilters(); _applyFilters();
// metadata dates impact sorting and grouping // metadata dates impact sorting and grouping
updateSections(); _applySort();
_applyGroup();
} }
} }