462 lines
18 KiB
Text
462 lines
18 KiB
Text
// lib/model/source/media_store_source.dart
|
|
import 'dart:async';
|
|
|
|
import 'package:aves/model/covers.dart';
|
|
import 'package:aves/model/dynamic_albums.dart';
|
|
import 'package:aves/model/entry/entry.dart';
|
|
import 'package:aves/model/entry/origins.dart';
|
|
import 'package:aves/model/favourites.dart';
|
|
import 'package:aves/model/filters/covered/stored_album.dart';
|
|
import 'package:aves/model/grouping/common.dart';
|
|
import 'package:aves/model/settings/settings.dart';
|
|
import 'package:aves/model/source/analysis_controller.dart';
|
|
import 'package:aves/model/source/collection_source.dart';
|
|
import 'package:aves/model/vaults/vaults.dart';
|
|
import 'package:aves/services/common/services.dart';
|
|
import 'package:aves/theme/durations.dart';
|
|
import 'package:aves/utils/android_file_utils.dart';
|
|
import 'package:aves/utils/debouncer.dart';
|
|
import 'package:aves_model/aves_model.dart';
|
|
import 'package:collection/collection.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
// solo per i contatori (facoltativi)
|
|
import 'package:sqflite/sqflite.dart' show Sqflite;
|
|
|
|
class MediaStoreSource extends CollectionSource {
|
|
final Debouncer _changeDebouncer = Debouncer(delay: ADurations.mediaContentChangeDebounceDelay);
|
|
final Set<String> _changedUris = {};
|
|
int? _lastGeneration;
|
|
SourceScope _loadedScope, _targetScope;
|
|
bool _canAnalyze = true;
|
|
Future<void>? _essentialLoader;
|
|
|
|
@override
|
|
set canAnalyze(bool enabled) => _canAnalyze = enabled;
|
|
|
|
@override
|
|
SourceScope get loadedScope => _loadedScope;
|
|
|
|
@override
|
|
SourceScope get targetScope => _targetScope;
|
|
|
|
@override
|
|
Future<void> init({
|
|
required SourceScope scope,
|
|
AnalysisController? analysisController,
|
|
bool loadTopEntriesFirst = false,
|
|
}) async {
|
|
_targetScope = scope;
|
|
await reportService.log('$runtimeType init target scope=$scope');
|
|
_essentialLoader ??= _loadEssentials();
|
|
await _essentialLoader;
|
|
addDirectories(albums: settings.pinnedFilters.whereType<StoredAlbumFilter>().map((v) => v.album).toSet());
|
|
await updateGeneration();
|
|
unawaited(
|
|
_loadEntries(
|
|
analysisController: analysisController,
|
|
loadTopEntriesFirst: loadTopEntriesFirst,
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _loadEssentials() async {
|
|
final stopwatch = Stopwatch()..start();
|
|
state = SourceState.loading;
|
|
await localMediaDb.init();
|
|
await vaults.init();
|
|
await favourites.init();
|
|
albumGrouping.init();
|
|
albumGrouping.setGroups(settings.albumGroups);
|
|
albumGrouping.registerSource(this);
|
|
tagGrouping.init();
|
|
tagGrouping.setGroups(settings.tagGroups);
|
|
tagGrouping.registerSource(this);
|
|
await covers.init();
|
|
await dynamicAlbums.init();
|
|
|
|
final deviceOffset = DateTime.now().timeZoneOffset.inMilliseconds;
|
|
final catalogOffset = settings.catalogTimeZoneOffsetMillis;
|
|
if (deviceOffset != catalogOffset) {
|
|
unawaited(reportService.log(
|
|
'Time zone offset change: $catalogOffset -> $deviceOffset. Clear catalog metadata to get correct date/times.'));
|
|
await localMediaDb.clearDates();
|
|
await localMediaDb.clearCatalogMetadata();
|
|
settings.catalogTimeZoneOffsetMillis = deviceOffset;
|
|
}
|
|
await loadDates();
|
|
debugPrint('$runtimeType load essentials complete in ${stopwatch.elapsed.inMilliseconds}ms');
|
|
}
|
|
|
|
Future<void> _loadEntries({
|
|
AnalysisController? analysisController,
|
|
required bool loadTopEntriesFirst,
|
|
}) async {
|
|
unawaited(reportService.log('$runtimeType load (known) start'));
|
|
final stopwatch = Stopwatch()..start();
|
|
state = SourceState.loading;
|
|
|
|
final scopeAlbumFilters = _targetScope?.whereType<StoredAlbumFilter>();
|
|
final scopeDirectory =
|
|
scopeAlbumFilters != null && scopeAlbumFilters.length == 1 ? scopeAlbumFilters.first.album : null;
|
|
|
|
// 🔒 Sentinella: conteggio remoti PRIMA (facoltativo)
|
|
final preRem = Sqflite.firstIntValue(
|
|
await localMediaDb.rawDb.rawQuery('SELECT COUNT(*) FROM entry WHERE origin=1'),
|
|
) ??
|
|
0;
|
|
debugPrint('[cleanup][pre] remoti in DB = $preRem');
|
|
|
|
// 🔧 PATCH: non azzerare tutta la tabella; cancella SOLO i locali
|
|
final swClear = Stopwatch()..start();
|
|
final deletedLocal = await localMediaDb.rawDb
|
|
.rawDelete('DELETE FROM entry WHERE origin = ?', [EntryOrigins.mediaStoreContent]);
|
|
swClear.stop();
|
|
debugPrint('$runtimeType load ${swClear.elapsed} clear local entries deleted $deletedLocal rows');
|
|
|
|
// 🔒 Sentinella: conteggio remoti DOPO (Δ deve essere 0)
|
|
final postRem = Sqflite.firstIntValue(
|
|
await localMediaDb.rawDb.rawQuery('SELECT COUNT(*) FROM entry WHERE origin=1'),
|
|
) ??
|
|
0;
|
|
debugPrint('[cleanup][post] remoti in DB = $postRem (Δ=${postRem - preRem})');
|
|
|
|
final Set<AvesEntry> topEntries = {};
|
|
if (loadTopEntriesFirst) {
|
|
final topIds = settings.topEntryIds?.toSet();
|
|
if (topIds != null) {
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} load ${topIds.length} top entries');
|
|
topEntries.addAll(await localMediaDb.loadEntriesById(topIds));
|
|
addEntries(topEntries);
|
|
}
|
|
}
|
|
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} fetch known entries');
|
|
final knownEntries =
|
|
await localMediaDb.loadEntries(origin: EntryOrigins.mediaStoreContent, directory: scopeDirectory);
|
|
final knownLiveEntries = knownEntries.where((entry) => !entry.trashed).toSet();
|
|
final isLargeCollection = knownEntries.length > 80000;
|
|
if (isLargeCollection && settings.isErrorReportingAllowed) {
|
|
settings.isErrorReportingAllowed = false;
|
|
}
|
|
unawaited(reportService.setCustomKey('is_large_collection', isLargeCollection));
|
|
unawaited(reportService.log('$runtimeType found ${knownEntries.length} known entries'));
|
|
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} check obsolete entries');
|
|
final knownDateByContentId =
|
|
Map.fromEntries(knownLiveEntries.map((entry) => MapEntry(entry.contentId, entry.dateModifiedMillis)));
|
|
final knownContentIds = knownDateByContentId.keys.toList();
|
|
final removedContentIds = (await mediaStoreService.checkObsoleteContentIds(knownContentIds)).toSet();
|
|
if (topEntries.isNotEmpty) {
|
|
final removedTopEntries = topEntries.where((entry) => removedContentIds.contains(entry.contentId));
|
|
await removeEntries(removedTopEntries.map((entry) => entry.uri).toSet(), includeTrash: false);
|
|
}
|
|
final removedEntries = knownEntries.where((entry) => removedContentIds.contains(entry.contentId)).toSet();
|
|
knownEntries.removeAll(removedEntries);
|
|
|
|
// show known entries
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} add known entries');
|
|
addEntries(knownEntries, notify: false);
|
|
notifyAlbumsChanged();
|
|
|
|
await _loadVaultEntries(scopeDirectory);
|
|
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} load metadata');
|
|
if (scopeDirectory != null) {
|
|
final ids = knownLiveEntries.map((entry) => entry.id).toSet();
|
|
await loadCatalogMetadata(ids: ids);
|
|
await loadAddresses(ids: ids);
|
|
} else {
|
|
await loadCatalogMetadata();
|
|
await loadAddresses();
|
|
|
|
// trash
|
|
await loadTrashDetails();
|
|
unawaited(
|
|
deleteExpiredTrash().then(
|
|
(deletedUris) {
|
|
if (deletedUris.isNotEmpty) {
|
|
debugPrint('evicted ${deletedUris.length} expired items from the trash');
|
|
removeEntries(deletedUris, includeTrash: true);
|
|
}
|
|
},
|
|
onError: (error) => debugPrint('failed to evict expired trash error=$error'),
|
|
),
|
|
);
|
|
}
|
|
updateDerivedFilters();
|
|
|
|
// clean up obsolete entries (sono locali: derivano dai knownEntries locali)
|
|
if (removedEntries.isNotEmpty) {
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} remove obsolete entries');
|
|
await localMediaDb.removeIds(removedEntries.map((entry) => entry.id).toSet());
|
|
}
|
|
|
|
_loadedScope = _targetScope;
|
|
unawaited(reportService
|
|
.log('$runtimeType load (known) done in ${stopwatch.elapsed.inSeconds}s for ${knownEntries.length} known, ${removedEntries.length} removed'));
|
|
|
|
if (_canAnalyze) {
|
|
await _loadNewEntries(
|
|
analysisController: analysisController,
|
|
directory: scopeDirectory,
|
|
knownLiveEntries: knownLiveEntries,
|
|
knownDateByContentId: knownDateByContentId,
|
|
);
|
|
} else {
|
|
state = SourceState.ready;
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
final newEntries = <AvesEntry>{};
|
|
|
|
// recover untracked trash items
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} recover untracked entries');
|
|
if (directory == null) {
|
|
newEntries.addAll(await recoverUntrackedTrashItems());
|
|
}
|
|
|
|
// verify paths
|
|
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) {
|
|
// mark obsolete (sulla mappa dei locali)
|
|
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) {
|
|
final contentId = entry.contentId;
|
|
final existingEntry = knownContentIds.contains(contentId)
|
|
? knownLiveEntries.firstWhereOrNull((entry) => entry.contentId == contentId)
|
|
: null;
|
|
entry.id = existingEntry?.id ?? localMediaDb.nextId;
|
|
|
|
newEntries.add(entry);
|
|
setProgress(done: newEntries.length, total: 0);
|
|
},
|
|
onDone: () async {
|
|
if (newEntries.isNotEmpty) {
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} save ${newEntries.length} new entries');
|
|
await localMediaDb.insertEntries(newEntries);
|
|
|
|
// dedup locali
|
|
final duplicates = await localMediaDb.searchLiveDuplicates(EntryOrigins.mediaStoreContent, newEntries);
|
|
if (duplicates.isNotEmpty) {
|
|
unawaited(reportService
|
|
.recordError(Exception('Loading entries yielded duplicates=${duplicates.join(', ')}')));
|
|
await localMediaDb.removeIds(duplicates.map((v) => v.id).toSet());
|
|
for (final duplicate in duplicates) {
|
|
final duplicateId = duplicate.id;
|
|
newEntries.removeWhere((v) => v.id == duplicateId);
|
|
}
|
|
}
|
|
|
|
// update trash details, if any
|
|
await Future.forEach(newEntries.where((v) => v.trashed), (entry) async {
|
|
final trashDetails = entry.trashDetails;
|
|
if (trashDetails != null) {
|
|
await localMediaDb.updateTrash(entry.id, trashDetails);
|
|
} else {
|
|
unawaited(reportService
|
|
.recordError(Exception('Adding trashed entry but trash details are missing for entry=$entry')));
|
|
}
|
|
});
|
|
|
|
addEntries(newEntries);
|
|
invalidateAlbumFilterSummary();
|
|
updateDirectories();
|
|
}
|
|
|
|
debugPrint('$runtimeType load ${stopwatch.elapsed} analyze');
|
|
Set<AvesEntry>? analysisEntries;
|
|
final analysisIds = analysisController?.entryIds;
|
|
if (analysisIds != null) {
|
|
analysisEntries = allEntries.where((entry) => analysisIds.contains(entry.id)).toSet();
|
|
}
|
|
await analyze(analysisController, entries: analysisEntries);
|
|
|
|
notifyAlbumsChanged();
|
|
|
|
unawaited(reportService
|
|
.log('$runtimeType load (new) done in ${stopwatch.elapsed.inSeconds}s for ${newEntries.length} new entries'));
|
|
},
|
|
onError: (error) => debugPrint('$runtimeType stream error=$error'),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<Set<String>> refreshUris(Set<String> changedUris, {AnalysisController? analysisController}) async {
|
|
if (!canRefresh || _essentialLoader == null || !isReady) return changedUris;
|
|
|
|
state = SourceState.loading;
|
|
|
|
unawaited(reportService.log('$runtimeType refresh start for ${changedUris.length} uris'));
|
|
final changedUriByContentId = Map.fromEntries(
|
|
changedUris.map((uri) {
|
|
final pathSegments = Uri.parse(uri).pathSegments;
|
|
if (pathSegments.isEmpty) return null;
|
|
final idString = pathSegments.last;
|
|
final contentId = int.tryParse(idString);
|
|
if (contentId == null) return null;
|
|
return MapEntry(contentId, uri);
|
|
}).nonNulls,
|
|
);
|
|
|
|
// clean up obsolete entries (URIs di MediaStore => locali)
|
|
final obsoleteContentIds =
|
|
(await mediaStoreService.checkObsoleteContentIds(changedUriByContentId.keys.toList())).toSet();
|
|
final obsoleteUris =
|
|
obsoleteContentIds.map((contentId) => changedUriByContentId[contentId]).nonNulls.toSet();
|
|
await removeEntries(obsoleteUris, includeTrash: false);
|
|
obsoleteContentIds.forEach(changedUriByContentId.remove);
|
|
|
|
// fetch new entries
|
|
final tempUris = <String>{};
|
|
final newEntries = <AvesEntry>{}, entriesToRefresh = <AvesEntry>{};
|
|
final existingDirectories = <String>{};
|
|
for (final kv in changedUriByContentId.entries) {
|
|
final contentId = kv.key;
|
|
final uri = kv.value;
|
|
final sourceEntry = await mediaFetchService.getEntry(uri, null);
|
|
if (sourceEntry != null) {
|
|
final existingEntry = allEntries.firstWhereOrNull((entry) => entry.contentId == contentId);
|
|
if (existingEntry == null ||
|
|
(sourceEntry.dateModifiedMillis ?? 0) > (existingEntry.dateModifiedMillis ?? 0) ||
|
|
sourceEntry.path != existingEntry.path) {
|
|
final newPath = sourceEntry.path;
|
|
final volume = newPath != null ? androidFileUtils.getStorageVolume(newPath) : null;
|
|
if (volume != null) {
|
|
if (existingEntry != null) {
|
|
entriesToRefresh.add(existingEntry);
|
|
} else if (_canAnalyze) {
|
|
sourceEntry.id = localMediaDb.nextId;
|
|
newEntries.add(sourceEntry);
|
|
}
|
|
final existingDirectory = existingEntry?.directory;
|
|
if (existingDirectory != null) {
|
|
existingDirectories.add(existingDirectory);
|
|
}
|
|
} else {
|
|
debugPrint('$runtimeType refreshUris entry=$sourceEntry is not located on a known storage volume. Will retry soon...');
|
|
tempUris.add(uri);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
await _refreshVaultEntries(
|
|
changedUris: changedUris.where(vaults.isVaultEntryUri).toSet(),
|
|
newEntries: newEntries,
|
|
entriesToRefresh: entriesToRefresh,
|
|
existingDirectories: existingDirectories,
|
|
);
|
|
|
|
invalidateAlbumFilterSummary(directories: existingDirectories);
|
|
|
|
if (newEntries.isNotEmpty) {
|
|
await localMediaDb.insertEntries(newEntries);
|
|
|
|
final duplicates = await localMediaDb.searchLiveDuplicates(EntryOrigins.mediaStoreContent, newEntries);
|
|
if (duplicates.isNotEmpty) {
|
|
unawaited(reportService
|
|
.recordError(Exception('Refreshing entries yielded duplicates=${duplicates.join(', ')}')));
|
|
await localMediaDb.removeIds(duplicates.map((v) => v.id).toSet());
|
|
for (final duplicate in duplicates) {
|
|
final duplicateId = duplicate.id;
|
|
newEntries.removeWhere((v) => v.id == duplicateId);
|
|
tempUris.add(duplicate.uri);
|
|
}
|
|
}
|
|
|
|
addEntries(newEntries);
|
|
await analyze(analysisController, entries: newEntries);
|
|
}
|
|
|
|
if (entriesToRefresh.isNotEmpty) {
|
|
await refreshEntries(entriesToRefresh, EntryDataType.values.toSet());
|
|
}
|
|
|
|
unawaited(reportService.log('$runtimeType refresh end for ${changedUris.length} uris'));
|
|
|
|
state = SourceState.ready;
|
|
|
|
return tempUris;
|
|
}
|
|
|
|
void onStoreChanged(String? uri) {
|
|
if (uri != null) _changedUris.add(uri);
|
|
if (_changedUris.isNotEmpty) {
|
|
_changeDebouncer(() async {
|
|
final todo = _changedUris.toSet();
|
|
_changedUris.clear();
|
|
final tempUris = await refreshUris(todo);
|
|
if (tempUris.isNotEmpty) {
|
|
_changedUris.addAll(tempUris);
|
|
onStoreChanged(null);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> checkForChanges() async {
|
|
final sinceGeneration = _lastGeneration;
|
|
if (sinceGeneration != null) {
|
|
_changedUris.addAll(await mediaStoreService.getChangedUris(sinceGeneration));
|
|
onStoreChanged(null);
|
|
}
|
|
await updateGeneration();
|
|
}
|
|
|
|
Future<void> updateGeneration() async {
|
|
_lastGeneration = await mediaStoreService.getGeneration();
|
|
}
|
|
|
|
// vault
|
|
|
|
Future<void> _loadVaultEntries(String? directory) async {
|
|
addEntries(await localMediaDb.loadEntries(origin: EntryOrigins.vault, directory: directory));
|
|
}
|
|
|
|
Future<void> _refreshVaultEntries({
|
|
required Set<String> changedUris,
|
|
required Set<AvesEntry> newEntries,
|
|
required Set<AvesEntry> entriesToRefresh,
|
|
required Set<String> existingDirectories,
|
|
}) async {
|
|
for (final uri in changedUris) {
|
|
final existingEntry = allEntries.firstWhereOrNull((entry) => entry.uri == uri);
|
|
if (existingEntry != null) {
|
|
entriesToRefresh.add(existingEntry);
|
|
final existingDirectory = existingEntry.directory;
|
|
if (existingDirectory != null) {
|
|
existingDirectories.add(existingDirectory);
|
|
}
|
|
} else {
|
|
final sourceEntry = await mediaFetchService.getEntry(uri, null, allowUnsized: true);
|
|
if (sourceEntry != null) {
|
|
newEntries.add(
|
|
sourceEntry.copyWith(
|
|
id: localMediaDb.nextId,
|
|
origin: EntryOrigins.vault,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|