Skip to content

Commit 0e87e56

Browse files
committed
Delete HSTariffNumberTotalValue in favor of reactive totalValue
1 parent b60095f commit 0e87e56

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModel.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,28 @@ private extension WooShippingCustomsFormViewModel {
120120
}
121121
.assign(to: &$isMissingITN)
122122

123+
let totalItemValue = $itemsViewModels
124+
.map { childViewModels in
125+
childViewModels.map { $0.$totalValue.eraseToAnyPublisher() }
126+
}
127+
.flatMap { childPublishers in
128+
childPublishers.combineLatest()
129+
}
130+
.map { values in
131+
return values.reduce(0) { partialResult, value in
132+
return partialResult + value
133+
}
134+
}
135+
123136
$internationalTransactionNumber.combineLatest(
124-
$itemsViewModels,
137+
totalItemValue,
125138
$destinationCountryCode)
126139
.map { input -> ITNValidationError? in
127-
let (itn, items, countryCode) = input
140+
let (itn, totalItemValue, countryCode) = input
128141
guard itn.isEmpty else {
129142
return ITNNumberValidator.isValid(itn) ? nil : .invalidFormat
130143
}
131144

132-
let totalItemValue = items.reduce(0, { sum, item in
133-
sum + item.totalValue
134-
})
135145
if totalItemValue > Constants.minimumValueForRequiredITN {
136146
return .missingForTotalShipmentValue
137147
}

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModel.swift

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ final class WooShippingCustomsItemViewModel: ObservableObject {
99
@Published var hsTariffNumber: String = ""
1010
@Published var valuePerUnit: String = ""
1111
@Published var weightPerUnit: String = ""
12-
// Useful to determine externally if the shipping requires an ITN
13-
@Published var hsTariffNumberTotalValue: (String, Decimal)?
1412

1513
@Published private var originCountryCode: String?
1614

@@ -31,13 +29,7 @@ final class WooShippingCustomsItemViewModel: ObservableObject {
3129

3230
@Published private(set) var countries: [Country] = []
3331

34-
var totalValue: Decimal {
35-
guard currencySymbol == "$",
36-
let valuePerUnitDecimal = Decimal(string: valuePerUnit) else {
37-
return 0
38-
}
39-
return valuePerUnitDecimal * itemQuantity
40-
}
32+
@Published private(set) var totalValue: Decimal = 0
4133

4234
/// View model for selecting a country from a list.
4335
var countrySelectorVM: CountrySelectorViewModel {
@@ -104,7 +96,7 @@ final class WooShippingCustomsItemViewModel: ObservableObject {
10496

10597
combineToPreselectCountry()
10698
combineRequiredInformationIsEntered()
107-
combineHSTariffNumberTotalValue()
99+
combineTotalItemValue()
108100
}
109101
}
110102

@@ -162,22 +154,19 @@ private extension WooShippingCustomsItemViewModel {
162154
.store(in: &cancellables)
163155
}
164156

165-
func combineHSTariffNumberTotalValue() {
166-
Publishers.CombineLatest($valuePerUnit, $hsTariffNumber)
167-
.sink { [weak self] valuePerUnit, hsTariffNumber in
168-
guard let self = self else { return }
169-
170-
guard self.currencySymbol == "$",
171-
let valuePerUnitDecimal = Decimal(string: valuePerUnit),
172-
hsTariffNumber.isNotEmpty,
173-
isValidTariffNumber else {
174-
self.hsTariffNumberTotalValue = nil
175-
return
176-
}
177-
178-
self.hsTariffNumberTotalValue = (hsTariffNumber, valuePerUnitDecimal * itemQuantity)
157+
func combineTotalItemValue() {
158+
$valuePerUnit.map { [weak self] valuePerUnit in
159+
guard
160+
let self,
161+
currencySymbol == "$",
162+
let valuePerUnitDecimal = Decimal(string: valuePerUnit)
163+
else {
164+
return 0
179165
}
180-
.store(in: &cancellables)
166+
167+
return valuePerUnitDecimal * itemQuantity
168+
}
169+
.assign(to: &$totalValue)
181170
}
182171

183172
/// Specifically introduced to check for a `0` value

0 commit comments

Comments
 (0)