Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ShoppingList and Store - create and update now use the same form #10

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/components/item_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ItemList extends StatelessWidget {

int getCount(item) {
if (listType == 'shopping list') {
return item.itemIDs.length;
return item.itemIDs?.length;
} else {
return list.length;
}
Expand Down
1 change: 0 additions & 1 deletion lib/db/database_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ class DatabaseManager {
});
return docRef;
}

}
81 changes: 81 additions & 0 deletions lib/forms/shopping_list_form.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'package:flutter/material.dart';
import 'package:grocery_go/db/database_manager.dart';
import 'package:grocery_go/db/shopping_list_dto.dart';

class ShoppingListForm extends StatefulWidget {
final args;

ShoppingListForm({this.args});

@override
_ShoppingListFormState createState() => _ShoppingListFormState(args);
}

class _ShoppingListFormState extends State<ShoppingListForm> {

final args;
_ShoppingListFormState(this.args);

final formKey = GlobalKey<FormState>();
final DatabaseManager db = DatabaseManager();

final shoppingListFields = ShoppingListDTO();

String validateStringInput(String value) {
if (value.isEmpty) {
return 'Please enter a name';
} else return null;
}

void updateShoppingList(BuildContext context) async {
final formState = formKey.currentState;

if (formState.validate()) {
formKey.currentState.save();
shoppingListFields.date = DateTime.now().toString();

if (args != null) {
// preserve existing data from args
shoppingListFields.itemIDs = args.list.itemIDs;
shoppingListFields.id = args.list.id;
await db.updateShoppingList(args.list.id, shoppingListFields);
} else {
shoppingListFields.itemIDs = new List<String>();
await db.addShoppingList(shoppingListFields);
}

Navigator.of(context).pop();
}
}

@override
Widget build(BuildContext context) {

return Form(
key: formKey,
child: Column(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
autofocus: true,
initialValue: (args?.list?.name),
decoration: InputDecoration(
labelText: 'List name',
border: OutlineInputBorder()
),
validator: (value) => validateStringInput(value),
onSaved: (value) {
shoppingListFields.name = value;
}
),
),
RaisedButton(
onPressed: () => updateShoppingList(context),
child: Text('Save shopping list'),
),
],
),
);
}
}
93 changes: 93 additions & 0 deletions lib/forms/store_form.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'package:flutter/material.dart';
import 'package:grocery_go/db/database_manager.dart';
import 'package:grocery_go/db/store_dto.dart';

class StoreForm extends StatefulWidget {
final args;

StoreForm({this.args});

@override
_StoreFormState createState() => _StoreFormState(args);
}

class _StoreFormState extends State<StoreForm> {

final args;
_StoreFormState(this.args);

final formKey = GlobalKey<FormState>();
final DatabaseManager db = DatabaseManager();

final storeFields = StoreDTO();

String validateStringInput(String value) {
if (value.isEmpty) {
return 'Please enter a name';
} else return null;
}

void updateStore(BuildContext context) async {
final formState = formKey.currentState;

if (formState.validate()) {
formKey.currentState.save();
storeFields.date = DateTime.now().toString();

if (args != null) {
storeFields.id = args.store.id;
await db.updateStore(args.store.id, storeFields);
} else {
await db.addStore(storeFields);
}

Navigator.of(context).pop();
}
}

@override
Widget build(BuildContext context) {

return Form(
key: formKey,
child: Column(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
autofocus: true,
initialValue: (args?.store?.name),
decoration: InputDecoration(
labelText: 'Store name',
border: OutlineInputBorder()
),
validator: (value) => validateStringInput(value),
onSaved: (value) {
storeFields.name = value;
}
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
autofocus: false,
initialValue: (args?.store?.address),
decoration: InputDecoration(
labelText: 'Location (optional)',
border: OutlineInputBorder()
),
validator: (value) => validateStringInput(value),
onSaved: (value) {
storeFields.address = value;
}
),
),
RaisedButton(
onPressed: () => updateStore(context),
child: Text('Save store'),
),
],
),
);
}
}
72 changes: 5 additions & 67 deletions lib/views/existing_list.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:grocery_go/db/database_manager.dart';
import 'package:grocery_go/db/shopping_list_dto.dart';
import 'package:grocery_go/forms/shopping_list_form.dart';
import 'package:grocery_go/models/shopping_list.dart';

class ExistingListArguments {
Expand All @@ -19,80 +18,19 @@ class ExistingList extends StatefulWidget {
class _ExistingListState extends State<ExistingList> {
@override
Widget build(BuildContext context) {

final ExistingListArguments args = ModalRoute.of(context).settings.arguments;

return Scaffold(
appBar: AppBar(
title: Text("Edit shopping list"),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(20),
child: UpdateShoppingListForm(),
child: ShoppingListForm(args: args),
),
),
);
}
}

class UpdateShoppingListForm extends StatefulWidget {
@override
_UpdateShoppingListFormState createState() => _UpdateShoppingListFormState();
}

class _UpdateShoppingListFormState extends State<UpdateShoppingListForm> {
final formKey = GlobalKey<FormState>();
final DatabaseManager db = DatabaseManager();

final updateShoppingListFields = ShoppingListDTO();

String validateStringInput(String value) {
if (value.isEmpty) {
return 'Please enter a name';
} else return null;
}

void updateList(BuildContext context, list) async {
final formState = formKey.currentState;

if (formState.validate()) {
formKey.currentState.save();

// take the existing data
updateShoppingListFields.id = list.id;
updateShoppingListFields.itemIDs = list.itemIDs;
// update the last edited date
updateShoppingListFields.date = DateTime.now().toString();
await db.updateShoppingList(list.id, updateShoppingListFields);
Navigator.of(context).pop();
}
}

@override
Widget build(BuildContext context) {

final ExistingListArguments args = ModalRoute.of(context).settings.arguments;

return Form(
key: formKey,
child: Column(
children: [
TextFormField(
autofocus: true,
initialValue: args.list.name,
decoration: InputDecoration(
labelText: 'List name',
border: OutlineInputBorder()
),
validator: (value) => validateStringInput(value),
onSaved: (value) {
updateShoppingListFields.name = value;
}
),
RaisedButton(
onPressed: () => updateList(context, args.list),
child: Text('Update list'),
),
],
),
);
}
}
Loading