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);
}
T? getCellData() {
T? getCellData({bool loadIfNoCache = true}) {
final data = cellCache.get(_cacheKey);
if (data == null) {
if (data == null && loadIfNoCache) {
_loadData();
}
return data;

View File

@ -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) {
return result.fold(
(data) {
if (!isClosed) {
add(SelectOptionEditorEvent.didReceiveOptions(data.options, data.selectOptions));
_delayOperation?.cancel();
_delayOperation = Timer(const Duration(milliseconds: 10), () {
_selectOptionService.getOpitonContext().then((result) {
if (isClosed) {
return;
}
},
return result.fold(
(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 ?? [],