From c05b646dddc0bbc23bb36dbf79f14610d8ae9a83 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Wed, 3 Feb 2021 14:44:55 +0900 Subject: [PATCH] media store monitoring: fixed external removal --- .../thibault/aves/utils/PermissionManager.kt | 2 -- lib/model/source/collection_lens.dart | 2 +- lib/model/source/collection_source.dart | 3 ++- lib/model/source/media_store_source.dart | 23 +++++++++++-------- .../collection/entry_set_action_delegate.dart | 2 +- .../common/chip_action_delegate.dart | 2 +- lib/widgets/viewer/entry_action_delegate.dart | 2 +- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/utils/PermissionManager.kt b/android/app/src/main/kotlin/deckers/thibault/aves/utils/PermissionManager.kt index 3e07b7c70..7ecf9a3b5 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/utils/PermissionManager.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/utils/PermissionManager.kt @@ -95,7 +95,6 @@ object PermissionManager { } } } - Log.d(LOG_TAG, "getInaccessibleDirectories dirPaths=$dirPaths -> inaccessibleDirs=$inaccessibleDirs") return inaccessibleDirs } @@ -124,7 +123,6 @@ object PermissionManager { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { accessibleDirs.add(StorageUtils.getPrimaryVolumePath(context)) } - Log.d(LOG_TAG, "getAccessibleDirs accessibleDirs=$accessibleDirs") return accessibleDirs } diff --git a/lib/model/source/collection_lens.dart b/lib/model/source/collection_lens.dart index 6d4f5bc74..1c7d77933 100644 --- a/lib/model/source/collection_lens.dart +++ b/lib/model/source/collection_lens.dart @@ -46,7 +46,7 @@ class CollectionLens with ChangeNotifier, CollectionActivityMixin, CollectionSel _subscriptions.add(source.eventBus.on().listen((e) => _refresh())); _subscriptions.add(source.eventBus.on().listen((e) => _refresh())); _subscriptions.add(source.eventBus.on().listen((e) { - if (filters.any((filter) => filter is LocationFilter)) { + if (this.filters.any((filter) => filter is LocationFilter)) { _refresh(); } })); diff --git a/lib/model/source/collection_source.dart b/lib/model/source/collection_source.dart index 137ca9786..3a9509455 100644 --- a/lib/model/source/collection_source.dart +++ b/lib/model/source/collection_source.dart @@ -71,7 +71,8 @@ abstract class CollectionSource with SourceBase, AlbumMixin, LocationMixin, TagM eventBus.fire(EntryAddedEvent()); } - void removeEntries(List entries) { + void removeEntries(Set entries) { + if (entries.isEmpty) return; entries.forEach((entry) => entry.removeFromFavourites()); _rawEntries.removeWhere(entries.contains); cleanEmptyAlbums(entries.map((entry) => entry.directory).toSet()); diff --git a/lib/model/source/media_store_source.dart b/lib/model/source/media_store_source.dart index 44f24eae2..8c17c80c6 100644 --- a/lib/model/source/media_store_source.dart +++ b/lib/model/source/media_store_source.dart @@ -123,8 +123,9 @@ class MediaStoreSource extends CollectionSource { // clean up obsolete entries final obsoleteContentIds = (await ImageFileService.getObsoleteEntries(uriByContentId.keys.toList())).toSet(); - uriByContentId.removeWhere((contentId, _) => obsoleteContentIds.contains(contentId)); - metadataDb.removeIds(obsoleteContentIds, updateFavourites: true); + obsoleteContentIds.forEach(uriByContentId.remove); + final obsoleteEntries = rawEntries.where((e) => obsoleteContentIds.contains(e.contentId)).toSet(); + removeEntries(obsoleteEntries); // fetch new entries final newEntries = []; @@ -132,14 +133,16 @@ class MediaStoreSource extends CollectionSource { final contentId = kv.key; final uri = kv.value; final sourceEntry = await ImageFileService.getEntry(uri, null); - final existingEntry = rawEntries.firstWhere((entry) => entry.contentId == contentId, orElse: () => null); - if (existingEntry == null || sourceEntry.dateModifiedSecs > existingEntry.dateModifiedSecs) { - final volume = androidFileUtils.getStorageVolume(sourceEntry.path); - if (volume != null) { - newEntries.add(sourceEntry); - } else { - debugPrint('$runtimeType refreshUris entry=$sourceEntry is not located on a known storage volume. Will retry soon...'); - tempUris.add(uri); + if (sourceEntry != null) { + final existingEntry = rawEntries.firstWhere((entry) => entry.contentId == contentId, orElse: () => null); + if (existingEntry == null || sourceEntry.dateModifiedSecs > existingEntry.dateModifiedSecs) { + final volume = androidFileUtils.getStorageVolume(sourceEntry.path); + if (volume != null) { + newEntries.add(sourceEntry); + } else { + debugPrint('$runtimeType refreshUris entry=$sourceEntry is not located on a known storage volume. Will retry soon...'); + tempUris.add(uri); + } } } } diff --git a/lib/widgets/collection/entry_set_action_delegate.dart b/lib/widgets/collection/entry_set_action_delegate.dart index 6c3276361..b51111f77 100644 --- a/lib/widgets/collection/entry_set_action_delegate.dart +++ b/lib/widgets/collection/entry_set_action_delegate.dart @@ -145,7 +145,7 @@ class EntrySetActionDelegate with FeedbackMixin, PermissionAwareMixin, SizeAware showFeedback(context, 'Failed to delete ${Intl.plural(count, one: '$count item', other: '$count items')}'); } if (deletedCount > 0) { - source.removeEntries(selection.where((e) => deletedUris.contains(e.uri)).toList()); + source.removeEntries(selection.where((e) => deletedUris.contains(e.uri)).toSet()); } collection.clearSelection(); collection.browse(); diff --git a/lib/widgets/filter_grids/common/chip_action_delegate.dart b/lib/widgets/filter_grids/common/chip_action_delegate.dart index 3d220401b..7760fac44 100644 --- a/lib/widgets/filter_grids/common/chip_action_delegate.dart +++ b/lib/widgets/filter_grids/common/chip_action_delegate.dart @@ -92,7 +92,7 @@ class AlbumChipActionDelegate extends ChipActionDelegate with FeedbackMixin, Per showFeedback(context, 'Failed to delete ${Intl.plural(count, one: '$count item', other: '$count items')}'); } if (deletedCount > 0) { - source.removeEntries(selection.where((e) => deletedUris.contains(e.uri)).toList()); + source.removeEntries(selection.where((e) => deletedUris.contains(e.uri)).toSet()); } }, ); diff --git a/lib/widgets/viewer/entry_action_delegate.dart b/lib/widgets/viewer/entry_action_delegate.dart index 566405e0c..d53ea166b 100644 --- a/lib/widgets/viewer/entry_action_delegate.dart +++ b/lib/widgets/viewer/entry_action_delegate.dart @@ -141,7 +141,7 @@ class EntryActionDelegate with FeedbackMixin, PermissionAwareMixin, SizeAwareMix showFeedback(context, 'Failed'); } else { if (hasCollection) { - collection.source.removeEntries([entry]); + collection.source.removeEntries({entry}); } EntryDeletedNotification(entry).dispatch(context); }