Skip to content

Commit 818bb10

Browse files
committed
fix: allow new preparation row reorder and delete
1 parent 6facc59 commit 818bb10

5 files changed

Lines changed: 160 additions & 103 deletions

File tree

lib/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/bloc/preparation_form_bloc.dart

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PreparationFormBloc
5757
state.copyWith(
5858
status: PreparationFormStatus.initial,
5959
preparationStepList: preparationFormState.preparationStepList,
60-
draftStep: null,
60+
clearAddingStepId: true,
6161
showValidationErrors: false,
6262
isValid: isValid,
6363
),
@@ -83,7 +83,7 @@ class PreparationFormBloc
8383
state.copyWith(
8484
preparationStepList: preparationStepList,
8585
status: PreparationFormStatus.initial,
86-
draftStep: null,
86+
clearAddingStepId: true,
8787
isValid: isValid,
8888
),
8989
);
@@ -103,11 +103,16 @@ class PreparationFormBloc
103103
);
104104
removedList.removeWhere((element) => element.id == event.preparationStepId);
105105

106-
final isValid = _validate([
107-
...removedList,
108-
if (state.draftStep != null) state.draftStep!,
109-
]);
110-
emit(state.copyWith(preparationStepList: removedList, isValid: isValid));
106+
final isValid = _validate(removedList);
107+
final removedAddingStep = state.addingStepId == event.preparationStepId;
108+
emit(
109+
state.copyWith(
110+
preparationStepList: removedList,
111+
status: removedAddingStep ? PreparationFormStatus.initial : null,
112+
clearAddingStepId: removedAddingStep,
113+
isValid: isValid,
114+
),
115+
);
111116
}
112117

113118
void _onPreparationFormPreparationStepNameChanged(
@@ -123,11 +128,18 @@ class PreparationFormBloc
123128
),
124129
);
125130

126-
final isValid = _validate([
127-
...changedList,
128-
if (state.draftStep != null) state.draftStep!,
129-
]);
130-
emit(state.copyWith(preparationStepList: changedList, isValid: isValid));
131+
final isValid = _validate(changedList);
132+
final shouldCommitAddingStep = _shouldCommitAddingStep(
133+
changedList[event.index],
134+
);
135+
emit(
136+
state.copyWith(
137+
preparationStepList: changedList,
138+
status: shouldCommitAddingStep ? PreparationFormStatus.initial : null,
139+
clearAddingStepId: shouldCommitAddingStep,
140+
isValid: isValid,
141+
),
142+
);
131143
}
132144

133145
void _onPreparationFormPreparationStepTimeChanged(
@@ -142,39 +154,62 @@ class PreparationFormBloc
142154
event.preparationStepTime,
143155
),
144156
);
145-
final isValid = _validate([
146-
...changedList,
147-
if (state.draftStep != null) state.draftStep!,
148-
]);
149-
emit(state.copyWith(preparationStepList: changedList, isValid: isValid));
157+
final isValid = _validate(changedList);
158+
final shouldCommitAddingStep = _shouldCommitAddingStep(
159+
changedList[event.index],
160+
);
161+
emit(
162+
state.copyWith(
163+
preparationStepList: changedList,
164+
status: shouldCommitAddingStep ? PreparationFormStatus.initial : null,
165+
clearAddingStepId: shouldCommitAddingStep,
166+
isValid: isValid,
167+
),
168+
);
169+
}
170+
171+
bool _shouldCommitAddingStep(PreparationStepFormState step) {
172+
return step.id == state.addingStepId &&
173+
step.preparationName.isValid &&
174+
step.preparationTime.isValid;
150175
}
151176

152177
void _onPreparationFormDraftStepNameChanged(
153178
PreparationFormDraftStepNameChanged event,
154179
Emitter<PreparationFormState> emit,
155180
) {
156-
final draftStep = state.draftStep ?? PreparationStepFormState();
157-
final changedDraft = draftStep.copyWith(
158-
preparationName: PreparationNameInputModel.dirty(
159-
event.preparationStepName,
181+
final draftIndex = state.preparationStepList.indexWhere(
182+
(step) => step.id == state.addingStepId,
183+
);
184+
if (draftIndex == -1) {
185+
return;
186+
}
187+
188+
add(
189+
PreparationFormPreparationStepNameChanged(
190+
index: draftIndex,
191+
preparationStepName: event.preparationStepName,
160192
),
161193
);
162-
final isValid = _validate([...state.preparationStepList, changedDraft]);
163-
emit(state.copyWith(draftStep: changedDraft, isValid: isValid));
164194
}
165195

166196
void _onPreparationFormDraftStepTimeChanged(
167197
PreparationFormDraftStepTimeChanged event,
168198
Emitter<PreparationFormState> emit,
169199
) {
170-
final draftStep = state.draftStep ?? PreparationStepFormState();
171-
final changedDraft = draftStep.copyWith(
172-
preparationTime: PreparationTimeInputModel.dirty(
173-
event.preparationStepTime,
200+
final draftIndex = state.preparationStepList.indexWhere(
201+
(step) => step.id == state.addingStepId,
202+
);
203+
if (draftIndex == -1) {
204+
return;
205+
}
206+
207+
add(
208+
PreparationFormPreparationStepTimeChanged(
209+
index: draftIndex,
210+
preparationStepTime: event.preparationStepTime,
174211
),
175212
);
176-
final isValid = _validate([...state.preparationStepList, changedDraft]);
177-
emit(state.copyWith(draftStep: changedDraft, isValid: isValid));
178213
}
179214

180215
void _onPreparationFormPreparationStepOrderChanged(
@@ -193,10 +228,7 @@ class PreparationFormBloc
193228
final item = changedList.removeAt(oldIndex);
194229
changedList.insert(newIndex, item);
195230

196-
final isValid = _validate([
197-
...changedList,
198-
if (state.draftStep != null) state.draftStep!,
199-
]);
231+
final isValid = _validate(changedList);
200232
emit(state.copyWith(preparationStepList: changedList, isValid: isValid));
201233
}
202234

@@ -221,12 +253,14 @@ class PreparationFormBloc
221253
return;
222254
}
223255

224-
final draftStep = PreparationStepFormState();
225-
final isValid = _validate([...state.preparationStepList, draftStep]);
256+
final addedStep = PreparationStepFormState();
257+
final changedList = [...state.preparationStepList, addedStep];
258+
final isValid = _validate(changedList);
226259
emit(
227260
state.copyWith(
228261
status: PreparationFormStatus.adding,
229-
draftStep: draftStep,
262+
preparationStepList: changedList,
263+
addingStepId: addedStep.id,
230264
isValid: isValid,
231265
),
232266
);

lib/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/bloc/preparation_form_state.dart

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ enum PreparationFormStatus { initial, success, adding }
44

55
enum PreparationFormInvalidField { name, time }
66

7-
const _draftStepNoChange = Object();
8-
97
final class PreparationFormState extends Equatable {
108
const PreparationFormState({
119
this.status = PreparationFormStatus.initial,
1210
this.preparationStepList = const [],
13-
this.draftStep,
11+
this.addingStepId,
1412
this.showValidationErrors = false,
1513
this.isValid = false,
1614
});
@@ -64,14 +62,12 @@ final class PreparationFormState extends Equatable {
6462

6563
final PreparationFormStatus status;
6664
final List<PreparationStepFormState> preparationStepList;
67-
final PreparationStepFormState? draftStep;
65+
final String? addingStepId;
6866
final bool showValidationErrors;
6967
final bool isValid;
7068

71-
List<PreparationStepFormState> get visiblePreparationStepList => [
72-
...preparationStepList,
73-
if (draftStep != null) draftStep!,
74-
];
69+
List<PreparationStepFormState> get visiblePreparationStepList =>
70+
preparationStepList;
7571

7672
PreparationStepFormState? get firstInvalidStep => visiblePreparationStepList
7773
.firstWhereOrNull((step) => invalidFieldFor(step) != null);
@@ -89,16 +85,17 @@ final class PreparationFormState extends Equatable {
8985
PreparationFormState copyWith({
9086
PreparationFormStatus? status,
9187
List<PreparationStepFormState>? preparationStepList,
92-
Object? draftStep = _draftStepNoChange,
88+
String? addingStepId,
89+
bool clearAddingStepId = false,
9390
bool? showValidationErrors,
9491
bool? isValid,
9592
}) {
9693
return PreparationFormState(
9794
status: status ?? this.status,
9895
preparationStepList: preparationStepList ?? this.preparationStepList,
99-
draftStep: identical(draftStep, _draftStepNoChange)
100-
? this.draftStep
101-
: draftStep as PreparationStepFormState?,
96+
addingStepId: clearAddingStepId
97+
? null
98+
: addingStepId ?? this.addingStepId,
10299
showValidationErrors: showValidationErrors ?? this.showValidationErrors,
103100
isValid: isValid ?? this.isValid,
104101
);
@@ -108,7 +105,7 @@ final class PreparationFormState extends Equatable {
108105
List<Object?> get props => [
109106
status,
110107
preparationStepList,
111-
draftStep,
108+
addingStepId,
112109
showValidationErrors,
113110
isValid,
114111
];

lib/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/components/preparation_form_create_list.dart

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_bloc/flutter_bloc.dart';
33
import 'package:on_time_front/presentation/onboarding/preparation_name_select/components/create_icon_button.dart';
44
import 'package:on_time_front/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/bloc/preparation_form_bloc.dart';
5-
import 'package:on_time_front/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/components/preparation_form_list_field.dart';
65
import 'package:on_time_front/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/components/preparation_form_reorderable_list.dart';
76

87
class PreparationFormCreateList extends StatelessWidget {
@@ -32,6 +31,7 @@ class PreparationFormCreateList extends StatelessWidget {
3231
children: [
3332
PreparationFormReorderableList(
3433
preparationStepList: preparationNameState.preparationStepList,
34+
addingStepId: preparationNameState.addingStepId,
3535
showValidationErrors: preparationNameState.showValidationErrors,
3636
stepKeyFor: stepKeyFor,
3737
nameFocusNodeFor: nameFocusNodeFor,
@@ -53,53 +53,6 @@ class PreparationFormCreateList extends StatelessWidget {
5353
),
5454
),
5555
),
56-
preparationNameState.status == PreparationFormStatus.adding
57-
? PreparationFormListField(
58-
key:
59-
stepKeyFor?.call(preparationNameState.draftStep!.id) ??
60-
ValueKey<String>(
61-
'draft_${preparationNameState.draftStep!.id}',
62-
),
63-
isAdding: true,
64-
showValidationErrors:
65-
preparationNameState.showValidationErrors,
66-
focusNode: nameFocusNodeFor?.call(
67-
preparationNameState.draftStep!.id,
68-
),
69-
preparationStep: preparationNameState.draftStep!,
70-
onNameChanged: (value) {
71-
context.read<PreparationFormBloc>().add(
72-
PreparationFormDraftStepNameChanged(
73-
preparationStepName: value,
74-
),
75-
);
76-
},
77-
onNameFocusLost: (value) {
78-
context.read<PreparationFormBloc>().add(
79-
PreparationFormDraftStepNameChanged(
80-
preparationStepName: value,
81-
),
82-
);
83-
},
84-
onPreparationTimeTapped: () {
85-
context.read<PreparationFormBloc>().add(
86-
PreparationFormDraftStepTimeChanged(
87-
preparationStepTime: preparationNameState
88-
.draftStep!
89-
.preparationTime
90-
.value,
91-
),
92-
);
93-
},
94-
onPreparationTimeChanged: (value) {
95-
context.read<PreparationFormBloc>().add(
96-
PreparationFormDraftStepTimeChanged(
97-
preparationStepTime: value,
98-
),
99-
);
100-
},
101-
)
102-
: SizedBox.shrink(),
10356
SizedBox(height: 28.0),
10457
Center(
10558
child: SizedBox(

lib/presentation/schedule_create/schedule_spare_and_preparing_time/preparation_form/components/preparation_form_reorderable_list.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class PreparationFormReorderableList extends StatelessWidget {
2828
const PreparationFormReorderableList({
2929
super.key,
3030
required this.preparationStepList,
31+
required this.addingStepId,
3132
required this.showValidationErrors,
3233
required this.stepKeyFor,
3334
required this.nameFocusNodeFor,
@@ -37,6 +38,7 @@ class PreparationFormReorderableList extends StatelessWidget {
3738
});
3839

3940
final List<PreparationStepFormState> preparationStepList;
41+
final String? addingStepId;
4042
final bool showValidationErrors;
4143
final Key Function(String stepId)? stepKeyFor;
4244
final FocusNode Function(String stepId)? nameFocusNodeFor;
@@ -97,6 +99,7 @@ class PreparationFormReorderableList extends StatelessWidget {
9799
stepKeyFor?.call(step.id) ??
98100
ValueKey<String>('field_${step.id}'),
99101
index: index,
102+
isAdding: step.id == addingStepId,
100103
showValidationErrors: showValidationErrors,
101104
focusNode: nameFocusNodeFor?.call(step.id),
102105
preparationStep: step,

0 commit comments

Comments
 (0)