chore: add default text when adding a new field (#2664)

This commit is contained in:
Nathan.fooo
2023-05-30 17:11:16 +08:00
committed by GitHub
parent acc1d779c5
commit 2247fa8edb
9 changed files with 33 additions and 35 deletions

View File

@ -15,7 +15,7 @@ class FieldEditorBloc extends Bloc<FieldEditorEvent, FieldEditorState> {
required String viewId, required String viewId,
required String fieldName, required String fieldName,
required bool isGroupField, required bool isGroupField,
required IFieldTypeOptionLoader loader, required ITypeOptionLoader loader,
}) : dataController = TypeOptionController(viewId: viewId, loader: loader), }) : dataController = TypeOptionController(viewId: viewId, loader: loader),
super(FieldEditorState.initial(viewId, fieldName, isGroupField)) { super(FieldEditorState.initial(viewId, fieldName, isGroupField)) {
on<FieldEditorEvent>( on<FieldEditorEvent>(
@ -28,6 +28,7 @@ class FieldEditorBloc extends Bloc<FieldEditorEvent, FieldEditorState> {
} }
}); });
await dataController.loadTypeOptionData(); await dataController.loadTypeOptionData();
add(FieldEditorEvent.didReceiveFieldChanged(dataController.field));
}, },
updateName: (name) { updateName: (name) {
if (state.name != name) { if (state.name != name) {

View File

@ -150,25 +150,14 @@ abstract class TypeOptionFieldDelegate {
void dispose(); void dispose();
} }
abstract class IFieldTypeOptionLoader { abstract class ITypeOptionLoader {
String get viewId; String get viewId;
Future<Either<TypeOptionPB, FlowyError>> load(); String get fieldName;
Future<Either<TypeOptionPB, FlowyError>> initialize();
Future<Either<Unit, FlowyError>> switchToField(
String fieldId,
FieldType fieldType,
) {
final payload = UpdateFieldTypePayloadPB.create()
..viewId = viewId
..fieldId = fieldId
..fieldType = fieldType;
return DatabaseEventUpdateFieldType(payload).send();
}
} }
/// Uses when creating a new field /// Uses when creating a new field
class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader { class NewFieldTypeOptionLoader extends ITypeOptionLoader {
TypeOptionPB? fieldTypeOption; TypeOptionPB? fieldTypeOption;
@override @override
@ -180,7 +169,7 @@ class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader {
/// Creates the field type option if the fieldTypeOption is null. /// Creates the field type option if the fieldTypeOption is null.
/// Otherwise, it loads the type option data from the backend. /// Otherwise, it loads the type option data from the backend.
@override @override
Future<Either<TypeOptionPB, FlowyError>> load() { Future<Either<TypeOptionPB, FlowyError>> initialize() {
if (fieldTypeOption != null) { if (fieldTypeOption != null) {
final payload = TypeOptionPathPB.create() final payload = TypeOptionPathPB.create()
..viewId = viewId ..viewId = viewId
@ -204,10 +193,13 @@ class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader {
}); });
} }
} }
@override
String get fieldName => fieldTypeOption?.field_2.name ?? '';
} }
/// Uses when editing a existing field /// Uses when editing a existing field
class FieldTypeOptionLoader extends IFieldTypeOptionLoader { class FieldTypeOptionLoader extends ITypeOptionLoader {
@override @override
final String viewId; final String viewId;
final FieldPB field; final FieldPB field;
@ -218,7 +210,7 @@ class FieldTypeOptionLoader extends IFieldTypeOptionLoader {
}); });
@override @override
Future<Either<TypeOptionPB, FlowyError>> load() { Future<Either<TypeOptionPB, FlowyError>> initialize() {
final payload = TypeOptionPathPB.create() final payload = TypeOptionPathPB.create()
..viewId = viewId ..viewId = viewId
..fieldId = field.id ..fieldId = field.id
@ -226,4 +218,7 @@ class FieldTypeOptionLoader extends IFieldTypeOptionLoader {
return DatabaseEventGetTypeOption(payload).send(); return DatabaseEventGetTypeOption(payload).send();
} }
@override
String get fieldName => field.name;
} }

View File

@ -1,4 +1,5 @@
import 'package:appflowy/plugins/database_view/application/field/field_controller.dart'; import 'package:appflowy/plugins/database_view/application/field/field_controller.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:flowy_infra/notifier.dart'; import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/field_entities.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-database2/field_entities.pb.dart';
@ -12,7 +13,7 @@ import 'type_option_context.dart';
class TypeOptionController { class TypeOptionController {
final String viewId; final String viewId;
late TypeOptionPB _typeOption; late TypeOptionPB _typeOption;
final IFieldTypeOptionLoader loader; final ITypeOptionLoader loader;
final PublishNotifier<FieldPB> _fieldNotifier = PublishNotifier(); final PublishNotifier<FieldPB> _fieldNotifier = PublishNotifier();
/// Returns a [TypeOptionController] used to modify the specified /// Returns a [TypeOptionController] used to modify the specified
@ -34,7 +35,7 @@ class TypeOptionController {
} }
Future<Either<TypeOptionPB, FlowyError>> loadTypeOptionData() async { Future<Either<TypeOptionPB, FlowyError>> loadTypeOptionData() async {
final result = await loader.load(); final result = await loader.initialize();
return result.fold( return result.fold(
(data) { (data) {
data.freeze(); data.freeze();
@ -85,7 +86,12 @@ class TypeOptionController {
} }
Future<void> switchToField(FieldType newFieldType) async { Future<void> switchToField(FieldType newFieldType) async {
final result = await loader.switchToField(field.id, newFieldType); final payload = UpdateFieldTypePayloadPB.create()
..viewId = viewId
..fieldId = field.id
..fieldType = newFieldType;
final result = await DatabaseEventUpdateFieldType(payload).send();
await result.fold( await result.fold(
(_) { (_) {
// Should load the type-option data after switching to a new field. // Should load the type-option data after switching to a new field.

View File

@ -38,7 +38,6 @@ class _GridFieldCellActionSheetState extends State<GridFieldCellActionSheet> {
width: 400, width: 400,
child: FieldEditor( child: FieldEditor(
viewId: widget.cellContext.viewId, viewId: widget.cellContext.viewId,
fieldName: field.name,
typeOptionLoader: FieldTypeOptionLoader( typeOptionLoader: FieldTypeOptionLoader(
viewId: widget.cellContext.viewId, viewId: widget.cellContext.viewId,
field: field, field: field,

View File

@ -16,15 +16,13 @@ import 'field_type_option_editor.dart';
class FieldEditor extends StatefulWidget { class FieldEditor extends StatefulWidget {
final String viewId; final String viewId;
final String fieldName;
final bool isGroupingField; final bool isGroupingField;
final Function(String)? onDeleted; final Function(String)? onDeleted;
final Function(String)? onHidden; final Function(String)? onHidden;
final IFieldTypeOptionLoader typeOptionLoader; final ITypeOptionLoader typeOptionLoader;
const FieldEditor({ const FieldEditor({
required this.viewId, required this.viewId,
this.fieldName = "",
required this.typeOptionLoader, required this.typeOptionLoader,
this.isGroupingField = false, this.isGroupingField = false,
this.onDeleted, this.onDeleted,
@ -63,7 +61,7 @@ class _FieldEditorState extends State<FieldEditor> {
return BlocProvider( return BlocProvider(
create: (context) => FieldEditorBloc( create: (context) => FieldEditorBloc(
viewId: widget.viewId, viewId: widget.viewId,
fieldName: widget.fieldName, fieldName: widget.typeOptionLoader.fieldName,
isGroupField: widget.isGroupingField, isGroupField: widget.isGroupingField,
loader: widget.typeOptionLoader, loader: widget.typeOptionLoader,
)..add(const FieldEditorEvent.initial()), )..add(const FieldEditorEvent.initial()),
@ -156,6 +154,7 @@ class _FieldNameTextField extends StatefulWidget {
} }
class _FieldNameTextFieldState extends State<_FieldNameTextField> { class _FieldNameTextFieldState extends State<_FieldNameTextField> {
final textController = TextEditingController();
FocusNode focusNode = FocusNode(); FocusNode focusNode = FocusNode();
@override @override
@ -180,6 +179,7 @@ class _FieldNameTextFieldState extends State<_FieldNameTextField> {
return BlocListener<FieldEditorBloc, FieldEditorState>( return BlocListener<FieldEditorBloc, FieldEditorState>(
listenWhen: (p, c) => p.field == none(), listenWhen: (p, c) => p.field == none(),
listener: (context, state) { listener: (context, state) {
textController.text = state.name;
focusNode.requestFocus(); focusNode.requestFocus();
}, },
child: BlocBuilder<FieldEditorBloc, FieldEditorState>( child: BlocBuilder<FieldEditorBloc, FieldEditorState>(
@ -190,11 +190,10 @@ class _FieldNameTextFieldState extends State<_FieldNameTextField> {
padding: const EdgeInsets.symmetric(horizontal: 12.0), padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: FlowyTextField( child: FlowyTextField(
focusNode: focusNode, focusNode: focusNode,
controller: textController,
onSubmitted: (String _) => PopoverContainer.of(context).close(), onSubmitted: (String _) => PopoverContainer.of(context).close(),
text: context.read<FieldEditorBloc>().state.name, text: state.name,
errorText: context.read<FieldEditorBloc>().state.errorText.isEmpty errorText: state.errorText.isEmpty ? null : state.errorText,
? null
: context.read<FieldEditorBloc>().state.errorText,
onChanged: (newName) { onChanged: (newName) {
context context
.read<FieldEditorBloc>() .read<FieldEditorBloc>()

View File

@ -143,7 +143,6 @@ class _GridPropertyCellState extends State<_GridPropertyCell> {
popupBuilder: (BuildContext context) { popupBuilder: (BuildContext context) {
return FieldEditor( return FieldEditor(
viewId: widget.viewId, viewId: widget.viewId,
fieldName: widget.fieldInfo.name,
typeOptionLoader: FieldTypeOptionLoader( typeOptionLoader: FieldTypeOptionLoader(
viewId: widget.viewId, viewId: widget.viewId,
field: widget.fieldInfo.field, field: widget.fieldInfo.field,

View File

@ -307,7 +307,6 @@ class _PropertyCellState extends State<_PropertyCell> {
Widget buildFieldEditor() { Widget buildFieldEditor() {
return FieldEditor( return FieldEditor(
viewId: widget.cellId.viewId, viewId: widget.cellId.viewId,
fieldName: widget.cellId.fieldInfo.field.name,
isGroupingField: widget.cellId.fieldInfo.isGroupField, isGroupingField: widget.cellId.fieldInfo.isGroupField,
typeOptionLoader: FieldTypeOptionLoader( typeOptionLoader: FieldTypeOptionLoader(
viewId: widget.cellId.viewId, viewId: widget.cellId.viewId,

View File

@ -85,7 +85,7 @@ class BoardTestContext {
FieldEditorBloc createFieldEditor({ FieldEditorBloc createFieldEditor({
FieldInfo? fieldInfo, FieldInfo? fieldInfo,
}) { }) {
IFieldTypeOptionLoader loader; ITypeOptionLoader loader;
if (fieldInfo == null) { if (fieldInfo == null) {
loader = NewFieldTypeOptionLoader(viewId: gridView.id); loader = NewFieldTypeOptionLoader(viewId: gridView.id);
} else { } else {

View File

@ -42,7 +42,7 @@ class GridTestContext {
FieldEditorBloc createFieldEditor({ FieldEditorBloc createFieldEditor({
FieldInfo? fieldInfo, FieldInfo? fieldInfo,
}) { }) {
IFieldTypeOptionLoader loader; ITypeOptionLoader loader;
if (fieldInfo == null) { if (fieldInfo == null) {
loader = NewFieldTypeOptionLoader(viewId: gridView.id); loader = NewFieldTypeOptionLoader(viewId: gridView.id);
} else { } else {