model: fixed collection group/sorting updates

This commit is contained in:
Thibault Deckers 2019-08-31 18:24:44 +09:00
parent cc869df575
commit 85f122f5a2
2 changed files with 39 additions and 30 deletions

View file

@ -64,12 +64,10 @@ class _HomePageState extends State<HomePage> {
(entryMap) => localMediaCollection.add(ImageEntry.fromMap(entryMap)), (entryMap) => localMediaCollection.add(ImageEntry.fromMap(entryMap)),
onDone: () async { onDone: () async {
debugPrint('mediastore stream done'); debugPrint('mediastore stream done');
localMediaCollection.updateSections();
localMediaCollection.updateAlbums(); localMediaCollection.updateAlbums();
await localMediaCollection.loadCatalogMetadata(); await localMediaCollection.loadCatalogMetadata();
setState(() {});
await localMediaCollection.catalogEntries(); await localMediaCollection.catalogEntries();
localMediaCollection.updateTags();
setState(() {});
await localMediaCollection.loadAddresses(); await localMediaCollection.loadAddresses();
await localMediaCollection.locateEntries(); await localMediaCollection.locateEntries();
}, },

View file

@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
class ImageCollection with ChangeNotifier { class ImageCollection with ChangeNotifier {
final List<ImageEntry> _rawEntries; final List<ImageEntry> _rawEntries;
Map<dynamic, List<ImageEntry>> sections = Map();
GroupFactor groupFactor = GroupFactor.date; GroupFactor groupFactor = GroupFactor.date;
SortFactor sortFactor = SortFactor.date; SortFactor sortFactor = SortFactor.date;
Set<String> albums = Set(), tags = Set(); Set<String> albums = Set(), tags = Set();
@ -15,7 +16,9 @@ class ImageCollection with ChangeNotifier {
@required List<ImageEntry> entries, @required List<ImageEntry> entries,
@required this.groupFactor, @required this.groupFactor,
@required this.sortFactor, @required this.sortFactor,
}) : _rawEntries = entries; }) : _rawEntries = entries {
updateSections();
}
int get imageCount => _rawEntries.where((entry) => !entry.isVideo).length; int get imageCount => _rawEntries.where((entry) => !entry.isVideo).length;
@ -25,34 +28,39 @@ class ImageCollection with ChangeNotifier {
int get tagCount => tags.length; int get tagCount => tags.length;
Map<dynamic, List<ImageEntry>> get sections { List<ImageEntry> get sortedEntries => List.unmodifiable(sections.entries.expand((e) => e.value));
switch (sortFactor) {
case SortFactor.date:
switch (groupFactor) {
case GroupFactor.album:
return groupBy(_rawEntries, (entry) => entry.bucketDisplayName);
case GroupFactor.date:
return groupBy(_rawEntries, (entry) => entry.monthTaken);
}
break;
case SortFactor.size:
return Map.fromEntries([MapEntry('All', _rawEntries)]);
}
return Map();
}
List<ImageEntry> get sortedEntries { sort(SortFactor sortFactor) {
return List.unmodifiable(sections.entries.expand((e) => e.value)); this.sortFactor = sortFactor;
updateSections();
} }
group(GroupFactor groupFactor) { group(GroupFactor groupFactor) {
this.groupFactor = groupFactor; this.groupFactor = groupFactor;
updateSections();
}
updateSections() {
_applySort();
switch (sortFactor) {
case SortFactor.date:
switch (groupFactor) {
case GroupFactor.album:
sections = groupBy(_rawEntries, (entry) => entry.bucketDisplayName);
break;
case GroupFactor.date:
sections = groupBy(_rawEntries, (entry) => entry.monthTaken);
break;
}
break;
case SortFactor.size:
sections = Map.fromEntries([MapEntry('All', _rawEntries)]);
break;
}
notifyListeners(); notifyListeners();
} }
sort(SortFactor sortFactor) { _applySort() {
this.sortFactor = sortFactor;
switch (sortFactor) { switch (sortFactor) {
case SortFactor.date: case SortFactor.date:
_rawEntries.sort((a, b) => b.bestDate.compareTo(a.bestDate)); _rawEntries.sort((a, b) => b.bestDate.compareTo(a.bestDate));
@ -61,8 +69,6 @@ class ImageCollection with ChangeNotifier {
_rawEntries.sort((a, b) => b.sizeBytes.compareTo(a.sizeBytes)); _rawEntries.sort((a, b) => b.sizeBytes.compareTo(a.sizeBytes));
break; break;
} }
notifyListeners();
} }
add(ImageEntry entry) => _rawEntries.add(entry); add(ImageEntry entry) => _rawEntries.add(entry);
@ -71,7 +77,7 @@ class ImageCollection with ChangeNotifier {
final success = await ImageFileService.delete(entry); final success = await ImageFileService.delete(entry);
if (success) { if (success) {
_rawEntries.remove(entry); _rawEntries.remove(entry);
notifyListeners(); updateSections();
} }
return success; return success;
} }
@ -80,6 +86,12 @@ class ImageCollection with ChangeNotifier {
updateTags() => tags = _rawEntries.expand((entry) => entry.xmpSubjects).toSet(); updateTags() => tags = _rawEntries.expand((entry) => entry.xmpSubjects).toSet();
onMetadataChanged() {
// metadata dates impact sorting and grouping
updateSections();
updateTags();
}
loadCatalogMetadata() async { loadCatalogMetadata() async {
debugPrint('$runtimeType loadCatalogMetadata start'); debugPrint('$runtimeType loadCatalogMetadata start');
final start = DateTime.now(); final start = DateTime.now();
@ -90,6 +102,7 @@ class ImageCollection with ChangeNotifier {
entry.catalogMetadata = saved.firstWhere((metadata) => metadata.contentId == contentId, orElse: () => null); entry.catalogMetadata = saved.firstWhere((metadata) => metadata.contentId == contentId, orElse: () => null);
} }
}); });
onMetadataChanged();
debugPrint('$runtimeType loadCatalogMetadata complete in ${DateTime.now().difference(start).inSeconds}s with ${saved.length} saved entries'); debugPrint('$runtimeType loadCatalogMetadata complete in ${DateTime.now().difference(start).inSeconds}s with ${saved.length} saved entries');
} }
@ -116,10 +129,8 @@ class ImageCollection with ChangeNotifier {
newMetadata.add(entry.catalogMetadata); newMetadata.add(entry.catalogMetadata);
}); });
metadataDb.saveMetadata(List.unmodifiable(newMetadata)); metadataDb.saveMetadata(List.unmodifiable(newMetadata));
onMetadataChanged();
debugPrint('$runtimeType catalogEntries complete in ${DateTime.now().difference(start).inSeconds}s with ${newMetadata.length} new entries'); debugPrint('$runtimeType catalogEntries complete in ${DateTime.now().difference(start).inSeconds}s with ${newMetadata.length} new entries');
// notify because metadata dates might change groups and order
notifyListeners();
} }
locateEntries() async { locateEntries() async {