diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 8afad565219..fdc050fee41 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -9,6 +9,7 @@ - [*] Shipping Labels: Enable the confirm button on the payment method sheet even when there are no changes. [https://github.com/woocommerce/woocommerce-ios/pull/15856] - [*] POS: start a new cart by scanning a barcode on the payment success screen [https://github.com/woocommerce/woocommerce-ios/pull/15870] - [*] Watch app: Fixed connection issue upon fresh install [https://github.com/woocommerce/woocommerce-ios/pull/15867] +- [Internal] Shipping Labels: Optimize requests for syncing countries [https://github.com/woocommerce/woocommerce-ios/pull/15875] - [*] Shipping Labels: Display label size from account settings as default [https://github.com/woocommerce/woocommerce-ios/pull/15873] 22.7 diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/ShipmentDetails/WooShippingShipmentDetailsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/ShipmentDetails/WooShippingShipmentDetailsViewModel.swift index 00b3088a142..a227a4fa352 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/ShipmentDetails/WooShippingShipmentDetailsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/ShipmentDetails/WooShippingShipmentDetailsViewModel.swift @@ -70,7 +70,6 @@ final class WooShippingShipmentDetailsViewModel: ObservableObject { order: order, shipment: shipment, originCountryCode: originCountryCodePublisher(), - stores: stores, storageManager: storageManager ) { [weak self] form in self?.customsForm = form diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModel.swift index 01b57f57656..0cc5ead0401 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsFormViewModel.swift @@ -42,7 +42,6 @@ final class WooShippingCustomsFormViewModel: ObservableObject { init(order: Order, shipment: Shipment, originCountryCode: AnyPublisher? = nil, - stores: StoresManager = ServiceLocator.stores, storageManager: StorageManagerType = ServiceLocator.storageManager, onFormReady: @escaping (ShippingLabelCustomsForm) -> ()) { self.onFormReady = onFormReady @@ -55,8 +54,7 @@ final class WooShippingCustomsFormViewModel: ObservableObject { itemWeight: $0.weight, currencySymbol: currencySymbol(from: order), originCountryCode: originCountryCode, - storageManager: storageManager, - stores: stores) + storageManager: storageManager) } listenToItemsRequiredInformationValues() diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModel.swift index 3a5a6199822..f5d6b730aa5 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Customs/WooShippingCustomsItemViewModel.swift @@ -15,8 +15,6 @@ final class WooShippingCustomsItemViewModel: ObservableObject { @Published private var originCountryCode: String? private let storageManager: StorageManagerType - private let stores: StoresManager - private let siteID: Int64 let currencySymbol: String let itemProductID: Int64 @@ -75,8 +73,7 @@ final class WooShippingCustomsItemViewModel: ObservableObject { itemWeight: Double, currencySymbol: String, originCountryCode: AnyPublisher? = nil, - storageManager: StorageManagerType = ServiceLocator.storageManager, - stores: StoresManager = ServiceLocator.stores) { + storageManager: StorageManagerType = ServiceLocator.storageManager) { self.title = itemName self.description = itemName self.itemProductID = itemProductID @@ -85,8 +82,6 @@ final class WooShippingCustomsItemViewModel: ObservableObject { self.weightPerUnit = String(itemWeight) self.currencySymbol = currencySymbol self.storageManager = storageManager - self.stores = stores - self.siteID = stores.sessionManager.defaultStoreID ?? Int64.min originCountryCode? .assign(to: &$originCountryCode) @@ -113,21 +108,20 @@ private extension WooShippingCustomsItemViewModel { } func fetchCountries() { - try? resultsController.performFetch() - countries = resultsController.fetchedObjects + resultsController.onDidChangeContent = { [weak self] in + self?.updateCountries() + } - let action = DataAction.synchronizeCountries(siteID: siteID) { [weak self] (result) in - guard let self = self else { return } - switch result { - case .success: - try? self.resultsController.performFetch() - self.countries = self.resultsController.fetchedObjects - case .failure: - break - } + resultsController.onDidResetContent = { [weak self] in + self?.updateCountries() } - stores.dispatch(action) + try? resultsController.performFetch() + updateCountries() + } + + func updateCountries() { + countries = resultsController.fetchedObjects } func combineRequiredInformationIsEntered() { diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift index 49de84fe655..842b8661a04 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift @@ -628,22 +628,19 @@ private extension WooShippingEditAddressViewModel { // MARK: Remote private extension WooShippingEditAddressViewModel { func fetchCountries() { - refreshCountriesAndUpdateSelections() - let action = DataAction.synchronizeCountries(siteID: siteID) { [weak self] (result) in - guard let self = self else { return } - switch result { - case .success: - refreshCountriesAndUpdateSelections() - case .failure: - break - } + resultsController.onDidChangeContent = { [weak self] in + self?.refreshCountriesAndUpdateSelections() + } + + resultsController.onDidResetContent = { [weak self] in + self?.refreshCountriesAndUpdateSelections() } - stores.dispatch(action) + try? resultsController.performFetch() + refreshCountriesAndUpdateSelections() } func refreshCountriesAndUpdateSelections() { - try? resultsController.performFetch() // Updating the selected country clears the selected state. // We track the initial state code so we can set the correct selected state. let stateCode = state.value diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift index df1efcb51f1..ec769a8a9c4 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift @@ -233,6 +233,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { loadDestinationAddress() } + syncCountries() updateShipmentDetailsViewModels() observeSelectedOriginAddress() observeDestinationAddress() @@ -467,6 +468,12 @@ private extension WooShippingCreateLabelsViewModel { } stores.dispatch(action) } + + /// Sync countries to update the storage with latest data from remote. + /// This is useful for checking countries in the customs form and listing countries in the edit address form. + func syncCountries() { + stores.dispatch(DataAction.synchronizeCountries(siteID: order.siteID) { _ in }) + } } // MARK: Utils diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift index 15587764320..8eb60786312 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift @@ -356,18 +356,11 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { XCTAssertEqual(viewModel.countries.count, 2, "Should include all countries for destination addresses") } - func test_init_fetches_countries_from_remote() { + func test_init_fetches_countries_from_storage() { // Given - let stores = MockStoresManager(sessionManager: .testingInstance) let storageManager = MockStorageManager() let country = Country(code: "US", name: "United States", states: []) - stores.whenReceivingAction(ofType: DataAction.self) { action in - switch action { - case .synchronizeCountries(_, let completion): - storageManager.insertSampleCountries(readOnlyCountries: [country]) - completion(.success([country])) - } - } + storageManager.insertSampleCountries(readOnlyCountries: [country]) // When let viewModel = WooShippingEditAddressViewModel(type: .destination(orderID: sampleOrderID), @@ -384,12 +377,10 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { isDefaultAddress: true, showCompanyField: true, isVerified: true, - stores: stores, storageManager: storageManager) // Then XCTAssertEqual(viewModel.countries.count, 1) - XCTAssertTrue(stores.receivedActions.first is DataAction) } func test_state_required_when_selected_country_contains_states() { diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingShipmentDetailsViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingShipmentDetailsViewModelTests.swift index 22b634458bf..9c6eae86ba1 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingShipmentDetailsViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingShipmentDetailsViewModelTests.swift @@ -359,14 +359,7 @@ final class WooShippingShipmentDetailsViewModelTests: XCTestCase { Country(code: "US", name: "United States", states: []), Country(code: "CA", name: "Canada", states: []) ] - - stores.whenReceivingAction(ofType: DataAction.self) { action in - switch action { - case let .synchronizeCountries(_, onCompletion): - storageManager.insertSampleCountries(readOnlyCountries: countries) - onCompletion(.success(countries)) - } - } + storageManager.insertSampleCountries(readOnlyCountries: countries) let originAddressSubject = CurrentValueSubject( sampleOriginAddress(country: "US", state: "NY") @@ -790,14 +783,7 @@ final class WooShippingShipmentDetailsViewModelTests: XCTestCase { Country(code: "US", name: "United States", states: []), Country(code: "CA", name: "Canada", states: []) ] - - stores.whenReceivingAction(ofType: DataAction.self) { action in - switch action { - case let .synchronizeCountries(_, onCompletion): - storageManager.insertSampleCountries(readOnlyCountries: countries) - onCompletion(.success(countries)) - } - } + storageManager.insertSampleCountries(readOnlyCountries: countries) let viewModel = WooShippingShipmentDetailsViewModel( order: Order.fake(), @@ -833,14 +819,7 @@ final class WooShippingShipmentDetailsViewModelTests: XCTestCase { originCountry, destinationCountry ] - - stores.whenReceivingAction(ofType: DataAction.self) { action in - switch action { - case let .synchronizeCountries(_, onCompletion): - storageManager.insertSampleCountries(readOnlyCountries: countries) - onCompletion(.success(countries)) - } - } + storageManager.insertSampleCountries(readOnlyCountries: countries) let shipment = sampleShipment