-
Notifications
You must be signed in to change notification settings - Fork 109
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
[Woo POS] Extract totals logic to TotalsViewModel
#13211
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a1f5322
Create `TotalsViewModel` and extract sync. Unit tests
iamgabrielma 701dda3
Move methods from dashboard to TotalsViewModel
iamgabrielma 2944fa9
Pass items from cart to calculate and sync order
iamgabrielma e62d9a2
Merge branch 'trunk' into task/12998-pos-extract-totals-vm
jaclync 4fe2aed
Move derived variables from TotalsView to TotalsViewModel.
jaclync c56f34e
Move payment related code from `PointOfSaleDashboardViewModel` to `To…
jaclync f3c427a
Remove unused `POSOrderService.updateOrderStatus`.
jaclync 85a0626
Minor polishes to privatize and remove dashboard VM dependency as muc…
jaclync 2e8b444
Fix unit testing build failure.
jaclync File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ import SwiftUI | |
|
||
struct TotalsView: View { | ||
@ObservedObject private var viewModel: PointOfSaleDashboardViewModel | ||
@ObservedObject private var totalsViewModel: TotalsViewModel | ||
|
||
init(viewModel: PointOfSaleDashboardViewModel) { | ||
init(viewModel: PointOfSaleDashboardViewModel, totalsViewModel: TotalsViewModel) { | ||
self.viewModel = viewModel | ||
self.totalsViewModel = totalsViewModel | ||
} | ||
|
||
var body: some View { | ||
|
@@ -19,21 +21,21 @@ struct TotalsView: View { | |
HStack { | ||
VStack(spacing: Constants.totalsVerticalSpacing) { | ||
priceFieldView(title: "Subtotal", | ||
formattedPrice: viewModel.formattedCartTotalPrice, | ||
formattedPrice: totalsViewModel.formattedCartTotalPrice, | ||
shimmeringActive: false, | ||
redacted: false) | ||
Divider() | ||
.overlay(Color.posTotalsSeparator) | ||
priceFieldView(title: "Taxes", | ||
formattedPrice: | ||
viewModel.formattedOrderTotalTaxPrice, | ||
shimmeringActive: viewModel.isSyncingOrder, | ||
redacted: viewModel.formattedOrderTotalTaxPrice == nil || viewModel.isSyncingOrder) | ||
totalsViewModel.formattedOrderTotalTaxPrice, | ||
shimmeringActive: totalsViewModel.isShimmering, | ||
redacted: totalsViewModel.isPriceFieldRedacted) | ||
Divider() | ||
.overlay(Color.posTotalsSeparator) | ||
totalPriceView(formattedPrice: viewModel.formattedOrderTotalPrice, | ||
shimmeringActive: viewModel.isSyncingOrder, | ||
redacted: viewModel.formattedOrderTotalPrice == nil || viewModel.isSyncingOrder) | ||
totalPriceView(formattedPrice: totalsViewModel.formattedOrderTotalPrice, | ||
shimmeringActive: totalsViewModel.isShimmering, | ||
redacted: totalsViewModel.isTotalPriceFieldRedacted) | ||
} | ||
.padding() | ||
.frame(idealWidth: Constants.pricesIdealWidth) | ||
|
@@ -42,9 +44,11 @@ struct TotalsView: View { | |
RoundedRectangle(cornerRadius: Constants.defaultBorderLineCornerRadius) | ||
.stroke(Color.posTotalsSeparator, lineWidth: Constants.defaultBorderLineWidth) | ||
) | ||
if viewModel.showRecalculateButton { | ||
if totalsViewModel.showRecalculateButton { | ||
Button("Calculate amounts") { | ||
viewModel.calculateAmountsTapped() | ||
totalsViewModel.calculateAmountsTapped( | ||
with: viewModel.cartViewModel.itemsInCart, | ||
allItems: viewModel.itemSelectorViewModel.items) | ||
} | ||
} | ||
} | ||
|
@@ -60,12 +64,25 @@ struct TotalsView: View { | |
} | ||
} | ||
.onDisappear { | ||
viewModel.onTotalsViewDisappearance() | ||
totalsViewModel.onTotalsViewDisappearance() | ||
} | ||
.sheet(isPresented: $totalsViewModel.showsCardReaderSheet, content: { | ||
// Might be the only way unless we make the type conform to `Identifiable` | ||
if let alertType = totalsViewModel.cardPresentPaymentAlertViewModel { | ||
PointOfSaleCardPresentPaymentAlert(alertType: alertType) | ||
} else { | ||
switch totalsViewModel.cardPresentPaymentEvent { | ||
case .idle, | ||
.show, // handled above | ||
.showOnboarding: | ||
Text(totalsViewModel.cardPresentPaymentEvent.temporaryEventDescription) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
private var gradientStops: [Gradient.Stop] { | ||
if viewModel.paymentState == .cardPaymentSuccessful { | ||
if totalsViewModel.paymentState == .cardPaymentSuccessful { | ||
return [ | ||
Gradient.Stop(color: Color.clear, location: 0.0), | ||
Gradient.Stop(color: Color.posTotalsGradientGreen, location: 1.0) | ||
|
@@ -78,10 +95,6 @@ struct TotalsView: View { | |
] | ||
} | ||
} | ||
|
||
private var paymentButtonsDisabled: Bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This appears to be unused, thanks for cleaning it up. |
||
return !viewModel.areAmountsFullyCalculated | ||
} | ||
} | ||
|
||
private extension TotalsView { | ||
|
@@ -107,7 +120,7 @@ private extension TotalsView { | |
|
||
@ViewBuilder | ||
private var paymentsActionButtons: some View { | ||
if viewModel.paymentState == .cardPaymentSuccessful { | ||
if totalsViewModel.paymentState == .cardPaymentSuccessful { | ||
newTransactionButton | ||
} | ||
else { | ||
|
@@ -116,19 +129,19 @@ private extension TotalsView { | |
} | ||
|
||
@ViewBuilder private var cardReaderView: some View { | ||
switch viewModel.cardReaderConnectionViewModel.connectionStatus { | ||
switch totalsViewModel.connectionStatus { | ||
case .connected: | ||
if let inlinePaymentMessage = viewModel.cardPresentPaymentInlineMessage { | ||
if let inlinePaymentMessage = totalsViewModel.cardPresentPaymentInlineMessage { | ||
PointOfSaleCardPresentPaymentInLineMessage(messageType: inlinePaymentMessage) | ||
} else { | ||
Text("Reader connected") | ||
Button(action: viewModel.cardPaymentTapped) { | ||
Button(action: totalsViewModel.cardPaymentTapped) { | ||
Text("Collect Payment") | ||
} | ||
} | ||
case .disconnected: | ||
Text("Reader disconnected") | ||
Button(action: viewModel.cardPaymentTapped) { | ||
Button(action: totalsViewModel.cardPaymentTapped) { | ||
Text("Collect Payment") | ||
} | ||
} | ||
|
@@ -181,10 +194,27 @@ private extension TotalsView { | |
} | ||
} | ||
|
||
fileprivate extension CardPresentPaymentEvent { | ||
var temporaryEventDescription: String { | ||
switch self { | ||
case .idle: | ||
return "Idle" | ||
case .show: | ||
return "Event" | ||
case .showOnboarding(let onboardingViewModel): | ||
return "Onboarding: \(onboardingViewModel.state.reasonForAnalytics)" // This will only show the initial onboarding state | ||
} | ||
} | ||
} | ||
|
||
#if DEBUG | ||
#Preview { | ||
TotalsView(viewModel: .init(itemProvider: POSItemProviderPreview(), | ||
cardPresentPaymentService: CardPresentPaymentPreviewService(), | ||
orderService: POSOrderPreviewService())) | ||
orderService: POSOrderPreviewService(), | ||
currencyFormatter: .init(currencySettings: .init())), | ||
totalsViewModel: .init(orderService: POSOrderPreviewService(), | ||
cardPresentPaymentService: CardPresentPaymentPreviewService(), | ||
currencyFormatter: .init(currencySettings: .init()))) | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be here because now the "Connect now" button in toolbar does not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now it only works if the totals are presented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh great catch! I will fix this today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking at this now and even if I change to how it was the sheet does not show, did we change something behind the scenes on how we set showsCardReaderSheet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does work for me with the changes on #13226, can you check if you can still reproduce the issue on that branch?