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) {
|
static List<CloudFile>? folderChildren(String? folderID) {
|
||||||
final folders = _map(StorageKeys.folderChildrenIndex);
|
final folders = _map(StorageKeys.folderChildrenIndex);
|
||||||
final entry = folders[_folderKey(folderID)];
|
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) {
|
static Future<void> _enqueue(Future<void> Function() operation) {
|
||||||
_writes = _writes.then((_) => operation());
|
_writes = _writes.then((_) => operation());
|
||||||
return _writes;
|
return _writes;
|
||||||
|
|||||||
@@ -549,6 +549,9 @@ class FileNotifier extends StateNotifier<FileState> {
|
|||||||
state = state.copyWith(statusMessage: '正在删除…');
|
state = state.copyWith(statusMessage: '正在删除…');
|
||||||
try {
|
try {
|
||||||
await _api!.fsDelete(files.map((f) => f.id).toList());
|
await _api!.fsDelete(files.map((f) => f.id).toList());
|
||||||
|
await FileMetadataCache.removeFilesFromAllFolders(
|
||||||
|
files.map((file) => file.id),
|
||||||
|
);
|
||||||
await FileMetadataCache.updateFolderChildren(
|
await FileMetadataCache.updateFolderChildren(
|
||||||
_currentParentID,
|
_currentParentID,
|
||||||
removeIDs: files.map((file) => file.id),
|
removeIDs: files.map((file) => file.id),
|
||||||
@@ -568,6 +571,9 @@ class FileNotifier extends StateNotifier<FileState> {
|
|||||||
if (_api == null) return;
|
if (_api == null) return;
|
||||||
try {
|
try {
|
||||||
await _api!.fsRecycle(files.map((f) => f.id).toList());
|
await _api!.fsRecycle(files.map((f) => f.id).toList());
|
||||||
|
await FileMetadataCache.removeFilesFromAllFolders(
|
||||||
|
files.map((file) => file.id),
|
||||||
|
);
|
||||||
await FileMetadataCache.updateFolderChildren(
|
await FileMetadataCache.updateFolderChildren(
|
||||||
_currentParentID,
|
_currentParentID,
|
||||||
removeIDs: files.map((file) => file.id),
|
removeIDs: files.map((file) => file.id),
|
||||||
@@ -609,6 +615,7 @@ class FileNotifier extends StateNotifier<FileState> {
|
|||||||
final ids = state.clipboard!.map((f) => f.id).toList();
|
final ids = state.clipboard!.map((f) => f.id).toList();
|
||||||
if (state.clipboardIsMove) {
|
if (state.clipboardIsMove) {
|
||||||
await _api!.fsMove(ids, parentID: _currentParentID);
|
await _api!.fsMove(ids, parentID: _currentParentID);
|
||||||
|
await FileMetadataCache.removeFilesFromAllFolders(ids);
|
||||||
await FileMetadataCache.updateFolderChildren(
|
await FileMetadataCache.updateFolderChildren(
|
||||||
_currentParentID,
|
_currentParentID,
|
||||||
addOrReplace: state.clipboard!,
|
addOrReplace: state.clipboard!,
|
||||||
@@ -743,6 +750,9 @@ class FileNotifier extends StateNotifier<FileState> {
|
|||||||
files.map((file) => file.id).toList(),
|
files.map((file) => file.id).toList(),
|
||||||
parentID: parentID,
|
parentID: parentID,
|
||||||
);
|
);
|
||||||
|
await FileMetadataCache.removeFilesFromAllFolders(
|
||||||
|
files.map((file) => file.id),
|
||||||
|
);
|
||||||
await FileMetadataCache.updateFolderChildren(
|
await FileMetadataCache.updateFolderChildren(
|
||||||
_currentParentID,
|
_currentParentID,
|
||||||
removeIDs: files.map((file) => file.id),
|
removeIDs: files.map((file) => file.id),
|
||||||
|
|||||||
Reference in New Issue
Block a user