fix: deleting a sort doesn't allow creating a new sort on that field (#4660)

This commit is contained in:
Richard Shiue 2024-02-16 13:39:49 +08:00 committed by GitHub
parent e81a2ff577
commit 3e0a003872
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,5 @@
import 'dart:collection';
import 'package:appflowy/plugins/database/application/database_view_service.dart';
import 'package:appflowy/plugins/database/application/field_settings/field_settings_listener.dart';
import 'package:appflowy/plugins/database/application/field_settings/field_settings_service.dart';
@ -333,6 +335,32 @@ class FieldController {
}
}
void updateFieldInfos(
List<SortInfo> newSortInfos,
SortChangesetNotificationPB changeset,
) {
final changedFieldIds = HashSet<String>.from([
...changeset.insertSorts.map((sort) => sort.sort.fieldId),
...changeset.updateSorts.map((sort) => sort.fieldId),
...changeset.deleteSorts.map((sort) => sort.fieldId),
]);
final newFieldInfos = [...fieldInfos];
for (final fieldId in changedFieldIds) {
final index =
newFieldInfos.indexWhere((fieldInfo) => fieldInfo.id == fieldId);
if (index == -1) {
continue;
}
newFieldInfos[index] = newFieldInfos[index].copyWith(
hasSort: newSortInfos.any((sort) => sort.fieldId == fieldId),
);
}
_fieldNotifier.fieldInfos = newFieldInfos;
}
_sortsListener.start(
onSortChanged: (result) {
if (_isDisposed) {
@ -346,6 +374,7 @@ class FieldController {
updateSortFromChangeset(newSortInfos, changeset);
_sortNotifier?.sorts = newSortInfos;
updateFieldInfos(newSortInfos, changeset);
},
(err) => Log.error(err),
);