86 lines
2.9 KiB
Dart
86 lines
2.9 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';
|
|
|
|
import 'remote_origin.dart';
|
|
import 'package:aves/remote/remote_sync_bus.dart';
|
|
|
|
extension CollectionSourceRemoteExt on CollectionSource {
|
|
/// Carica dal DB tutte le entry remote (origin == RemoteOrigin.value) 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: RemoteOrigin.value);
|
|
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 == RemoteOrigin.value && !e.trashed)
|
|
.map((e) => e.remoteId)
|
|
.whereType<String>()
|
|
.toSet();
|
|
|
|
final Set<String> existingUris = allEntries
|
|
.where((e) => e.origin == RemoteOrigin.value && !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 == RemoteOrigin.value && !e.trashed)
|
|
.length;
|
|
addEntries(toAdd);
|
|
final dopo = allEntries
|
|
.where((e) => e.origin == RemoteOrigin.value && !e.trashed)
|
|
.length;
|
|
|
|
debugPrint(
|
|
'[remote-append] appese=${dopo - prima} (prima=$prima -> dopo=$dopo)');
|
|
}
|
|
}
|
|
|
|
/// 🔥 Visibilità dei thumbs remoti
|
|
/// - syncing → visibili
|
|
/// - upToDate → visibili
|
|
/// - serverDown → nascosti
|
|
/// - disabled → nascosti
|
|
extension RemoteVisibilityExt on CollectionSource {
|
|
bool get remoteVisible {
|
|
final state = RemoteSyncBus.instance.stateNotifier.value;
|
|
return state == RemoteSyncState.syncing ||
|
|
state == RemoteSyncState.upToDate;
|
|
}
|
|
}
|