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