fix: synchronize folder caches after file changes
This commit is contained in:
@@ -78,6 +78,29 @@ class FileMetadataCache {
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> removeFilesFromAllFolders(Iterable<String> fileIDs) {
|
||||
final removed = fileIDs.toSet();
|
||||
if (removed.isEmpty) return Future.value();
|
||||
return _enqueue(() async {
|
||||
final folders = _map(StorageKeys.folderChildrenIndex);
|
||||
for (final entry in folders.entries.toList()) {
|
||||
final snapshot = entry.value;
|
||||
if (snapshot is! Map || snapshot['children'] is! List) continue;
|
||||
final children = (snapshot['children'] as List)
|
||||
.whereType<Map>()
|
||||
.where((child) => !removed.contains(_fileID(child)))
|
||||
.toList();
|
||||
final original = snapshot['children'] as List;
|
||||
if (children.length == original.length) continue;
|
||||
folders[entry.key] = {
|
||||
'childIds': children.map(_fileID).whereType<String>().toList(),
|
||||
'children': children,
|
||||
};
|
||||
}
|
||||
await StorageManager.set(StorageKeys.folderChildrenIndex, folders);
|
||||
});
|
||||
}
|
||||
|
||||
static List<CloudFile>? folderChildren(String? folderID) {
|
||||
final folders = _map(StorageKeys.folderChildrenIndex);
|
||||
final entry = folders[_folderKey(folderID)];
|
||||
@@ -125,6 +148,16 @@ class FileMetadataCache {
|
||||
}
|
||||
}
|
||||
|
||||
static String? _fileID(Map value) {
|
||||
for (final key in const ['id', 'fileId', 'file_id', 'resId', 'res_id']) {
|
||||
final candidate = value[key];
|
||||
if (candidate != null && candidate.toString().isNotEmpty) {
|
||||
return candidate.toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static Future<void> _enqueue(Future<void> Function() operation) {
|
||||
_writes = _writes.then((_) => operation());
|
||||
return _writes;
|
||||
|
||||
@@ -549,6 +549,9 @@ class FileNotifier extends StateNotifier<FileState> {
|
||||
state = state.copyWith(statusMessage: '正在删除…');
|
||||
try {
|
||||
await _api!.fsDelete(files.map((f) => f.id).toList());
|
||||
await FileMetadataCache.removeFilesFromAllFolders(
|
||||
files.map((file) => file.id),
|
||||
);
|
||||
await FileMetadataCache.updateFolderChildren(
|
||||
_currentParentID,
|
||||
removeIDs: files.map((file) => file.id),
|
||||
@@ -568,6 +571,9 @@ class FileNotifier extends StateNotifier<FileState> {
|
||||
if (_api == null) return;
|
||||
try {
|
||||
await _api!.fsRecycle(files.map((f) => f.id).toList());
|
||||
await FileMetadataCache.removeFilesFromAllFolders(
|
||||
files.map((file) => file.id),
|
||||
);
|
||||
await FileMetadataCache.updateFolderChildren(
|
||||
_currentParentID,
|
||||
removeIDs: files.map((file) => file.id),
|
||||
@@ -609,6 +615,7 @@ class FileNotifier extends StateNotifier<FileState> {
|
||||
final ids = state.clipboard!.map((f) => f.id).toList();
|
||||
if (state.clipboardIsMove) {
|
||||
await _api!.fsMove(ids, parentID: _currentParentID);
|
||||
await FileMetadataCache.removeFilesFromAllFolders(ids);
|
||||
await FileMetadataCache.updateFolderChildren(
|
||||
_currentParentID,
|
||||
addOrReplace: state.clipboard!,
|
||||
@@ -743,6 +750,9 @@ class FileNotifier extends StateNotifier<FileState> {
|
||||
files.map((file) => file.id).toList(),
|
||||
parentID: parentID,
|
||||
);
|
||||
await FileMetadataCache.removeFilesFromAllFolders(
|
||||
files.map((file) => file.id),
|
||||
);
|
||||
await FileMetadataCache.updateFolderChildren(
|
||||
_currentParentID,
|
||||
removeIDs: files.map((file) => file.id),
|
||||
|
||||
Reference in New Issue
Block a user