Skip to content

Commit 39f47d3

Browse files
committed
Reduce code duplication
1 parent f1b6bca commit 39f47d3

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

WooCommerce/Classes/ViewRelated/Filters/FilterListViewController.swift

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,10 @@ private extension FilterListViewController {
273273
}
274274

275275
let selectedValueAction: (FilterType) -> Void = { [weak self] selectedOption in
276-
guard let self = self else {
277-
return
278-
}
279-
if selectedOption.description != selected.selectedValue.description {
280-
selected.selectedValue = selectedOption
281-
self.updateUI(numberOfActiveFilters: self.viewModel.filterTypeViewModels.numberOfActiveFilters)
282-
self.listSelector.reloadData()
283-
}
276+
guard let self else { return }
277+
selected.selectedValue = selectedOption
278+
updateUI(numberOfActiveFilters: viewModel.filterTypeViewModels.numberOfActiveFilters)
279+
listSelector.reloadData()
284280
}
285281

286282
switch selected.listSelectorConfig {
@@ -289,7 +285,9 @@ private extension FilterListViewController {
289285
data: options,
290286
selected: selected.selectedValue,
291287
hostViewController: self)
292-
self.selectedFilterValueSubscription = command.onItemSelected.sink { selectedValueAction($0) }
288+
self.selectedFilterValueSubscription = command.onItemSelected.sink {
289+
selectedValueAction($0)
290+
}
293291
let staticListSelector = ListSelectorViewController(command: command, tableViewStyle: .plain) { _ in }
294292
self.listSelector.navigationController?.pushViewController(staticListSelector, animated: true)
295293
case .multiSelectStaticOptions(let options):
@@ -304,10 +302,9 @@ private extension FilterListViewController {
304302
title: selected.title,
305303
options: options,
306304
initialSelection: selectedItems,
307-
onSelection: { [weak self] selectedOptions in
308-
selected.selectedValue = MultipleFilterSelection(items: selectedOptions)
309-
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
310-
self?.listSelector.reloadData()
305+
onSelection: { selectedOptions in
306+
let filterType = MultipleFilterSelection(items: selectedOptions)
307+
selectedValueAction(filterType)
311308
}
312309
)
313310
let hostingController = UIHostingController(rootView: multiSelectView)
@@ -350,9 +347,8 @@ private extension FilterListViewController {
350347
onProductSelectionStateChanged: { [weak self] product, _ in
351348
guard let self else { return }
352349

353-
selected.selectedValue = FilterOrdersByProduct(id: product.productID, name: product.name)
354-
self.updateUI(numberOfActiveFilters: self.viewModel.filterTypeViewModels.numberOfActiveFilters)
355-
self.listSelector.reloadData()
350+
let filterType = FilterOrdersByProduct(id: product.productID, name: product.name)
351+
selectedValueAction(filterType)
356352
self.listSelector.dismiss(animated: true)
357353
},
358354
onCloseButtonTapped: { [weak self] in
@@ -381,11 +377,9 @@ private extension FilterListViewController {
381377
configuration: configuration,
382378
addressFormViewModel: nil,
383379
selectedCustomerID: selectedCustomerID,
384-
onCustomerSelected: { [weak self] customer in
385-
selected.selectedValue = CustomerFilter(customer: customer)
386-
387-
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
388-
self?.listSelector.reloadData()
380+
onCustomerSelected: { customer in
381+
let filterType = CustomerFilter(customer: customer)
382+
selectedValueAction(filterType)
389383
}
390384
)
391385
}()
@@ -404,10 +398,9 @@ private extension FilterListViewController {
404398
viewModel: viewModel,
405399
syncable: syncable,
406400
initialSelections: selectedMembers,
407-
onSelection: { [weak self] resources in
408-
selected.selectedValue = MultipleFilterSelection(items: resources)
409-
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
410-
self?.listSelector.reloadData()
401+
onSelection: { resources in
402+
let filterType = MultipleFilterSelection(items: resources)
403+
selectedValueAction(filterType)
411404
}
412405
)
413406
let hostingController = UIHostingController(rootView: memberListSelectorView)
@@ -426,13 +419,12 @@ private extension FilterListViewController {
426419
viewModel: viewModel,
427420
syncable: syncable,
428421
initialSelections: selectedProducts,
429-
onSelection: { [weak self] products in
422+
onSelection: { products in
430423
let filters = products.map { product in
431424
BookingProductFilter(productID: product.productID, name: product.name)
432425
}
433-
selected.selectedValue = MultipleFilterSelection(items: filters)
434-
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
435-
self?.listSelector.reloadData()
426+
let filterType = MultipleFilterSelection(items: filters)
427+
selectedValueAction(filterType)
436428
}
437429
)
438430
let hostingController = UIHostingController(rootView: memberListSelectorView)
@@ -442,10 +434,9 @@ private extension FilterListViewController {
442434
let dateTimeFilterView = BookingDateTimeFilterView(
443435
startDate: selectedDateRange?.startDate,
444436
endDate: selectedDateRange?.endDate,
445-
onSelection: { [weak self] startDate, endDate in
446-
selected.selectedValue = BookingDateRangeFilter(startDate: startDate, endDate: endDate)
447-
self?.updateUI(numberOfActiveFilters: self?.viewModel.filterTypeViewModels.numberOfActiveFilters ?? 0)
448-
self?.listSelector.reloadData()
437+
onSelection: { startDate, endDate in
438+
let filterType = BookingDateRangeFilter(startDate: startDate, endDate: endDate)
439+
selectedValueAction(filterType)
449440
}
450441
)
451442
let hostingController = UIHostingController(rootView: dateTimeFilterView)

0 commit comments

Comments
 (0)