Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

chore: set loaded data in SelectOptionCellEditorBloc

appflowyb8582b9b6845
diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart
index c6f71a2e32..aad8137f2e 100644
--- a/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart
+++ b/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart
@@ -163,9 +163,9 @@ class _GridCellContext<T, D> extends Equatable {
_cellDataNotifier.removeListener(fn);
}
- T? getCellData() {
+ T? getCellData({bool loadIfNoCache = true}) {
final data = cellCache.get(_cacheKey);
- if (data == null) {
+ if (data == null && loadIfNoCache) {
_loadData();
}
return data;
diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/select_option_editor_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/select_option_editor_bloc.dart
index d5065aa6fa..87eabdf759 100644
--- a/frontend/app_flowy/lib/workspace/application/grid/cell/select_option_editor_bloc.dart
+++ b/frontend/app_flowy/lib/workspace/application/grid/cell/select_option_editor_bloc.dart
@@ -1,4 +1,5 @@
import 'dart:async';
+import 'package:app_flowy/workspace/application/grid/field/grid_listenr.dart';
import 'package:dartz/dartz.dart';
import 'package:flowy_sdk/log.dart';
import 'package:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.dart';
@@ -13,11 +14,14 @@ part 'select_option_editor_bloc.freezed.dart';
class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionEditorState> {
final SelectOptionService _selectOptionService;
final GridSelectOptionCellContext cellContext;
+ late final GridFieldsListener _fieldListener;
void Function()? _onCellChangedFn;
+ Timer? _delayOperation;
SelectOptionCellEditorBloc({
required this.cellContext,
}) : _selectOptionService = SelectOptionService(gridCell: cellContext.gridCell),
+ _fieldListener = GridFieldsListener(gridId: cellContext.gridId),
super(SelectOptionEditorState.initial(cellContext)) {
on<SelectOptionEditorEvent>(
(event, emit) async {
@@ -64,6 +68,8 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
cellContext.removeListener(_onCellChangedFn!);
_onCellChangedFn = null;
}
+ _delayOperation?.cancel();
+ await _fieldListener.stop();
cellContext.dispose();
return super.close();
}
@@ -108,23 +114,21 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
}
void _loadOptions() {
- final selectionCellData = cellContext.getCellData();
- if (selectionCellData == null) {
- final service = SelectOptionService(gridCell: cellContext.gridCell);
- service.getOpitonContext().then((result) {
+ _delayOperation?.cancel();
+ _delayOperation = Timer(const Duration(milliseconds: 10), () {
+ _selectOptionService.getOpitonContext().then((result) {
+ if (isClosed) {
+ return;
+ }
return result.fold(
- (data) {
- if (!isClosed) {
- add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions));
- }
- },
+ (data) => add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions)),
(err) {
Log.error(err);
return null;
},
);
});
- }
+ });
}
_MakeOptionResult _makeOptions(Option<String> filter, List<SelectOption> allOptions) {
@@ -156,13 +160,21 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
_onCellChangedFn = cellContext.startListening(
onCellChanged: ((selectOptionContext) {
if (!isClosed) {
- add(SelectOptionEditorEvent.didReceiveOptions(
- selectOptionContext.options,
- selectOptionContext.selectOptions,
- ));
+ _loadOptions();
}
}),
);
+
+ _fieldListener.start(onFieldsChanged: (result) {
+ result.fold(
+ (changeset) {
+ if (changeset.updatedFields.isNotEmpty) {
+ _loadOptions();
+ }
+ },
+ (err) => Log.error(err),
+ );
+ });
}
}
@@ -189,7 +201,7 @@ class SelectOptionEditorState with _$SelectOptionEditorState {
}) = _SelectOptionEditorState;
factory SelectOptionEditorState.initial(GridSelectOptionCellContext context) {
- final data = context.getCellData();
+ final data = context.getCellData(loadIfNoCache: false);
return SelectOptionEditorState(
options: data?.options ?? [],
allOptions: data?.options ?? [],