refactor: clean up select type option logic (#4554)

This commit is contained in:
Richard Shiue 2024-01-30 23:20:11 +08:00 committed by GitHub
parent 792573f46d
commit 18c7fff34b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 54 additions and 132 deletions

View File

@ -1,8 +1,8 @@
import 'package:appflowy_backend/protobuf/flowy-database2/select_option_entities.pb.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:protobuf/protobuf.dart';
part 'edit_select_option_bloc.freezed.dart';
class EditSelectOptionBloc
@ -11,15 +11,15 @@ class EditSelectOptionBloc
: super(EditSelectOptionState.initial(option)) {
on<EditSelectOptionEvent>(
(event, emit) async {
event.map(
updateName: (_UpdateName value) {
emit(state.copyWith(option: _updateName(value.name)));
event.when(
updateName: (name) {
emit(state.copyWith(option: _updateName(name)));
},
updateColor: (_UpdateColor value) {
emit(state.copyWith(option: _updateColor(value.color)));
updateColor: (color) {
emit(state.copyWith(option: _updateColor(color)));
},
delete: (_Delete value) {
emit(state.copyWith(deleted: const Some(true)));
delete: () {
emit(state.copyWith(deleted: true));
},
);
},
@ -53,12 +53,12 @@ class EditSelectOptionEvent with _$EditSelectOptionEvent {
class EditSelectOptionState with _$EditSelectOptionState {
const factory EditSelectOptionState({
required SelectOptionPB option,
required Option<bool> deleted,
required bool deleted,
}) = _EditSelectOptionState;
factory EditSelectOptionState.initial(SelectOptionPB option) =>
EditSelectOptionState(
option: option,
deleted: none(),
deleted: false,
);
}

View File

@ -1,28 +1,12 @@
import 'dart:async';
import 'package:appflowy_backend/protobuf/flowy-database2/select_option_entities.pb.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'select_type_option_actions.dart';
part 'select_option_type_option_bloc.freezed.dart';
abstract class ISelectOptionAction {
Future<List<SelectOptionPB>> insertOption(
List<SelectOptionPB> options,
String newOptionName,
);
List<SelectOptionPB> deleteOption(
List<SelectOptionPB> options,
SelectOptionPB deletedOption,
);
List<SelectOptionPB> updateOption(
List<SelectOptionPB> options,
SelectOptionPB updatedOption,
);
}
class SelectOptionTypeOptionBloc
extends Bloc<SelectOptionTypeOptionEvent, SelectOptionTypeOptionState> {
SelectOptionTypeOptionBloc({

View File

@ -4,11 +4,10 @@ import 'package:appflowy/plugins/database/grid/presentation/widgets/header/type_
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/select_option_entities.pb.dart';
import 'select_option_type_option_bloc.dart';
import 'type_option_service.dart';
class MultiSelectAction implements ISelectOptionAction {
MultiSelectAction({
abstract class ISelectOptionAction {
ISelectOptionAction({
required this.onTypeOptionUpdated,
required String viewId,
required String fieldId,
@ -17,7 +16,11 @@ class MultiSelectAction implements ISelectOptionAction {
final TypeOptionBackendService service;
final TypeOptionDataCallback onTypeOptionUpdated;
@override
void updateTypeOption(List<SelectOptionPB> options) {
final newTypeOption = MultiSelectTypeOptionPB()..options.addAll(options);
onTypeOptionUpdated(newTypeOption.writeToBuffer());
}
Future<List<SelectOptionPB>> insertOption(
List<SelectOptionPB> options,
String optionName,
@ -32,7 +35,7 @@ class MultiSelectAction implements ISelectOptionAction {
newOptions.insert(0, option);
}
_updateTypeOption(newOptions);
updateTypeOption(newOptions);
return newOptions;
},
(err) {
@ -43,7 +46,6 @@ class MultiSelectAction implements ISelectOptionAction {
});
}
@override
List<SelectOptionPB> deleteOption(
List<SelectOptionPB> options,
SelectOptionPB deletedOption,
@ -55,11 +57,10 @@ class MultiSelectAction implements ISelectOptionAction {
newOptions.removeAt(index);
}
_updateTypeOption(newOptions);
updateTypeOption(newOptions);
return newOptions;
}
@override
List<SelectOptionPB> updateOption(
List<SelectOptionPB> options,
SelectOptionPB option,
@ -70,12 +71,35 @@ class MultiSelectAction implements ISelectOptionAction {
newOptions[index] = option;
}
_updateTypeOption(newOptions);
updateTypeOption(newOptions);
return newOptions;
}
}
void _updateTypeOption(List<SelectOptionPB> options) {
class MultiSelectAction extends ISelectOptionAction {
MultiSelectAction({
required super.viewId,
required super.fieldId,
required super.onTypeOptionUpdated,
});
@override
void updateTypeOption(List<SelectOptionPB> options) {
final newTypeOption = MultiSelectTypeOptionPB()..options.addAll(options);
onTypeOptionUpdated(newTypeOption.writeToBuffer());
}
}
class SingleSelectAction extends ISelectOptionAction {
SingleSelectAction({
required super.viewId,
required super.fieldId,
required super.onTypeOptionUpdated,
});
@override
void updateTypeOption(List<SelectOptionPB> options) {
final newTypeOption = SingleSelectTypeOptionPB()..options.addAll(options);
onTypeOptionUpdated(newTypeOption.writeToBuffer());
}
}

View File

@ -1,83 +0,0 @@
import 'dart:async';
import 'package:appflowy/plugins/database/grid/presentation/widgets/header/type_option/builder.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/select_option_entities.pb.dart';
import 'select_option_type_option_bloc.dart';
import 'type_option_service.dart';
class SingleSelectAction implements ISelectOptionAction {
SingleSelectAction({
required this.onTypeOptionUpdated,
required String viewId,
required String fieldId,
}) : service = TypeOptionBackendService(viewId: viewId, fieldId: fieldId);
final TypeOptionBackendService service;
final TypeOptionDataCallback onTypeOptionUpdated;
@override
Future<List<SelectOptionPB>> insertOption(
List<SelectOptionPB> options,
String optionName,
) {
final newOptions = List<SelectOptionPB>.from(options);
return service.newOption(name: optionName).then((result) {
return result.fold(
(option) {
final exists =
newOptions.any((element) => element.name == option.name);
if (!exists) {
newOptions.insert(0, option);
}
_updateTypeOption(newOptions);
return newOptions;
},
(err) {
Log.error(err);
return newOptions;
},
);
});
}
@override
List<SelectOptionPB> deleteOption(
List<SelectOptionPB> options,
SelectOptionPB deletedOption,
) {
final newOptions = List<SelectOptionPB>.from(options);
final index =
newOptions.indexWhere((option) => option.id == deletedOption.id);
if (index != -1) {
newOptions.removeAt(index);
}
final newTypeOption = MultiSelectTypeOptionPB()..options.addAll(newOptions);
onTypeOptionUpdated(newTypeOption.writeToBuffer());
return newOptions;
}
@override
List<SelectOptionPB> updateOption(
List<SelectOptionPB> options,
SelectOptionPB updatedOption,
) {
final newOptions = List<SelectOptionPB>.from(options);
final index =
newOptions.indexWhere((option) => option.id == updatedOption.id);
if (index != -1) {
newOptions[index] = updatedOption;
}
_updateTypeOption(newOptions);
return newOptions;
}
void _updateTypeOption(List<SelectOptionPB> options) {
final newTypeOption = SingleSelectTypeOptionPB()..options.addAll(options);
onTypeOptionUpdated(newTypeOption.writeToBuffer());
}
}

View File

@ -1,10 +1,4 @@
import 'package:appflowy_backend/protobuf/flowy-database2/checkbox_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/date_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/number_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/select_option_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/text_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/timestamp_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/url_entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
abstract class TypeOptionParser<T> {
T fromBuffer(List<int> buffer);

View File

@ -1,4 +1,4 @@
import 'package:appflowy/plugins/database/application/field/type_option/multi_select_type_option.dart';
import 'package:appflowy/plugins/database/application/field/type_option/select_type_option_actions.dart';
import 'package:appflowy/plugins/database/application/field/type_option/type_option_data_parser.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'package:flutter/material.dart';

View File

@ -1,5 +1,6 @@
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/plugins/database/application/field/type_option/select_option_type_option_bloc.dart';
import 'package:appflowy/plugins/database/application/field/type_option/select_type_option_actions.dart';
import 'package:appflowy/plugins/database/grid/presentation/layout/sizes.dart';
import 'package:appflowy/plugins/database/widgets/cell_editor/extension.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/select_option_entities.pb.dart';

View File

@ -43,7 +43,9 @@ class SelectOptionTypeOptionEditor extends StatelessWidget {
BlocListener<EditSelectOptionBloc, EditSelectOptionState>(
listenWhen: (p, c) => p.deleted != c.deleted,
listener: (context, state) {
state.deleted.fold(() => null, (_) => onDeleted());
if (state.deleted) {
onDeleted();
}
},
),
BlocListener<EditSelectOptionBloc, EditSelectOptionState>(

View File

@ -1,4 +1,4 @@
import 'package:appflowy/plugins/database/application/field/type_option/single_select_type_option.dart';
import 'package:appflowy/plugins/database/application/field/type_option/select_type_option_actions.dart';
import 'package:appflowy/plugins/database/application/field/type_option/type_option_data_parser.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'package:flutter/material.dart';