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

Create, retrieve, display Stores #7

Merged
merged 1 commit into from
Jun 21, 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
11 changes: 9 additions & 2 deletions lib/db/database_manager.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:grocery_go/db/shopping_list_dto.dart';
import 'package:grocery_go/db/store_dto.dart';

class DatabaseManager {

final CollectionReference shoppingLists = Firestore.instance.collection('shopping_lists');
final CollectionReference stores = Firestore.instance.collection('stores');

Stream<QuerySnapshot> getShoppingListStream() {
return shoppingLists.snapshots();
return shoppingLists.orderBy("name").snapshots();
}

Stream<QuerySnapshot> getStoresStream() {
return stores.snapshots();
return stores.orderBy("name").snapshots();
}

Future<DocumentReference> addShoppingList(ShoppingListDTO shoppingList) async {
Expand All @@ -25,4 +26,10 @@ class DatabaseManager {
return shoppingLists.document(id).updateData(shoppingList.toJson());
} */

Future<DocumentReference> addStore(StoreDTO store) async {
DocumentReference docRef = await stores.add(store.toJson());
stores.document(docRef.documentID).updateData({'id':docRef.documentID});
return docRef;
}

}
18 changes: 18 additions & 0 deletions lib/db/store_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class StoreDTO {

String id;
String name;
String date;
String address;

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

Map<String, dynamic> toJson() => <String, dynamic> {
'id': this.id ?? '',
'name': this.name,
'date': this.date,
'address': this.address,
};
}
6 changes: 0 additions & 6 deletions lib/views/new_shopping_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,9 @@ class _AddShoppingListFormState extends State<AddShoppingListForm> {
final formState = formKey.currentState;
if (formState.validate()) {
formKey.currentState.save();

// this data is auto-generated when a new list is made
newShoppingListFields.date = DateTime.now().toString();
newShoppingListFields.itemIDs = new List<String>();

// put this stuff in the db and get the ID that was created
await db.addShoppingList(newShoppingListFields);

// confirm it with a snack bar
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('New list created: ' + newShoppingListFields.name))
);
Expand Down
115 changes: 82 additions & 33 deletions lib/views/new_store.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:grocery_go/db/database_manager.dart';
import 'package:grocery_go/db/store_dto.dart';

class NewStore extends StatefulWidget {

Expand All @@ -12,45 +14,92 @@ class NewStore extends StatefulWidget {

class _NewStoreState extends State<NewStore> {

void saveList(BuildContext context) {
print("Saving new store");
Navigator.of(context).pop();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add new store"),
appBar: AppBar(
title: Text("Add new store"),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(20),
child: AddStoreForm(),
),
body: Center(
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(10, 20, 10, 10),
child: TextFormField(
autofocus: true,
decoration: InputDecoration(
labelText: 'Store name',
border: OutlineInputBorder()),
),
),
Padding(
padding: EdgeInsets.all(10),
child: TextFormField(
autofocus: true,
decoration: InputDecoration(
labelText: 'Store location',
border: OutlineInputBorder()),
),
),
RaisedButton(
onPressed: () => saveList(context),
child: Text('Save store'),
),
],
),
);
}
}


class AddStoreForm extends StatefulWidget {
@override
_AddStoreFormState createState() => _AddStoreFormState();
}

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

final newStoreFields = StoreDTO();

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

void saveNewStore(BuildContext context) async {
final formState = formKey.currentState;
if (formState.validate()) {
formKey.currentState.save();
newStoreFields.date = DateTime.now().toString();
await db.addStore(newStoreFields);
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('New list created: ' + newStoreFields.name))
);
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,
decoration: InputDecoration(
labelText: 'Store name',
border: OutlineInputBorder()
),
validator: (value) => validateStringInput(value),
onSaved: (value) {
newStoreFields.name = value;
}
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
autofocus: false,
decoration: InputDecoration(
labelText: 'Location (optional)',
border: OutlineInputBorder()
),
onSaved: (value) {
newStoreFields.address = value;
}
),
),
RaisedButton(
onPressed: () => saveNewStore(context),
child: Text('Save'),
),
],
),
);
}
}