sentry-demos / ios

iOS Empower Plant
1 stars 4 forks source link

Shopping Cart Data Structure #4

Closed thinkocapo closed 2 years ago

thinkocapo commented 2 years ago

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 image

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

image

Shopping Cart

image

log of Total being computed, and it saves to the ShoppingCart.swift singleton image

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

    /*
     // how it works in application-monitoring-javascript
     body: JSON.stringify({
         cart: cart,
         form: this.state
     })
     */

can try building JSON like this:

// EXAMPLE
let jsonObject: [String: Any] = [
            "type_id": 1,
            "model_id": 1,
            "transfer": [
                "startDate": "10/04/2015 12:45",
                "endDate": "10/04/2015 16:00"
            ],
            "custom": "savedData"
        ]

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....

// 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

and

self.instance.quantities.setValue(0, forKey: "key")
self.instance.quantities['prop1'] = 0
self.instance.quantities[product.productId] = self.instance.quantities[product.productId] || 0
self.instance.quantities["prop1"] = 0 // struct

https://stackoverflow.com/questions/26667380/in-swift-for-anyobject-how-do-i-setvalue-then-call-valueforkey