Skip to content

Commit

Permalink
Merge pull request #208 from autodo-app/200
Browse files Browse the repository at this point in the history
Fix overwritten data when adding multiple cars in setup screens
  • Loading branch information
baylessj authored Jan 26, 2020
2 parents d1dfe55 + 19e4fb6 commit 339bebc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
1 change: 1 addition & 0 deletions lib/blocs/src/repeating/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class RepeatsBloc extends Bloc<RepeatsEvent, RepeatsState> {
List<Car> newCars = event.cars
.map((c) => (curRepeats.any((r) => r.cars.contains(c.name)) ? null : c))
.toList();
newCars.removeWhere((c) => c == null);
if (newCars.length == 0) {
print('all cars have repeats, not adding defaults');
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/blocs/src/repeating/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class RepeatsLoaded extends RepeatsState {
List<Object> get props => repeats;

@override
String toString() => 'RepeatsLoaded { repeats: $repeats }';
String toString() =>
'RepeatsLoaded { repeats: ${repeats.map((r) => "{${r.name}, ${r.cars}}")} }';
}

class RepeatsNotLoaded extends RepeatsState {}
78 changes: 48 additions & 30 deletions lib/screens/welcome/views/new_user_setup/mileage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import 'base.dart';
class CarEntryField extends StatefulWidget {
final Function next;
final Function onNameSaved, onMileageSaved;
final GlobalKey<FormState> formKey;

CarEntryField(this.next, this.onNameSaved, this.onMileageSaved);
CarEntryField(this.next, this.onNameSaved, this.onMileageSaved, this.formKey);

@override
State<CarEntryField> createState() =>
CarEntryFieldState(next, onNameSaved, onMileageSaved);
CarEntryFieldState(next, onNameSaved, onMileageSaved, formKey);
}

class CarEntryFieldState extends State<CarEntryField> {
bool firstWritten = false;
FocusNode _nameNode, _mileageNode;
Function nextNode;
final Function onNameSaved, onMileageSaved;
final GlobalKey<FormState> formKey;

CarEntryFieldState(this.nextNode, this.onNameSaved, this.onMileageSaved);
CarEntryFieldState(
this.nextNode, this.onNameSaved, this.onMileageSaved, this.formKey);

@override
initState() {
Expand Down Expand Up @@ -71,24 +74,27 @@ class CarEntryFieldState extends State<CarEntryField> {

return Container(
padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
child: nameField(),
child: Form(
key: formKey,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
child: nameField(),
),
),
),
Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
child: mileageField(),
Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
child: mileageField(),
),
),
),
],
],
),
),
);
}
Expand All @@ -107,18 +113,24 @@ class MileageScreen extends StatefulWidget {

class MileageScreenState extends State<MileageScreen> {
var mileageEntry;
static final emptyCar = {'name': '', 'mileage': 0};
List<Map<String, dynamic>> cars = [emptyCar];
List<GlobalKey<FormState>> formKeys = [GlobalKey<FormState>()];
List<Car> cars = [Car()];

MileageScreenState(this.mileageEntry);

_next() async {
if (widget.mileageKey.currentState.validate()) {
widget.mileageKey.currentState.save();
for (var c in cars) {
BlocProvider.of<CarsBloc>(context)
.add(AddCar(Car(name: c['name'], mileage: c['mileage'])));
bool allValidated = true;
formKeys.forEach((k) {
if (k.currentState.validate()) {
k.currentState.save();
} else {
allValidated = false;
}
});
if (allValidated) {
cars.forEach((c) {
BlocProvider.of<CarsBloc>(context).add(AddCar(c));
});
// hide the keyboard
FocusScope.of(context).requestFocus(FocusNode());
await Future.delayed(Duration(milliseconds: 400));
Expand Down Expand Up @@ -156,9 +168,12 @@ class MileageScreenState extends State<MileageScreen> {

Widget card() {
List<Widget> carFields = [];
for (var car in cars) {
carFields.add(CarEntryField(_next, (val) => car['name'] = val,
(val) => car['mileage'] = int.parse(val)));
for (var i in Iterable.generate(cars.length)) {
carFields.add(CarEntryField((i == cars.length - 1) ? _next : null,
(val) {
cars[i] = cars[i].copyWith(name: val);
}, (val) => cars[i] = cars[i].copyWith(mileage: int.parse(val)),
formKeys[i]));
}

return Container(
Expand All @@ -180,7 +195,10 @@ class MileageScreenState extends State<MileageScreen> {
padding: EdgeInsets.all(0),
icon: Icon(Icons.add),
label: Text('Add'),
onPressed: () => setState(() => cars.add(emptyCar)),
onPressed: () => setState(() {
cars.add(Car());
formKeys.add(GlobalKey<FormState>());
}),
),
FlatButton.icon(
padding: EdgeInsets.all(0),
Expand Down

0 comments on commit 339bebc

Please sign in to comment.