You can now click on product items and they get added into the shopping cart data structure (see the console logs in XCode). The Shopping Cart UI itself is still not yet built, but that should be easy. It's just another table.
Features
Singleton class ShoppingCart.swift as the datamodel/database for shopping cart data, implemented, and its data (static variables) can be updated and accessed from any controller.
TableView click handler for selecting a row (i.e. adding item to shopping cart). followed this tutorial and updated method names according to IDE's warnings+suggestions.
Data Structure for shopping cart, borrowed from the application-monitoring-javascript demo
Did not add methods for Removing items from Shopping Cart yet. You can restart the app for now. We also don't capture item deletions as button clicks in breadcrumbs yet (our tests don't do that).
Enhancements
Product Entity model updated with remaining attributes
utility function for resetting database
products only write to database if database is empty. but http request + db query still run every time (for the spans in the transaction)
// These failed
var quantities = {}
class quantities: NSObject {}
var quantities: NSObject = {}
struct quantities {}
var quantities = {
"1": 0;
"2": 0;
}
struct quantities {
var one = 0
var two = 0
}
https://stackoverflow.com/questions/31145990/dynamically-create-objects-and-set-attributes-in-swift.
https://stackoverflow.com/questions/38121957/singleton-and-class-properties-in-swift
Overview
You can now click on product items and they get added into the shopping cart data structure (see the console logs in XCode). The Shopping Cart UI itself is still not yet built, but that should be easy. It's just another table.
Features
Data Structure for shopping cart, borrowed from the application-monitoring-javascript demo
Did not add methods for Removing items from Shopping Cart yet. You can restart the app for now. We also don't capture item deletions as button clicks in breadcrumbs yet (our tests don't do that).
Enhancements
Testing
EmpowerPlantViewController transaction CartViewController transaction
Empower Plant home screen
Shopping Cart
log of Total being computed, and it saves to the ShoppingCart.swift singleton
Next
Add 'TOTAL' price to the Shopping Cart UI screen.
A checkout form (optional, some mobile demo's don't have it) for name/address/phone.
Add a Purchase button.
For checkout (Purchase), will need put these on a 'cart' property on req body, may need a JSON object for that, which has Id's again
can try building JSON like this:
Technical Notes
Data persistence options for Shopping Cart...
ModelController - Too much boilerplate...https://code.tutsplus.com/tutorials/the-right-way-to-share-state-between-swift-view-controllers--cms-28474
Data Container - Too much boilerplate...
Singleton Pattern - Easy, https://betterprogramming.pub/5-ways-to-pass-data-between-view-controllers-18acb467f5ec class Settings
File I/O - Gets us a span for the transaction but is overkill for a ShoppingCart. We can add a File I/O example elsewhere.
CoreData - Easy (already using)
Segue - Going back and forth between controllers, could present problems with persistence. Sounds like it's intended more for one-way data flow https://levelup.gitconnected.com/swift-xcode-sharing-data-between-view-controllers-8d270e99ca1e
Also....
and
https://stackoverflow.com/questions/26667380/in-swift-for-anyobject-how-do-i-setvalue-then-call-valueforkey