|
@@ -13,28 +13,19 @@ part 'selection_editor_bloc.freezed.dart';
|
|
|
|
|
|
class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionEditorState> {
|
|
|
final SelectOptionService _selectOptionService;
|
|
|
- final SingleFieldListener _fieldListener;
|
|
|
- final CellListener _cellListener;
|
|
|
+ final GridCellContext<SelectOptionContext> cellContext;
|
|
|
Timer? _delayOperation;
|
|
|
|
|
|
SelectOptionEditorBloc({
|
|
|
- required GridCell cellData,
|
|
|
- required List<SelectOption> options,
|
|
|
- required List<SelectOption> selectedOptions,
|
|
|
- }) : _selectOptionService = SelectOptionService(),
|
|
|
- _fieldListener = SingleFieldListener(fieldId: cellData.field.id),
|
|
|
- _cellListener = CellListener(rowId: cellData.rowId, fieldId: cellData.field.id),
|
|
|
- super(SelectOptionEditorState.initial(cellData, options, selectedOptions)) {
|
|
|
+ required this.cellContext,
|
|
|
+ }) : _selectOptionService = SelectOptionService(gridCell: cellContext.gridCell),
|
|
|
+ super(SelectOptionEditorState.initial(cellContext)) {
|
|
|
on<SelectOptionEditorEvent>(
|
|
|
(event, emit) async {
|
|
|
await event.map(
|
|
|
initial: (_Initial value) async {
|
|
|
_startListening();
|
|
|
},
|
|
|
- didReceiveFieldUpdate: (_DidReceiveFieldUpdate value) {
|
|
|
- emit(state.copyWith(field: value.field));
|
|
|
- _loadOptions();
|
|
|
- },
|
|
|
didReceiveOptions: (_DidReceiveOptions value) {
|
|
|
emit(state.copyWith(
|
|
|
options: value.options,
|
|
@@ -61,26 +52,17 @@ class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionE
|
|
|
@override
|
|
|
Future<void> close() async {
|
|
|
_delayOperation?.cancel();
|
|
|
- await _fieldListener.stop();
|
|
|
- await _cellListener.stop();
|
|
|
+ cellContext.removeListener();
|
|
|
return super.close();
|
|
|
}
|
|
|
|
|
|
void _createOption(String name) async {
|
|
|
- final result = await _selectOptionService.create(
|
|
|
- gridId: state.gridId,
|
|
|
- fieldId: state.field.id,
|
|
|
- rowId: state.rowId,
|
|
|
- name: name,
|
|
|
- );
|
|
|
- result.fold((l) => _loadOptions(), (err) => Log.error(err));
|
|
|
+ final result = await _selectOptionService.create(name: name);
|
|
|
+ result.fold((l) => {}, (err) => Log.error(err));
|
|
|
}
|
|
|
|
|
|
void _deleteOption(SelectOption option) async {
|
|
|
final result = await _selectOptionService.delete(
|
|
|
- gridId: state.gridId,
|
|
|
- fieldId: state.field.id,
|
|
|
- rowId: state.rowId,
|
|
|
option: option,
|
|
|
);
|
|
|
|
|
@@ -89,9 +71,6 @@ class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionE
|
|
|
|
|
|
void _updateOption(SelectOption option) async {
|
|
|
final result = await _selectOptionService.update(
|
|
|
- gridId: state.gridId,
|
|
|
- fieldId: state.field.id,
|
|
|
- rowId: state.rowId,
|
|
|
option: option,
|
|
|
);
|
|
|
|
|
@@ -101,70 +80,50 @@ class SelectOptionEditorBloc extends Bloc<SelectOptionEditorEvent, SelectOptionE
|
|
|
void _onSelectOption(String optionId) {
|
|
|
final hasSelected = state.selectedOptions.firstWhereOrNull((option) => option.id == optionId);
|
|
|
if (hasSelected != null) {
|
|
|
- _selectOptionService.unSelect(
|
|
|
- gridId: state.gridId,
|
|
|
- fieldId: state.field.id,
|
|
|
- rowId: state.rowId,
|
|
|
- optionId: optionId,
|
|
|
- );
|
|
|
+ _selectOptionService.unSelect(optionId: optionId);
|
|
|
} else {
|
|
|
- _selectOptionService.select(
|
|
|
- gridId: state.gridId,
|
|
|
- fieldId: state.field.id,
|
|
|
- rowId: state.rowId,
|
|
|
- optionId: optionId,
|
|
|
- );
|
|
|
+ _selectOptionService.select(optionId: optionId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void _loadOptions() async {
|
|
|
- _delayOperation?.cancel();
|
|
|
- _delayOperation = Timer(
|
|
|
- const Duration(milliseconds: 1),
|
|
|
- () async {
|
|
|
- final result = await _selectOptionService.getOpitonContext(
|
|
|
- gridId: state.gridId,
|
|
|
- fieldId: state.field.id,
|
|
|
- rowId: state.rowId,
|
|
|
- );
|
|
|
- if (isClosed) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- result.fold(
|
|
|
- (selectOptionContext) => add(SelectOptionEditorEvent.didReceiveOptions(
|
|
|
- selectOptionContext.options,
|
|
|
- selectOptionContext.selectOptions,
|
|
|
- )),
|
|
|
- (err) => Log.error(err),
|
|
|
- );
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
+ // void _loadOptions() async {
|
|
|
+ // _delayOperation?.cancel();
|
|
|
+ // _delayOperation = Timer(
|
|
|
+ // const Duration(milliseconds: 1),
|
|
|
+ // () async {
|
|
|
+ // final result = await _selectOptionService.getOpitonContext();
|
|
|
+ // if (isClosed) {
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // result.fold(
|
|
|
+ // (selectOptionContext) => add(SelectOptionEditorEvent.didReceiveOptions(
|
|
|
+ // selectOptionContext.options,
|
|
|
+ // selectOptionContext.selectOptions,
|
|
|
+ // )),
|
|
|
+ // (err) => Log.error(err),
|
|
|
+ // );
|
|
|
+ // },
|
|
|
+ // );
|
|
|
+ // }
|
|
|
|
|
|
void _startListening() {
|
|
|
- _cellListener.updateCellNotifier?.addPublishListener((result) {
|
|
|
- result.fold(
|
|
|
- (notificationData) => _loadOptions(),
|
|
|
- (err) => Log.error(err),
|
|
|
- );
|
|
|
+ cellContext.onCellChanged((selectOptionContext) {
|
|
|
+ if (!isClosed) {
|
|
|
+ add(SelectOptionEditorEvent.didReceiveOptions(
|
|
|
+ selectOptionContext.options,
|
|
|
+ selectOptionContext.selectOptions,
|
|
|
+ ));
|
|
|
+ }
|
|
|
});
|
|
|
- _cellListener.start();
|
|
|
-
|
|
|
- _fieldListener.updateFieldNotifier?.addPublishListener((result) {
|
|
|
- result.fold(
|
|
|
- (field) => add(SelectOptionEditorEvent.didReceiveFieldUpdate(field)),
|
|
|
- (err) => Log.error(err),
|
|
|
- );
|
|
|
- }, listenWhen: () => !isClosed);
|
|
|
- _fieldListener.start();
|
|
|
+
|
|
|
+ cellContext.onFieldChanged(() => cellContext.reloadCellData());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@freezed
|
|
|
class SelectOptionEditorEvent with _$SelectOptionEditorEvent {
|
|
|
const factory SelectOptionEditorEvent.initial() = _Initial;
|
|
|
- const factory SelectOptionEditorEvent.didReceiveFieldUpdate(Field field) = _DidReceiveFieldUpdate;
|
|
|
const factory SelectOptionEditorEvent.didReceiveOptions(
|
|
|
List<SelectOption> options, List<SelectOption> selectedOptions) = _DidReceiveOptions;
|
|
|
const factory SelectOptionEditorEvent.newOption(String optionName) = _NewOption;
|
|
@@ -176,24 +135,15 @@ class SelectOptionEditorEvent with _$SelectOptionEditorEvent {
|
|
|
@freezed
|
|
|
class SelectOptionEditorState with _$SelectOptionEditorState {
|
|
|
const factory SelectOptionEditorState({
|
|
|
- required String gridId,
|
|
|
- required Field field,
|
|
|
- required String rowId,
|
|
|
required List<SelectOption> options,
|
|
|
required List<SelectOption> selectedOptions,
|
|
|
}) = _SelectOptionEditorState;
|
|
|
|
|
|
- factory SelectOptionEditorState.initial(
|
|
|
- GridCell cellData,
|
|
|
- List<SelectOption> options,
|
|
|
- List<SelectOption> selectedOptions,
|
|
|
- ) {
|
|
|
+ factory SelectOptionEditorState.initial(GridCellContext<SelectOptionContext> context) {
|
|
|
+ final data = context.getCellData();
|
|
|
return SelectOptionEditorState(
|
|
|
- gridId: cellData.gridId,
|
|
|
- field: cellData.field,
|
|
|
- rowId: cellData.rowId,
|
|
|
- options: options,
|
|
|
- selectedOptions: selectedOptions,
|
|
|
+ options: data?.options ?? [],
|
|
|
+ selectedOptions: data?.selectOptions ?? [],
|
|
|
);
|
|
|
}
|
|
|
}
|