Fix device sync: folder status badges, remove-deletes-local, multi-select, header summary
Build APK / build (push) Successful in 5m35s
Build APK / build (push) Successful in 5m35s
- Folders now show the synced status badge themselves, not just their nested files - device-sync state never tracked folders individually, so syncStatusFor derives a folder's badge from whether its path falls within sync scope instead. - Turning sync off for a path now deletes its local mirror and clears its native sync-state entries (SyncEngine.removeLocalSync), instead of just stopping future updates and leaving the downloaded copy behind. - "Sync to device" in Files' selection toolbar now works over the whole selection at once instead of being gated to a single selected item. - The sync header's expanded panel shows real folder/item counts instead of a static "Synced" label that stayed accurate-sounding even before anything had actually synced. - Confirmation SnackBars on starting/stopping sync from both the selection toolbar and Settings' Device Sync card. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,10 +20,15 @@ class SyncStatusController extends ChangeNotifier {
|
||||
final SessionController session;
|
||||
|
||||
static const _prefSyncedPaths = 'ui_synced_folders';
|
||||
static const _prefSyncedPathTypes = 'ui_synced_folder_types';
|
||||
static const _prefSyncEverything = 'ui_sync_everything';
|
||||
static const _prefSyncOnCellular = 'ui_sync_on_cellular';
|
||||
|
||||
List<String> _syncedPaths = [];
|
||||
// path -> isFolder, keyed the same as [_syncedPaths] - missing entries
|
||||
// (from before this map existed) default to folder, the overwhelmingly
|
||||
// common case.
|
||||
Map<String, bool> _syncedPathTypes = {};
|
||||
bool _syncEverything = false;
|
||||
bool _syncOnCellular = false;
|
||||
|
||||
@@ -47,6 +52,16 @@ class SyncStatusController extends ChangeNotifier {
|
||||
bool get isSyncingNow => _isSyncingNow;
|
||||
List<SyncConflictInfo> get syncConflicts => List.unmodifiable(_syncConflicts);
|
||||
|
||||
/// How many of [syncedPaths] are folders (as opposed to individual
|
||||
/// files) - used by the sync header's expanded summary.
|
||||
int get syncedFolderCount =>
|
||||
_syncedPaths.where((p) => _syncedPathTypes[p] ?? true).length;
|
||||
|
||||
/// How many individual files have actually been mirrored locally so far
|
||||
/// - distinct from [syncedFolderCount]/[syncedPaths], which are just the
|
||||
/// configured *targets*, not what's actually landed on disk yet.
|
||||
int get syncedItemCount => _syncedFileIds.length;
|
||||
|
||||
SyncHeaderStatus get syncHeaderStatus {
|
||||
if (_syncConflicts.isNotEmpty) return SyncHeaderStatus.alert;
|
||||
if (_isSyncingNow) return SyncHeaderStatus.syncing;
|
||||
@@ -54,7 +69,47 @@ class SyncStatusController extends ChangeNotifier {
|
||||
return SyncHeaderStatus.done;
|
||||
}
|
||||
|
||||
/// True if [path] itself, or an ancestor of it, is covered by device
|
||||
/// sync - either explicitly (one of [_syncedPaths]) or via
|
||||
/// [_syncEverything]. Shared by [syncStatusFor] (folders) and
|
||||
/// [localSyncedFilePath] (files).
|
||||
bool _isPathInSyncScope(String path) {
|
||||
if (_syncEverything) return true;
|
||||
final normalized = path.endsWith('/')
|
||||
? path.substring(0, path.length - 1)
|
||||
: path;
|
||||
return _syncedPaths.any((folder) {
|
||||
final f = folder.endsWith('/')
|
||||
? folder.substring(0, folder.length - 1)
|
||||
: folder;
|
||||
return normalized == f || normalized.startsWith('$f/');
|
||||
});
|
||||
}
|
||||
|
||||
/// True if any pending conflict falls under [folderPath].
|
||||
bool _folderHasConflict(String folderPath) {
|
||||
final normalized = folderPath.endsWith('/')
|
||||
? folderPath.substring(0, folderPath.length - 1)
|
||||
: folderPath;
|
||||
return _syncConflicts.any((c) {
|
||||
final p = c.remotePath.endsWith('/')
|
||||
? c.remotePath.substring(0, c.remotePath.length - 1)
|
||||
: c.remotePath;
|
||||
return p == normalized || p.startsWith('$normalized/');
|
||||
});
|
||||
}
|
||||
|
||||
/// Folders don't get their own entry in [_syncedFileIds] (only individual
|
||||
/// files do - see `SyncEngine.diffFolder`'s `if (entry.isFolder) continue`),
|
||||
/// so a folder's status is derived from whether it's in sync scope at all
|
||||
/// rather than tracked per-item like a file's is.
|
||||
SyncItemStatus syncStatusFor(NextcloudItem item) {
|
||||
if (item.isFolder) {
|
||||
if (!_isPathInSyncScope(item.path)) return SyncItemStatus.none;
|
||||
if (_folderHasConflict(item.path)) return SyncItemStatus.conflict;
|
||||
if (_isSyncingNow) return SyncItemStatus.syncing;
|
||||
return SyncItemStatus.synced;
|
||||
}
|
||||
if (_syncingFileIds.contains(item.id)) return SyncItemStatus.syncing;
|
||||
if (_syncConflicts.any((c) => c.fileId == item.id)) {
|
||||
return SyncItemStatus.conflict;
|
||||
@@ -71,6 +126,7 @@ class SyncStatusController extends ChangeNotifier {
|
||||
|
||||
void _onAccountCleared() {
|
||||
_syncedPaths = [];
|
||||
_syncedPathTypes = {};
|
||||
_syncEverything = false;
|
||||
_isSyncingNow = false;
|
||||
_syncingFileIds = {};
|
||||
@@ -95,6 +151,19 @@ class SyncStatusController extends ChangeNotifier {
|
||||
} else {
|
||||
_syncedPaths = [];
|
||||
}
|
||||
final typesJson = prefs.getString(k(_prefSyncedPathTypes));
|
||||
if (typesJson != null) {
|
||||
try {
|
||||
_syncedPathTypes = (jsonDecode(typesJson) as Map).map(
|
||||
(k, v) => MapEntry(k as String, v as bool),
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[SyncStatusController] Synced path types restore failed: $e');
|
||||
_syncedPathTypes = {};
|
||||
}
|
||||
} else {
|
||||
_syncedPathTypes = {};
|
||||
}
|
||||
_syncEverything = prefs.getBool(k(_prefSyncEverything)) ?? false;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -117,30 +186,40 @@ class SyncStatusController extends ChangeNotifier {
|
||||
void _persistSyncedPaths() {
|
||||
final id = session.activeAccountId;
|
||||
if (id == null) return;
|
||||
session.prefsFuture.then(
|
||||
(p) => p.setString(
|
||||
session.prefsFuture.then((p) {
|
||||
p.setString(
|
||||
session.accountStore.accountPrefKey(id, _prefSyncedPaths),
|
||||
jsonEncode(_syncedPaths),
|
||||
),
|
||||
);
|
||||
);
|
||||
p.setString(
|
||||
session.accountStore.accountPrefKey(id, _prefSyncedPathTypes),
|
||||
jsonEncode(_syncedPathTypes),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
bool isPathSynced(String path) => _syncedPaths.contains(path);
|
||||
|
||||
void addSyncedPath(String path) {
|
||||
void addSyncedPath(String path, {required bool isFolder}) {
|
||||
if (_syncedPaths.contains(path)) return;
|
||||
_syncedPaths = [..._syncedPaths, path];
|
||||
_syncedPathTypes = {..._syncedPathTypes, path: isFolder};
|
||||
notifyListeners();
|
||||
_persistSyncedPaths();
|
||||
unawaited(SyncService.reschedule(session, this));
|
||||
}
|
||||
|
||||
/// Unsyncs [path] and deletes its already-downloaded local mirror (see
|
||||
/// `SyncService.removeLocalSync`) - stopping sync alone would leave
|
||||
/// whatever had already been downloaded sitting on disk indefinitely.
|
||||
void removeSyncedPath(String path) {
|
||||
if (!_syncedPaths.contains(path)) return;
|
||||
_syncedPaths = _syncedPaths.where((f) => f != path).toList();
|
||||
_syncedPathTypes = {..._syncedPathTypes}..remove(path);
|
||||
notifyListeners();
|
||||
_persistSyncedPaths();
|
||||
unawaited(SyncService.reschedule(session, this));
|
||||
unawaited(SyncService.removeLocalSync(session, path));
|
||||
}
|
||||
|
||||
void setSyncEverything(bool value) {
|
||||
@@ -192,18 +271,7 @@ class SyncStatusController extends ChangeNotifier {
|
||||
Future<String?> localSyncedFilePath(NextcloudItem item) async {
|
||||
final id = session.activeAccountId;
|
||||
if (id == null) return null;
|
||||
final itemPath = item.path.endsWith('/')
|
||||
? item.path.substring(0, item.path.length - 1)
|
||||
: item.path;
|
||||
final isSynced =
|
||||
_syncEverything ||
|
||||
_syncedPaths.any((folder) {
|
||||
final f = folder.endsWith('/')
|
||||
? folder.substring(0, folder.length - 1)
|
||||
: folder;
|
||||
return itemPath == f || itemPath.startsWith('$f/');
|
||||
});
|
||||
if (!isSynced) return null;
|
||||
if (!_isPathInSyncScope(item.path)) return null;
|
||||
return SyncService.localSyncedFilePath(id, item.path);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user