chore: set loaded data in SelectOptionCellEditorBloc

This commit is contained in:
appflowy 2022-05-28 15:55:43 +08:00
parent 62c322ab21
commit b8582b9b68
2 changed files with 29 additions and 17 deletions

View File

@ -163,9 +163,9 @@ class _GridCellContext<T, D> extends Equatable {
_cellDataNotifier.removeListener(fn); _cellDataNotifier.removeListener(fn);
} }
T? getCellData() { T? getCellData({bool loadIfNoCache = true}) {
final data = cellCache.get(_cacheKey); final data = cellCache.get(_cacheKey);
if (data == null) { if (data == null && loadIfNoCache) {
_loadData(); _loadData();
} }
return data; return data;

View File

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:app_flowy/workspace/application/grid/field/grid_listenr.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:flowy_sdk/log.dart'; import 'package:flowy_sdk/log.dart';
import 'package:flowy_sdk/protobuf/flowy-grid/selection_type_option.pb.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> { class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionEditorState> {
final SelectOptionService _selectOptionService; final SelectOptionService _selectOptionService;
final GridSelectOptionCellContext cellContext; final GridSelectOptionCellContext cellContext;
late final GridFieldsListener _fieldListener;
void Function()? _onCellChangedFn; void Function()? _onCellChangedFn;
Timer? _delayOperation;
SelectOptionCellEditorBloc({ SelectOptionCellEditorBloc({
required this.cellContext, required this.cellContext,
}) : _selectOptionService = SelectOptionService(gridCell: cellContext.gridCell), }) : _selectOptionService = SelectOptionService(gridCell: cellContext.gridCell),
_fieldListener = GridFieldsListener(gridId: cellContext.gridId),
super(SelectOptionEditorState.initial(cellContext)) { super(SelectOptionEditorState.initial(cellContext)) {
on<SelectOptionEditorEvent>( on<SelectOptionEditorEvent>(
(event, emit) async { (event, emit) async {
@ -64,6 +68,8 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
cellContext.removeListener(_onCellChangedFn!); cellContext.removeListener(_onCellChangedFn!);
_onCellChangedFn = null; _onCellChangedFn = null;
} }
_delayOperation?.cancel();
await _fieldListener.stop();
cellContext.dispose(); cellContext.dispose();
return super.close(); return super.close();
} }
@ -108,23 +114,21 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
} }
void _loadOptions() { void _loadOptions() {
final selectionCellData = cellContext.getCellData(); _delayOperation?.cancel();
if (selectionCellData == null) { _delayOperation = Timer(const Duration(milliseconds: 10), () {
final service = SelectOptionService(gridCell: cellContext.gridCell); _selectOptionService.getOpitonContext().then((result) {
service.getOpitonContext().then((result) { if (isClosed) {
return;
}
return result.fold( return result.fold(
(data) { (data) => add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions)),
if (!isClosed) {
add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions));
}
},
(err) { (err) {
Log.error(err); Log.error(err);
return null; return null;
}, },
); );
}); });
} });
} }
_MakeOptionResult _makeOptions(Option<String> filter, List<SelectOption> allOptions) { _MakeOptionResult _makeOptions(Option<String> filter, List<SelectOption> allOptions) {
@ -156,13 +160,21 @@ class SelectOptionCellEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOpt
_onCellChangedFn = cellContext.startListening( _onCellChangedFn = cellContext.startListening(
onCellChanged: ((selectOptionContext) { onCellChanged: ((selectOptionContext) {
if (!isClosed) { if (!isClosed) {
add(SelectOptionEditorEvent.didReceiveOptions( _loadOptions();
selectOptionContext.options,
selectOptionContext.selectOptions,
));
} }
}), }),
); );
_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; }) = _SelectOptionEditorState;
factory SelectOptionEditorState.initial(GridSelectOptionCellContext context) { factory SelectOptionEditorState.initial(GridSelectOptionCellContext context) {
final data = context.getCellData(); final data = context.getCellData(loadIfNoCache: false);
return SelectOptionEditorState( return SelectOptionEditorState(
options: data?.options ?? [], options: data?.options ?? [],
allOptions: data?.options ?? [], allOptions: data?.options ?? [],