Skip to content

Commit 590a252

Browse files
committed
Sync countries when triggered
1 parent a50b9db commit 590a252

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Address Edit/EditAddressFormViewModel.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class EditAddressFormViewModel: ObservableObject {
3333
self.storageManager = storageManager
3434
self.stores = stores
3535
updateFieldsWithOriginalAddress()
36+
bindSyncTrigger()
3637
fetchStoredCountriesAndTriggerSyncIfNeeded()
3738
}
3839

@@ -57,6 +58,10 @@ final class EditAddressFormViewModel: ObservableObject {
5758

5859
// MARK: Navigation and utility
5960

61+
/// Define if the view should show placeholders instead of the real elements.
62+
///
63+
@Published private(set) var showPlaceholders: Bool = false
64+
6065
/// Return `true` if the done button should be enabled.
6166
///
6267
var isDoneButtonEnabled: Bool {
@@ -112,4 +117,37 @@ private extension EditAddressFormViewModel {
112117
return syncCountriesTrigger.send()
113118
}
114119
}
120+
121+
/// Sync countries when requested. Defines the `showPlaceholderState` value depending if countries are being synced or not.
122+
///
123+
func bindSyncTrigger() {
124+
syncCountriesTrigger
125+
.handleEvents(receiveOutput: { // Set `showPlaceholders` to `true` before initiating sync.
126+
self.showPlaceholders = true // I could not find a way to assign this using combine operators. :-(
127+
})
128+
.map { // Sync countries
129+
self.makeSyncCountriesFuture()
130+
.replaceError(with: ()) // TODO: Handle errors
131+
}
132+
.switchToLatest()
133+
.map { _ in // Set `showPlaceholders` to `false` after sync is done.
134+
false
135+
}
136+
.assign(to: &$showPlaceholders)
137+
}
138+
139+
/// Creates a publisher that syncs countries into our storage layer.
140+
///
141+
func makeSyncCountriesFuture() -> AnyPublisher<Void, Error> {
142+
Future<Void, Error> { [weak self] promise in
143+
guard let self = self else { return }
144+
145+
let action = DataAction.synchronizeCountries(siteID: self.siteID) { result in
146+
let newResult = result.map { _ in } // Hides the result success type because we don't need it.
147+
promise(newResult)
148+
}
149+
self.stores.dispatch(action)
150+
}
151+
.eraseToAnyPublisher()
152+
}
115153
}

0 commit comments

Comments
 (0)