Skip to content

Commit d061be2

Browse files
committed
Remove sync code from country view model
1 parent 2845357 commit d061be2

File tree

4 files changed

+2
-104
lines changed

4 files changed

+2
-104
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class CountrySelectorCommand: ObservableListSelectorCommand {
99

1010
/// Original array of countries.
1111
///
12-
private var countries: [Country]
12+
private let countries: [Country]
1313

1414
/// Data to display
1515
///
@@ -41,13 +41,6 @@ final class CountrySelectorCommand: ObservableListSelectorCommand {
4141
cell.textLabel?.text = model.name
4242
}
4343

44-
/// Resets countries data.
45-
///
46-
func resetCountries(_ countries: [Country]) {
47-
self.countries = countries
48-
self.data = countries
49-
}
50-
5144
/// Filter available countries that contains a given search term.
5245
///
5346
func filterCountries(term: String) {

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

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ final class CountrySelectorViewModel: FilterListSelectorViewModelable, Observabl
1515
}
1616
}
1717

18-
/// Define if the view should show placeholders instead of the real elements.
19-
///
20-
@Published private(set) var showPlaceholders: Bool = false
21-
2218
/// Command that powers the `ListSelector` view.
2319
///
2420
let command = CountrySelectorCommand(countries: [])
@@ -31,93 +27,12 @@ final class CountrySelectorViewModel: FilterListSelectorViewModelable, Observabl
3127
///
3228
let filterPlaceholder = Localization.placeholder
3329

34-
/// ResultsController for stored countries.
35-
///
36-
private lazy var countriesResultsController: ResultsController<StorageCountry> = {
37-
let countriesDescriptor = NSSortDescriptor(key: "name", ascending: true)
38-
return ResultsController<StorageCountry>(storageManager: storageManager, sortedBy: [countriesDescriptor])
39-
}()
40-
41-
/// Trigger to sync countries.
42-
///
43-
private let syncCountriesTrigger = PassthroughSubject<Void, Never>()
44-
45-
/// Storage to fetch countries
46-
///
47-
private let storageManager: StorageManagerType
48-
49-
/// Stores to sync countries
50-
///
51-
private let stores: StoresManager
52-
5330
/// Current `SiteID`, needed to sync countries from a remote source.
5431
///
5532
private let siteID: Int64
5633

57-
init(siteID: Int64, storageManager: StorageManagerType = ServiceLocator.storageManager, stores: StoresManager = ServiceLocator.stores) {
34+
init(siteID: Int64) {
5835
self.siteID = siteID
59-
self.storageManager = storageManager
60-
self.stores = stores
61-
bindSyncTrigger()
62-
bindStoredCountries()
63-
}
64-
}
65-
66-
// MARK: Helpers
67-
private extension CountrySelectorViewModel {
68-
/// Fetches & Binds countries from storage, If there are no stored countries, trigger a sync request.
69-
///
70-
func bindStoredCountries() {
71-
72-
// Bind stored countries & command
73-
countriesResultsController.onDidChangeContent = { [weak self] in
74-
guard let self = self else { return }
75-
self.command.resetCountries(self.countriesResultsController.fetchedObjects)
76-
}
77-
78-
// Initial fetch
79-
try? countriesResultsController.performFetch()
80-
81-
// Trigger a sync request if there are no countries.
82-
guard !countriesResultsController.isEmpty else {
83-
return syncCountriesTrigger.send()
84-
}
85-
86-
// Reset countries with fetched
87-
command.resetCountries(countriesResultsController.fetchedObjects)
88-
}
89-
90-
/// Sync countries when requested. Defines the `showPlaceholderState` value depending if countries are being synced or not.
91-
///
92-
func bindSyncTrigger() {
93-
syncCountriesTrigger
94-
.handleEvents(receiveOutput: { // Set `showPlaceholders` to `true` before initiating sync.
95-
self.showPlaceholders = true // I could not find a way to assign this using combine operators. :-(
96-
})
97-
.map { // Sync countries
98-
self.makeSyncCountriesFuture()
99-
.replaceError(with: ()) // TODO: Handle errors
100-
}
101-
.switchToLatest()
102-
.map { _ in // Set `showPlaceholders` to `false` after sync is done.
103-
false
104-
}
105-
.assign(to: &$showPlaceholders)
106-
}
107-
108-
/// Creates a publisher that syncs countries into our storage layer.
109-
///
110-
func makeSyncCountriesFuture() -> AnyPublisher<Void, Error> {
111-
Future<Void, Error> { [weak self] promise in
112-
guard let self = self else { return }
113-
114-
let action = DataAction.synchronizeCountries(siteID: self.siteID) { result in
115-
let newResult = result.map { _ in } // Hides the result success type because we don't need it.
116-
promise(newResult)
117-
}
118-
self.stores.dispatch(action)
119-
}
120-
.eraseToAnyPublisher()
12136
}
12237
}
12338

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ protocol FilterListSelectorViewModelable: ObservableObject {
2020
/// Placeholder for the filter text field
2121
///
2222
var filterPlaceholder: String { get }
23-
24-
/// Value to indicate if the views should be redacted
25-
///
26-
var showPlaceholders: Bool { get }
2723
}
2824

2925
/// Filterable List Selector View
@@ -42,8 +38,6 @@ struct FilterListSelector<ViewModel: FilterListSelectorViewModelable>: View {
4238
ListSelector(command: viewModel.command, tableStyle: .plain)
4339
}
4440
.navigationTitle(viewModel.navigationTitle)
45-
.redacted(reason: viewModel.showPlaceholders ? .placeholder : [])
46-
.shimmering(active: viewModel.showPlaceholders)
4741
}
4842
}
4943

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ final class StateSelectorViewModel: FilterListSelectorViewModelable, ObservableO
2525
/// Filter text field placeholder
2626
///
2727
let filterPlaceholder = Localization.placeholder
28-
29-
/// States do not require data loading
30-
///
31-
let showPlaceholders = false
3228
}
3329

3430
// MARK: Constants

0 commit comments

Comments
 (0)