mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: add default text when adding a new field (#2664)
This commit is contained in:
parent
acc1d779c5
commit
2247fa8edb
@ -15,7 +15,7 @@ class FieldEditorBloc extends Bloc<FieldEditorEvent, FieldEditorState> {
|
||||
required String viewId,
|
||||
required String fieldName,
|
||||
required bool isGroupField,
|
||||
required IFieldTypeOptionLoader loader,
|
||||
required ITypeOptionLoader loader,
|
||||
}) : dataController = TypeOptionController(viewId: viewId, loader: loader),
|
||||
super(FieldEditorState.initial(viewId, fieldName, isGroupField)) {
|
||||
on<FieldEditorEvent>(
|
||||
@ -28,6 +28,7 @@ class FieldEditorBloc extends Bloc<FieldEditorEvent, FieldEditorState> {
|
||||
}
|
||||
});
|
||||
await dataController.loadTypeOptionData();
|
||||
add(FieldEditorEvent.didReceiveFieldChanged(dataController.field));
|
||||
},
|
||||
updateName: (name) {
|
||||
if (state.name != name) {
|
||||
|
@ -150,25 +150,14 @@ abstract class TypeOptionFieldDelegate {
|
||||
void dispose();
|
||||
}
|
||||
|
||||
abstract class IFieldTypeOptionLoader {
|
||||
abstract class ITypeOptionLoader {
|
||||
String get viewId;
|
||||
Future<Either<TypeOptionPB, FlowyError>> load();
|
||||
|
||||
Future<Either<Unit, FlowyError>> switchToField(
|
||||
String fieldId,
|
||||
FieldType fieldType,
|
||||
) {
|
||||
final payload = UpdateFieldTypePayloadPB.create()
|
||||
..viewId = viewId
|
||||
..fieldId = fieldId
|
||||
..fieldType = fieldType;
|
||||
|
||||
return DatabaseEventUpdateFieldType(payload).send();
|
||||
}
|
||||
String get fieldName;
|
||||
Future<Either<TypeOptionPB, FlowyError>> initialize();
|
||||
}
|
||||
|
||||
/// Uses when creating a new field
|
||||
class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader {
|
||||
class NewFieldTypeOptionLoader extends ITypeOptionLoader {
|
||||
TypeOptionPB? fieldTypeOption;
|
||||
|
||||
@override
|
||||
@ -180,7 +169,7 @@ class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader {
|
||||
/// Creates the field type option if the fieldTypeOption is null.
|
||||
/// Otherwise, it loads the type option data from the backend.
|
||||
@override
|
||||
Future<Either<TypeOptionPB, FlowyError>> load() {
|
||||
Future<Either<TypeOptionPB, FlowyError>> initialize() {
|
||||
if (fieldTypeOption != null) {
|
||||
final payload = TypeOptionPathPB.create()
|
||||
..viewId = viewId
|
||||
@ -204,10 +193,13 @@ class NewFieldTypeOptionLoader extends IFieldTypeOptionLoader {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String get fieldName => fieldTypeOption?.field_2.name ?? '';
|
||||
}
|
||||
|
||||
/// Uses when editing a existing field
|
||||
class FieldTypeOptionLoader extends IFieldTypeOptionLoader {
|
||||
class FieldTypeOptionLoader extends ITypeOptionLoader {
|
||||
@override
|
||||
final String viewId;
|
||||
final FieldPB field;
|
||||
@ -218,7 +210,7 @@ class FieldTypeOptionLoader extends IFieldTypeOptionLoader {
|
||||
});
|
||||
|
||||
@override
|
||||
Future<Either<TypeOptionPB, FlowyError>> load() {
|
||||
Future<Either<TypeOptionPB, FlowyError>> initialize() {
|
||||
final payload = TypeOptionPathPB.create()
|
||||
..viewId = viewId
|
||||
..fieldId = field.id
|
||||
@ -226,4 +218,7 @@ class FieldTypeOptionLoader extends IFieldTypeOptionLoader {
|
||||
|
||||
return DatabaseEventGetTypeOption(payload).send();
|
||||
}
|
||||
|
||||
@override
|
||||
String get fieldName => field.name;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
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:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-database2/field_entities.pb.dart';
|
||||
@ -12,7 +13,7 @@ import 'type_option_context.dart';
|
||||
class TypeOptionController {
|
||||
final String viewId;
|
||||
late TypeOptionPB _typeOption;
|
||||
final IFieldTypeOptionLoader loader;
|
||||
final ITypeOptionLoader loader;
|
||||
final PublishNotifier<FieldPB> _fieldNotifier = PublishNotifier();
|
||||
|
||||
/// Returns a [TypeOptionController] used to modify the specified
|
||||
@ -34,7 +35,7 @@ class TypeOptionController {
|
||||
}
|
||||
|
||||
Future<Either<TypeOptionPB, FlowyError>> loadTypeOptionData() async {
|
||||
final result = await loader.load();
|
||||
final result = await loader.initialize();
|
||||
return result.fold(
|
||||
(data) {
|
||||
data.freeze();
|
||||
@ -85,7 +86,12 @@ class TypeOptionController {
|
||||
}
|
||||
|
||||
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(
|
||||
(_) {
|
||||
// Should load the type-option data after switching to a new field.
|
||||
|
@ -38,7 +38,6 @@ class _GridFieldCellActionSheetState extends State<GridFieldCellActionSheet> {
|
||||
width: 400,
|
||||
child: FieldEditor(
|
||||
viewId: widget.cellContext.viewId,
|
||||
fieldName: field.name,
|
||||
typeOptionLoader: FieldTypeOptionLoader(
|
||||
viewId: widget.cellContext.viewId,
|
||||
field: field,
|
||||
|
@ -16,15 +16,13 @@ import 'field_type_option_editor.dart';
|
||||
|
||||
class FieldEditor extends StatefulWidget {
|
||||
final String viewId;
|
||||
final String fieldName;
|
||||
final bool isGroupingField;
|
||||
final Function(String)? onDeleted;
|
||||
final Function(String)? onHidden;
|
||||
final IFieldTypeOptionLoader typeOptionLoader;
|
||||
final ITypeOptionLoader typeOptionLoader;
|
||||
|
||||
const FieldEditor({
|
||||
required this.viewId,
|
||||
this.fieldName = "",
|
||||
required this.typeOptionLoader,
|
||||
this.isGroupingField = false,
|
||||
this.onDeleted,
|
||||
@ -63,7 +61,7 @@ class _FieldEditorState extends State<FieldEditor> {
|
||||
return BlocProvider(
|
||||
create: (context) => FieldEditorBloc(
|
||||
viewId: widget.viewId,
|
||||
fieldName: widget.fieldName,
|
||||
fieldName: widget.typeOptionLoader.fieldName,
|
||||
isGroupField: widget.isGroupingField,
|
||||
loader: widget.typeOptionLoader,
|
||||
)..add(const FieldEditorEvent.initial()),
|
||||
@ -156,6 +154,7 @@ class _FieldNameTextField extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _FieldNameTextFieldState extends State<_FieldNameTextField> {
|
||||
final textController = TextEditingController();
|
||||
FocusNode focusNode = FocusNode();
|
||||
|
||||
@override
|
||||
@ -180,6 +179,7 @@ class _FieldNameTextFieldState extends State<_FieldNameTextField> {
|
||||
return BlocListener<FieldEditorBloc, FieldEditorState>(
|
||||
listenWhen: (p, c) => p.field == none(),
|
||||
listener: (context, state) {
|
||||
textController.text = state.name;
|
||||
focusNode.requestFocus();
|
||||
},
|
||||
child: BlocBuilder<FieldEditorBloc, FieldEditorState>(
|
||||
@ -190,11 +190,10 @@ class _FieldNameTextFieldState extends State<_FieldNameTextField> {
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12.0),
|
||||
child: FlowyTextField(
|
||||
focusNode: focusNode,
|
||||
controller: textController,
|
||||
onSubmitted: (String _) => PopoverContainer.of(context).close(),
|
||||
text: context.read<FieldEditorBloc>().state.name,
|
||||
errorText: context.read<FieldEditorBloc>().state.errorText.isEmpty
|
||||
? null
|
||||
: context.read<FieldEditorBloc>().state.errorText,
|
||||
text: state.name,
|
||||
errorText: state.errorText.isEmpty ? null : state.errorText,
|
||||
onChanged: (newName) {
|
||||
context
|
||||
.read<FieldEditorBloc>()
|
||||
|
@ -143,7 +143,6 @@ class _GridPropertyCellState extends State<_GridPropertyCell> {
|
||||
popupBuilder: (BuildContext context) {
|
||||
return FieldEditor(
|
||||
viewId: widget.viewId,
|
||||
fieldName: widget.fieldInfo.name,
|
||||
typeOptionLoader: FieldTypeOptionLoader(
|
||||
viewId: widget.viewId,
|
||||
field: widget.fieldInfo.field,
|
||||
|
@ -307,7 +307,6 @@ class _PropertyCellState extends State<_PropertyCell> {
|
||||
Widget buildFieldEditor() {
|
||||
return FieldEditor(
|
||||
viewId: widget.cellId.viewId,
|
||||
fieldName: widget.cellId.fieldInfo.field.name,
|
||||
isGroupingField: widget.cellId.fieldInfo.isGroupField,
|
||||
typeOptionLoader: FieldTypeOptionLoader(
|
||||
viewId: widget.cellId.viewId,
|
||||
|
@ -85,7 +85,7 @@ class BoardTestContext {
|
||||
FieldEditorBloc createFieldEditor({
|
||||
FieldInfo? fieldInfo,
|
||||
}) {
|
||||
IFieldTypeOptionLoader loader;
|
||||
ITypeOptionLoader loader;
|
||||
if (fieldInfo == null) {
|
||||
loader = NewFieldTypeOptionLoader(viewId: gridView.id);
|
||||
} else {
|
||||
|
@ -42,7 +42,7 @@ class GridTestContext {
|
||||
FieldEditorBloc createFieldEditor({
|
||||
FieldInfo? fieldInfo,
|
||||
}) {
|
||||
IFieldTypeOptionLoader loader;
|
||||
ITypeOptionLoader loader;
|
||||
if (fieldInfo == null) {
|
||||
loader = NewFieldTypeOptionLoader(viewId: gridView.id);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user