Skip to content

Instantly share code, notes, and snippets.

@jasonrdsouza
Last active July 7, 2018 15:30
Show Gist options
  • Save jasonrdsouza/4e3cce6e94a3a94edff417d9f329e0d1 to your computer and use it in GitHub Desktop.
Save jasonrdsouza/4e3cce6e94a3a94edff417d9f329e0d1 to your computer and use it in GitHub Desktop.
Grocery List App Brainstorm
/*
* The basic idea is to make a Grocery List progressive web app in Dartlang,
* using Firestore (https://firebase.google.com/docs/firestore/) as the backend.
* This pad is a quick sketch of the logic necessary to get the app working.
*/
enum Categories {
produce,
dairy,
household,
canned,
meat,
bakery,
frozen,
unknown
}
enum AmountType {
oz, // ounces/ fluid ounces
lb, // pounds
cup, // cups
quart, // quarts
num, // count
pkg, // normal sized package
ml, // milliliters
gram, // grams
unknown // no good existing amount type
}
class GroceryItem {
String name;
int amount;
AmountType amountType;
GroceryItem(String name, int amount, AmountType amountType) {
this.name = name.toLowerCase();
this.amount = amount;
this.amountType = amountType;
}
GroceryItem.fromString(String description) {
List<String> parts = description.split(" ");
this.amount = int.parse(parts[0]);
this.amountType = fromAmountTypeString(parts[1]);
this.name = parts.sublist(2).join(" ").toLowerCase();
}
AmountType fromAmountTypeString(String amountTypePart) {
return AmountType.values.firstWhere(
(a) => a.toString().substring(a.toString().indexOf('.')+1) == amountTypePart.toLowerCase(),
orElse: () => AmountType.unknown
);
}
String toString() {
return "{ Amount: ${this.amount}, Type: ${this.amountType}, Name: ${this.name} }";
}
}
// Instances of this class comprise the main DB collection, which allows for the core
// functionality of creating and updating grocery lists
class GroceryList {
DateTime datetime;
int totalCost;
List<GroceryItem> list;
GroceryList(this.datetime, this.totalCost, this.list);
GroceryList.newList() {
this.datetime = DateTime.now();
this.totalCost = 0;
this.list = [];
}
void addItem(GroceryItem item) {
list.add(item);
}
List<GroceryItem> items() {
return list;
}
}
class GroceryCategory {
String normalizedName;
Categories category;
int priority;
GroceryCategory(this.normalizedName, this.category, this.priority);
GroceryCategory.unknown(String name) {
normalizedName = name;
category = Categories.unknown;
priority = 0;
}
}
// Instances of this class comprise the secondary DB collection, which allows for
// grocery list ordering based on the categorizations and prioritizations of
// individual groceries.
class GroceryCategorizer {
String normalize(String groceryName) {
// remove pluralization?
// fix spacing?
// deal with apostrophes, dashes, etc.
// how much normalization to we want to do at the item level,
// and how much do we want to do internally (hidden from the user)
return groceryName.toLowerCase();
}
Map<String, GroceryCategory> groceryCategories; // populate with the contents from the db
GroceryCategory categorize(String groceryName) {
String normalizedGroceryName = normalize(groceryName);
if (groceryCategories.containsKey(normalizedGroceryName)) {
return groceryCategories[normalizedGroceryName];
} else {
return GroceryCategory.unknown(normalizedGroceryName);
}
}
}
void main() {
GroceryList groceryList = GroceryList.newList();
groceryList.addItem(GroceryItem.fromString("3 num bananas"));
groceryList.addItem(GroceryItem.fromString("16 oz cream"));
groceryList.addItem(GroceryItem.fromString("2 lb chicken"));
groceryList.addItem(GroceryItem.fromString("2 pkg hot dogs"));
for (GroceryItem item in groceryList.items()) {
print(item);
}
}
@jasonrdsouza
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment