Skip to content

Commit 7119219

Browse files
committed
PointOfSaleCollectCashView: prefill text field with order total, update button enabled state based on input & loading state, and remove validation error.
1 parent 363e0b0 commit 7119219

File tree

3 files changed

+289
-117
lines changed

3 files changed

+289
-117
lines changed

WooCommerce/Classes/POS/Presentation/PointOfSaleCollectCashView.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ struct PointOfSaleCollectCashView: View {
2525
String.localizedStringWithFormat(Localization.backNavigationSubtitle, orderTotal)
2626
}
2727

28+
private var isButtonEnabled: Bool {
29+
viewHelper.isPaymentButtonEnabled(orderTotal: orderTotal,
30+
textFieldAmountInput: textFieldAmountInput,
31+
isLoading: isLoading)
32+
}
33+
2834
@StateObject private var textFieldViewModel = FormattableAmountTextFieldViewModel(size: .extraLarge,
2935
locale: Locale.autoupdatingCurrent,
3036
storeCurrencySettings: ServiceLocator.currencySettings,
@@ -89,7 +95,7 @@ struct PointOfSaleCollectCashView: View {
8995
.buttonStyle(POSFilledButtonStyle(size: .normal, isLoading: isLoading))
9096
.frame(maxWidth: .infinity)
9197
.dynamicTypeSize(...DynamicTypeSize.accessibility1)
92-
.disabled(isLoading)
98+
.disabled(!isButtonEnabled)
9399
}
94100
.padding([.horizontal])
95101
.padding(.bottom, max(keyboardFrame.height - geometry.safeAreaInsets.bottom,
@@ -110,6 +116,9 @@ struct PointOfSaleCollectCashView: View {
110116
}
111117
}
112118
.frame(maxWidth: .infinity, maxHeight: .infinity)
119+
.onAppear {
120+
prefillOrderTotal()
121+
}
113122
}
114123

115124
private func markComplete() async throws {
@@ -122,9 +131,6 @@ struct PointOfSaleCollectCashView: View {
122131
private extension PointOfSaleCollectCashView {
123132
private func submitCashAmount() async {
124133
ServiceLocator.analytics.track(.pointOfSaleCashPaymentTapped)
125-
guard validateAmountOnSubmit() else {
126-
return
127-
}
128134
isLoading = true
129135
do {
130136
try await markComplete()
@@ -141,14 +147,12 @@ private extension PointOfSaleCollectCashView {
141147
textFieldAmountInput: textFieldAmountInput)
142148
}
143149

144-
private func validateAmountOnSubmit() -> Bool {
145-
viewHelper.validateAmountOnSubmit(
146-
orderTotal: orderTotal,
147-
textFieldAmountInput: textFieldAmountInput,
148-
onError: { error in
149-
errorMessage = error
150-
})
150+
private func prefillOrderTotal() {
151+
if let orderDecimal = viewHelper.parseCurrency(orderTotal) {
152+
textFieldViewModel.presetAmount(orderDecimal)
151153
}
154+
isTextFieldFocused = true
155+
}
152156
}
153157

154158
private extension PointOfSaleCollectCashView {

WooCommerce/Classes/POS/ViewHelpers/CollectCashViewHelper.swift

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,17 @@ final class CollectCashViewHelper {
5454
}
5555
}
5656

57-
func validateAmountOnSubmit(orderTotal: String,
57+
func isPaymentButtonEnabled(orderTotal: String,
5858
textFieldAmountInput: String,
59-
onError: (String) -> Void) -> Bool {
60-
let userInput = textFieldAmountInput.isNotEmpty ? textFieldAmountInput : "0"
61-
59+
isLoading: Bool) -> Bool {
6260
guard let orderDecimal = parseCurrency(orderTotal),
63-
let inputDecimal = parseCurrency(userInput) else {
64-
onError(Localization.failedToCollectCashPayment)
65-
return false
66-
}
67-
68-
if inputDecimal < orderDecimal {
69-
onError(Localization.cashPaymentAmountNotEnough)
61+
let inputDecimal = parseCurrency(textFieldAmountInput.isNotEmpty ? textFieldAmountInput : "0") else {
7062
return false
7163
}
72-
return true
64+
return inputDecimal >= orderDecimal && !isLoading
7365
}
7466

75-
private func parseCurrency(_ amountString: String) -> Decimal? {
67+
func parseCurrency(_ amountString: String) -> Decimal? {
7668
// Removes all leading/trailing whitespace, if any
7769
let sanitized = amountString.trimmingCharacters(in: .whitespacesAndNewlines)
7870

@@ -101,16 +93,6 @@ private extension CollectCashViewHelper {
10193
comment: "Change due when the cash amount entered exceeds the order total." +
10294
"Reads as 'Change due: $1.23'"
10395
)
104-
static let failedToCollectCashPayment = NSLocalizedString(
105-
"collectcashviewhelper.failedtocollectcashpayment.errormessage",
106-
value: "Error trying to process payment. Try again.",
107-
comment: "Error message when the system fails to collect a cash payment."
108-
)
109-
static let cashPaymentAmountNotEnough = NSLocalizedString(
110-
"collectcashviewhelper.cashpaymentamountnotenough.errormessage",
111-
value: "Amount must be more or equal to total.",
112-
comment: "Error message when the cash amount entered is less than the order total."
113-
)
11496
}
11597
}
11698

0 commit comments

Comments
 (0)