Skip to content

Commit 77baf7d

Browse files
CypherPoetCypherPoet
CypherPoet
authored and
CypherPoet
committed
Complete Day 51
1 parent 6778c73 commit 77baf7d

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ Cheers! ✌️
5353
- **Day 47:** [Milestone for Projects 7-9](./day-047/)
5454
- **Day 48:** [What Star Wars can Teach Us About Swift](./day-048/)
5555
- **Day 49:** [_Project 10: CupcakeCorner_ (Part One)](./day-049/)
56+
- **Day 50:** [_Project 10: CupcakeCorner_ (Part Two)](./day-050/)
5657

5758
</details>
5859

59-
- **Day 50:** [_Project 10: CupcakeCorner_ (Part Two)](./day-050/)
60+
- **Day 51:** [_Project 10: CupcakeCorner_ (Part Three)](./day-051/)
6061

6162

6263

day-049/Projects/CupcakeCorner/CupcakeCorner/Scenes/Order Form/OrderFormContainerViewModel.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ final class OrderFormContainerViewModel: ObservableObject {
1818

1919

2020
// MARK: - Published Properties
21-
@Published var isShowingSaveConfirmationAlert = false
22-
@Published var isShowingSaveErrorAlert = false
2321
@Published var isShowingAlert = false
2422

2523

@@ -38,7 +36,7 @@ extension OrderFormContainerViewModel {
3836
private var ordersState: OrdersState { store.state.ordersState }
3937

4038
private var currentAlertContent: (title: String, message: String) {
41-
switch (ordersState.currentOrder, ordersState.saveError) {
39+
switch (ordersState.successfullySavedOrder, ordersState.saveError) {
4240
case (let order?, .none):
4341
let cupcakesString = order.quantity == 1 ? "cupcake" : "cupcakes"
4442

Loading
Binary file not shown.

day-051/README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Day 51: _Project 10: CupcakeCorner_ (Part Three)
2+
3+
_Follow along at https://www.hackingwithswift.com/100/swiftui/51_.
4+
5+
<br/>
6+
7+
8+
# 📒 Field Notes
9+
10+
This day covers Part Three of _`Project 10: CupcakeCorner`_ in the [100 Days of SwiftUI Challenge](https://www.hackingwithswift.com/100/swiftui/51). (Project 10 files can be found in the [directory for Part One](../day-049/).)
11+
12+
It focuses on several specific topics:
13+
14+
- Encoding an ObservableObject class
15+
- Sending and receiving orders over the internet
16+
17+
18+
I took the opportunity with this day to explore how I could fit async networking side effects into a redux/reducer architecture.
19+
20+
Ultimately, this led to creating another view model `ObservableObject` for the container view -- which handled tapping in to the app state and detecting whether or not an alert should be shown.
21+
22+
23+
```swift
24+
25+
final class OrderFormContainerViewModel: ObservableObject {
26+
private var subscriptions = Set<AnyCancellable>()
27+
28+
var store: AppStore
29+
30+
31+
// MARK: - Published Properties
32+
@Published var isShowingAlert = false
33+
34+
35+
// MARK: - Init
36+
init(store: AppStore) {
37+
self.store = store
38+
39+
setupSubscribers()
40+
}
41+
}
42+
43+
44+
// MARK: - Publishers
45+
extension OrderFormContainerViewModel {
46+
47+
// 🔑 Tap into the store's `state` publisher.
48+
private var ordersStatePublisher: AnyPublisher<OrdersState, Never> {
49+
store.$state
50+
.map(\.ordersState)
51+
.eraseToAnyPublisher()
52+
}
53+
54+
55+
... <<More publishers composed from here>> ...
56+
57+
```
58+
59+
60+
```swift
61+
// MARK: - Body
62+
extension OrderFormContainerView {
63+
64+
var body: some View {
65+
NavigationView {
66+
OrderFormView(
67+
viewModel: orderFormViewModel,
68+
buildDestination: {
69+
DeliveryAddressFormView(
70+
viewModel: self.deliveryAddressViewModel,
71+
buildDestination: {
72+
CheckoutView(
73+
viewModel: CheckoutViewModel(
74+
order: self.orderFromFormData
75+
),
76+
onSubmit: self.submitOrder(_:)
77+
)
78+
}
79+
)
80+
}
81+
)
82+
.navigationBarTitle("🧁 Cupcake Corner")
83+
.alert(isPresented: $viewModel.isShowingSaveConfirmationAlert, content: { self.saveConfirmationAlert })
84+
.alert(isPresented: $viewModel.isShowingSaveErrorAlert, content: { self.saveErrorAlert })
85+
}
86+
}
87+
}
88+
89+
```
90+
91+
92+
<br/>
93+
94+
95+
96+
# 📸 Screenshots
97+
98+
99+
<div style="text-align: center;">
100+
<img src="../day-049/Projects/CupcakeCorner/Screenshots/day-51-recording-1.gif" width="400px"/>
101+
</div>

0 commit comments

Comments
 (0)