64 lines
2.3 KiB
Dart
64 lines
2.3 KiB
Dart
// lib/remote/collection_source_remote_ext.dart
|
|
import 'package:aves/model/entry/entry.dart';
|
|
import 'package:aves/model/source/collection_source.dart';
|
|
import 'package:aves/services/common/services.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
extension CollectionSourceRemoteExt on CollectionSource {
|
|
/// Carica dal DB tutte le entry remote (origin=1) non cestinate
|
|
/// e le aggiunge alla CollectionSource evitando duplicati in memoria.
|
|
Future<void> appendRemoteEntriesFromDb() async {
|
|
// 1) carica dal DB (qui è Set nella tua base di codice)
|
|
final Set<AvesEntry> remoti = await localMediaDb.loadEntries(origin: 1);
|
|
debugPrint('[remote-append] candidati=${remoti.length}');
|
|
|
|
// 2) filtra visibili
|
|
final Iterable<AvesEntry> visibili = remoti.where((e) => !e.trashed);
|
|
final int visCount = visibili.length;
|
|
debugPrint('[remote-append] visibili=$visCount');
|
|
|
|
// 3) chiavi già presenti nella Source (per evitare doppioni in memoria)
|
|
final Set<String> existingRemoteIds = allEntries
|
|
.where((e) => e.origin == 1 && !e.trashed)
|
|
.map((e) => e.remoteId)
|
|
.whereType<String>()
|
|
.toSet();
|
|
|
|
final Set<String> existingUris = allEntries
|
|
.where((e) => e.origin == 1 && !e.trashed)
|
|
.map((e) => e.uri)
|
|
.whereType<String>()
|
|
.toSet();
|
|
|
|
// 4) dedupe deterministica “dentro il batch” per remoteId/uri
|
|
final Map<String, AvesEntry> byKey = <String, AvesEntry>{};
|
|
for (final e in visibili) {
|
|
final rid = e.remoteId;
|
|
final key = (rid != null && rid.isNotEmpty) ? 'rid:$rid' : 'uri:${e.uri}';
|
|
byKey[key] = e;
|
|
}
|
|
|
|
// 5) prendi solo quelli non già presenti in memoria
|
|
final Set<AvesEntry> toAdd = <AvesEntry>{};
|
|
for (final e in byKey.values) {
|
|
final rid = e.remoteId;
|
|
final u = e.uri;
|
|
|
|
final bool alreadyInMemory =
|
|
(rid != null && rid.isNotEmpty && existingRemoteIds.contains(rid)) ||
|
|
(u != null && u.isNotEmpty && existingUris.contains(u));
|
|
|
|
if (!alreadyInMemory) {
|
|
toAdd.add(e);
|
|
}
|
|
}
|
|
|
|
final prima = allEntries.where((e) => e.origin == 1 && !e.trashed).length;
|
|
addEntries(toAdd);
|
|
final dopo = allEntries.where((e) => e.origin == 1 && !e.trashed).length;
|
|
|
|
debugPrint('[remote-append] appese=${dopo - prima} (prima=$prima -> dopo=$dopo)');
|
|
}
|
|
}
|
|
|
|
|