This commit is contained in:
Thibault Deckers 2024-10-13 23:03:25 +02:00
parent 1058aba262
commit bb89756815
2 changed files with 32 additions and 12 deletions

View file

@ -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) { if (startAnalysisService) {
await AnalysisService.startService( await AnalysisService.startService(
force: force, force: force,

View file

@ -79,7 +79,7 @@ class MediaStoreSource extends CollectionSource {
String? directory, String? directory,
required bool loadTopEntriesFirst, required bool loadTopEntriesFirst,
}) async { }) async {
unawaited(reportService.log('$runtimeType load start')); unawaited(reportService.log('$runtimeType load (known) start'));
final stopwatch = Stopwatch()..start(); final stopwatch = Stopwatch()..start();
state = SourceState.loading; state = SourceState.loading;
clearEntries(); clearEntries();
@ -146,20 +146,29 @@ class MediaStoreSource extends CollectionSource {
await localMediaDb.removeIds(removedEntries.map((entry) => entry.id).toSet()); await localMediaDb.removeIds(removedEntries.map((entry) => entry.id).toSet());
} }
// verify paths because some apps move files without updating their `last modified date` unawaited(reportService.log('$runtimeType load (known) done in ${stopwatch.elapsed.inSeconds}s for ${knownEntries.length} known, ${removedEntries.length} removed'));
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;
});
if (!_canAnalyze) { if (_canAnalyze) {
// it can discover new entries only if it can analyze them // 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; 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 // items to add to the collection
final newEntries = <AvesEntry>{}; final newEntries = <AvesEntry>{};
@ -170,8 +179,18 @@ class MediaStoreSource extends CollectionSource {
newEntries.addAll(await recoverUntrackedTrashItems()); 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 // fetch new & modified entries
debugPrint('$runtimeType load ${stopwatch.elapsed} fetch new entries'); debugPrint('$runtimeType load ${stopwatch.elapsed} fetch new entries');
final knownContentIds = knownDateByContentId.keys.toSet();
mediaStoreService.getEntries(knownDateByContentId, directory: directory).listen( mediaStoreService.getEntries(knownDateByContentId, directory: directory).listen(
(entry) { (entry) {
// when discovering modified entry with known content ID, // when discovering modified entry with known content ID,
@ -220,7 +239,7 @@ class MediaStoreSource extends CollectionSource {
// so we manually notify change for potential home screen filters // so we manually notify change for potential home screen filters
notifyAlbumsChanged(); 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'), onError: (error) => debugPrint('$runtimeType stream error=$error'),
); );