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

Update the name of an existing shopping list #8

Merged
merged 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/components/list_item.dart
Expand Up @@ -10,7 +10,7 @@ class ListItem extends StatelessWidget {
final onTap;
final onInfoTap;

ListItem({Key key, @required this.item, this.listType, this.count, this.onTap, this.onInfoTap});
ListItem({Key key, this.item: "Unknown", this.listType, this.count, this.onTap, this.onInfoTap});

buildDateString(date) {
return DateFormat.yMMMd().format(DateTime.parse(date));
Expand All @@ -26,7 +26,7 @@ class ListItem extends StatelessWidget {
buildTitleString() {
if (listType == 'item') {
return item.name + (item.quantity > 1 ? ' (' + item.quantity.toString() + ')' : '');
} else { // crossed off, shopping list, store
} else { // crossed off, shopping list, store
return item.name;
}
}
Expand Down
13 changes: 8 additions & 5 deletions lib/db/database_manager.dart
Expand Up @@ -20,11 +20,14 @@ class DatabaseManager {
shoppingLists.document(docRef.documentID).updateData({'id':docRef.documentID});
return docRef;
}

/*
Future<void> updateShoppingList(String id, ShoppingListDTO shoppingList) {
return shoppingLists.document(id).updateData(shoppingList.toJson());
} */

Future<DocumentReference> updateShoppingList(String id, ShoppingListDTO shoppingList) async {
DocumentReference docRef = shoppingLists.document(id);
Firestore.instance.runTransaction((transaction) async {
await transaction.update(docRef, shoppingList.toJson());
});
return docRef;
}

Future<DocumentReference> addStore(StoreDTO store) async {
DocumentReference docRef = await stores.add(store.toJson());
Expand Down
2 changes: 1 addition & 1 deletion lib/db/shopping_list_dto.dart
Expand Up @@ -6,7 +6,7 @@ class ShoppingListDTO {
List itemIDs;

String toString() {
return 'id: $id, name: $name, date: $date, storeIDs: $itemIDs';
return 'id: $id, name: $name, date: $date, itemIDs: $itemIDs';
}

Map<String, dynamic> toJson() => <String, dynamic> {
Expand Down
103 changes: 76 additions & 27 deletions lib/views/existing_list.dart
@@ -1,49 +1,98 @@
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/models/shopping_list.dart';

class ExistingListArguments {
final ShoppingList list;
ExistingListArguments(this.list);
}

class ExistingList extends StatelessWidget {

class ExistingList extends StatefulWidget {
static const routeName = '/existingList';
ExistingList({Key key});

void updateList (BuildContext context) {
print("Updating list");
Navigator.of(context).pop();
}
@override
_ExistingListState createState() => _ExistingListState();
}

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 list: " + args.list.name),
title: Text("Edit shopping list"),
),
body: Center(
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(10, 20, 10, 10),
child: TextFormField(
autofocus: true,
initialValue: args.list.name,
decoration: InputDecoration(
labelText: 'List name',
border: OutlineInputBorder()),
),
),
RaisedButton(
onPressed: () => updateList(context),
child: Text('Save list'),
),
],
child: Padding(
padding: EdgeInsets.all(20),
child: UpdateShoppingListForm(),
),
),
);
}
}

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'),
),
],
),
);
}
}