Skip to content

Commit b8582b9

Browse files
committed
chore: set loaded data in SelectOptionCellEditorBloc
1 parent 62c322a commit b8582b9

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/context_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ class _GridCellContext<T, D> extends Equatable {
163163
_cellDataNotifier.removeListener(fn);
164164
}
165165

166-
T? getCellData() {
166+
T? getCellData({bool loadIfNoCache = true}) {
167167
final data = cellCache.get(_cacheKey);
168-
if (data == null) {
168+
if (data == null && loadIfNoCache) {
169169
_loadData();
170170
}
171171
return data;

frontend/app_flowy/lib/workspace/application/grid/cell/select_option_editor_bloc.dart

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'package:app_flowy/workspace/application/grid/field/grid_listenr.dart';
23
import 'package:dartz/dartz.dart';
34
import 'package:flowy_sdk/log.dart';
45
import 'package:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.dart';
@@ -13,11 +14,14 @@ part 'select_option_editor_bloc.freezed.dart';
1314
class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionEditorState> {
1415
final SelectOptionService _selectOptionService;
1516
final GridSelectOptionCellContext cellContext;
17+
late final GridFieldsListener _fieldListener;
1618
void Function()? _onCellChangedFn;
19+
Timer? _delayOperation;
1720

1821
SelectOptionCellEditorBloc({
1922
required this.cellContext,
2023
}) : _selectOptionService = SelectOptionService(gridCell: cellContext.gridCell),
24+
_fieldListener = GridFieldsListener(gridId: cellContext.gridId),
2125
super(SelectOptionEditorState.initial(cellContext)) {
2226
on<SelectOptionEditorEvent>(
2327
(event, emit) async {
@@ -64,6 +68,8 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
6468
cellContext.removeListener(_onCellChangedFn!);
6569
_onCellChangedFn = null;
6670
}
71+
_delayOperation?.cancel();
72+
await _fieldListener.stop();
6773
cellContext.dispose();
6874
return super.close();
6975
}
@@ -108,23 +114,21 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
108114
}
109115

110116
void _loadOptions() {
111-
final selectionCellData = cellContext.getCellData();
112-
if (selectionCellData == null) {
113-
final service = SelectOptionService(gridCell: cellContext.gridCell);
114-
service.getOpitonContext().then((result) {
117+
_delayOperation?.cancel();
118+
_delayOperation = Timer(const Duration(milliseconds: 10), () {
119+
_selectOptionService.getOpitonContext().then((result) {
120+
if (isClosed) {
121+
return;
122+
}
115123
return result.fold(
116-
(data) {
117-
if (!isClosed) {
118-
add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions));
119-
}
120-
},
124+
(data) => add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions)),
121125
(err) {
122126
Log.error(err);
123127
return null;
124128
},
125129
);
126130
});
127-
}
131+
});
128132
}
129133

130134
_MakeOptionResult _makeOptions(Option<String> filter, List<SelectOption> allOptions) {
@@ -156,13 +160,21 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
156160
_onCellChangedFn = cellContext.startListening(
157161
onCellChanged: ((selectOptionContext) {
158162
if (!isClosed) {
159-
add(SelectOptionEditorEvent.didReceiveOptions(
160-
selectOptionContext.options,
161-
selectOptionContext.selectOptions,
162-
));
163+
_loadOptions();
163164
}
164165
}),
165166
);
167+
168+
_fieldListener.start(onFieldsChanged: (result) {
169+
result.fold(
170+
(changeset) {
171+
if (changeset.updatedFields.isNotEmpty) {
172+
_loadOptions();
173+
}
174+
},
175+
(err) => Log.error(err),
176+
);
177+
});
166178
}
167179
}
168180

@@ -189,7 +201,7 @@ class SelectOptionEditorState with _$SelectOptionEditorState {
189201
}) = _SelectOptionEditorState;
190202

191203
factory SelectOptionEditorState.initial(GridSelectOptionCellContext context) {
192-
final data = context.getCellData();
204+
final data = context.getCellData(loadIfNoCache: false);
193205
return SelectOptionEditorState(
194206
options: data?.options ?? [],
195207
allOptions: data?.options ?? [],

0 commit comments

Comments
 (0)