Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
deckerst committed Oct 13, 2024
1 parent 1058aba commit bb89756
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/model/source/collection_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ abstract class CollectionSource with SourceBase, AlbumMixin, CountryMixin, Place
}
}
}
// TODO TLAD check whether app is in foreground before starting service
if (startAnalysisService) {
await AnalysisService.startService(
force: force,
Expand Down
43 changes: 31 additions & 12 deletions lib/model/source/media_store_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class MediaStoreSource extends CollectionSource {
String? directory,
required bool loadTopEntriesFirst,
}) async {
unawaited(reportService.log('$runtimeType load start'));
unawaited(reportService.log('$runtimeType load (known) start'));
final stopwatch = Stopwatch()..start();
state = SourceState.loading;
clearEntries();
Expand Down Expand Up @@ -146,20 +146,29 @@ class MediaStoreSource extends CollectionSource {
await localMediaDb.removeIds(removedEntries.map((entry) => entry.id).toSet());
}

// verify paths because some apps move files without updating their `last modified date`
debugPrint('$runtimeType load ${stopwatch.elapsed} check obsolete paths');
final knownPathByContentId = Map.fromEntries(knownLiveEntries.map((entry) => MapEntry(entry.contentId, entry.path)));
final movedContentIds = (await mediaStoreService.checkObsoletePaths(knownPathByContentId)).toSet();
movedContentIds.forEach((contentId) {
// make obsolete by resetting its modified date
knownDateByContentId[contentId] = 0;
});
unawaited(reportService.log('$runtimeType load (known) done in ${stopwatch.elapsed.inSeconds}s for ${knownEntries.length} known, ${removedEntries.length} removed'));

if (!_canAnalyze) {
if (_canAnalyze) {
// it can discover new entries only if it can analyze them
await _loadNewEntries(
analysisController: analysisController,
directory: directory,
knownLiveEntries: knownLiveEntries,
knownDateByContentId: knownDateByContentId,
);
} else {
state = SourceState.ready;
return;
}
}

Future<void> _loadNewEntries({
required AnalysisController? analysisController,
required String? directory,
required Set<AvesEntry> knownLiveEntries,
required Map<int?, int?> knownDateByContentId,
}) async {
unawaited(reportService.log('$runtimeType load (new) start'));
final stopwatch = Stopwatch()..start();

// items to add to the collection
final newEntries = <AvesEntry>{};
Expand All @@ -170,8 +179,18 @@ class MediaStoreSource extends CollectionSource {
newEntries.addAll(await recoverUntrackedTrashItems());
}

// verify paths because some apps move files without updating their `last modified date`
debugPrint('$runtimeType load ${stopwatch.elapsed} check obsolete paths');
final knownPathByContentId = Map.fromEntries(knownLiveEntries.map((entry) => MapEntry(entry.contentId, entry.path)));
final movedContentIds = (await mediaStoreService.checkObsoletePaths(knownPathByContentId)).toSet();
movedContentIds.forEach((contentId) {
// make obsolete by resetting its modified date
knownDateByContentId[contentId] = 0;
});

// fetch new & modified entries
debugPrint('$runtimeType load ${stopwatch.elapsed} fetch new entries');
final knownContentIds = knownDateByContentId.keys.toSet();
mediaStoreService.getEntries(knownDateByContentId, directory: directory).listen(
(entry) {
// when discovering modified entry with known content ID,
Expand Down Expand Up @@ -220,7 +239,7 @@ class MediaStoreSource extends CollectionSource {
// so we manually notify change for potential home screen filters
notifyAlbumsChanged();

unawaited(reportService.log('$runtimeType load done in ${stopwatch.elapsed.inSeconds}s for ${knownEntries.length} known, ${newEntries.length} new, ${removedEntries.length} removed'));
unawaited(reportService.log('$runtimeType load (new) done in ${stopwatch.elapsed.inSeconds}s for ${newEntries.length} new entries'));
},
onError: (error) => debugPrint('$runtimeType stream error=$error'),
);
Expand Down

0 comments on commit bb89756

Please sign in to comment.