Skip to content

[Woo POS] Avoid initial view model unwanted recreation #12799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WooCommerce/Classes/POS/Presentation/FilterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct FilterView: View {
var body: some View {
Button("Filter") {
// TODO: https://github.com/woocommerce/woocommerce-ios/issues/12761
viewModel.showFilters()
}
.frame(maxWidth: .infinity, idealHeight: 120)
.font(.title2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ struct PointOfSaleDashboardView: View {
.sheet(isPresented: $viewModel.showsCardReaderSheet, content: {
CardReaderConnectionView(viewModel: viewModel.cardReaderConnectionViewModel)
})
.sheet(isPresented: $viewModel.showsFilterSheet, content: {
FilterView(viewModel: viewModel)
})
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import SwiftUI

struct PointOfSaleEntryPointView: View {
// TODO:
// Temporary. DI proper product models once we have a data layer
private let viewModel: PointOfSaleDashboardViewModel = {
let products = POSProductFactory.makeFakeProducts()
let viewModel = PointOfSaleDashboardViewModel(products: products, cardReaderConnectionViewModel: .init(state: .scanningForReader(cancel: {})))

return viewModel
}()
@StateObject private var viewModel: PointOfSaleDashboardViewModel = PointOfSaleDashboardViewModel(
products: POSProductFactory.makeFakeProducts(),
cardReaderConnectionViewModel: .init(state: .connectingToReader)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: is the change of the cardReaderConnectionViewModel initial state from scanningForReader to connectingToReader intentional? really just a minor non-blocking observation as it's just a simulated flow, feel free to leave it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, not intentional at all, just a random connecting initial state :D

)

private let hideAppTabBar: ((Bool) -> Void)

// Necessary to expose the View's entry point to WooCommerce
// Otherwise the current switch on `HubMenu` where this View is created
// will error with "No exact matches in reference to static method 'buildExpression'"
public init(hideAppTabBar: @escaping ((Bool) -> Void)) {
init(hideAppTabBar: @escaping ((Bool) -> Void)) {
self.hideAppTabBar = hideAppTabBar
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ final class PointOfSaleDashboardViewModel: ObservableObject {
@Published var productsInCart: [CartProduct] = []

@Published var showsCardReaderSheet: Bool = false
@Published var showsFilterSheet: Bool = false
@ObservedObject private(set) var cardReaderConnectionViewModel: CardReaderConnectionViewModel

init(products: [POSProduct],
Expand Down Expand Up @@ -70,4 +71,15 @@ final class PointOfSaleDashboardViewModel: ObservableObject {
func showCardReaderConnection() {
showsCardReaderSheet = true
}

func showFilters() {
showsFilterSheet = true
}
}

extension PointOfSaleDashboardViewModel {
// Helper function to populate SwifUI previews
static func defaultPreview() -> PointOfSaleDashboardViewModel {
PointOfSaleDashboardViewModel(products: [], cardReaderConnectionViewModel: .init(state: .connectingToReader))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class HubMenuViewController: UIHostingController<HubMenu> {
tapToPayBadgePromotionChecker: TapToPayBadgePromotionChecker) {
self.viewModel = HubMenuViewModel(siteID: siteID,
tapToPayBadgePromotionChecker: tapToPayBadgePromotionChecker)

self.tapToPayBadgePromotionChecker = tapToPayBadgePromotionChecker
super.init(rootView: HubMenu(viewModel: viewModel))
configureTabBarItem()
Expand Down