Skip to content

Commit 13df218

Browse files
committed
Use ProductsListViewModel to store selection state
1 parent e90c6ed commit 13df218

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

WooCommerce/Classes/ViewRelated/Products/ProductsViewController.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import class AutomatticTracks.CrashLogging
99
///
1010
final class ProductsViewController: UIViewController, GhostableViewController {
1111

12+
let viewModel: ProductListViewModel = .init()
13+
1214
/// Main TableView
1315
///
1416
@IBOutlet weak var tableView: UITableView!
@@ -274,6 +276,7 @@ private extension ProductsViewController {
274276
}
275277

276278
@objc func finishBulkEditing() {
279+
viewModel.deselectAll()
277280
tableView.setEditing(false, animated: true)
278281

279282
// Enable pull-to-refresh
@@ -363,7 +366,7 @@ private extension ProductsViewController {
363366
}
364367

365368
func configureNavigationBarTitleForEditing() {
366-
let selectedProducts = tableView.indexPathsForSelectedRows?.count ?? 0
369+
let selectedProducts = viewModel.selectedProductsCount
367370
if selectedProducts == 0 {
368371
navigationItem.title = Localization.bulkEditingTitle
369372
} else {
@@ -666,18 +669,28 @@ extension ProductsViewController: UITableViewDelegate {
666669
}
667670

668671
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
669-
guard !tableView.isEditing else {
672+
let product = resultsController.object(at: indexPath)
673+
674+
if tableView.isEditing {
675+
viewModel.selectProduct(product)
670676
configureNavigationBarTitleForEditing()
671-
return
672-
}
677+
} else {
678+
tableView.deselectRow(at: indexPath, animated: true)
673679

674-
tableView.deselectRow(at: indexPath, animated: true)
680+
ServiceLocator.analytics.track(.productListProductTapped)
681+
682+
didSelectProduct(product: product)
683+
}
684+
}
675685

676-
ServiceLocator.analytics.track(.productListProductTapped)
686+
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
687+
guard tableView.isEditing else {
688+
return
689+
}
677690

678691
let product = resultsController.object(at: indexPath)
679-
680-
didSelectProduct(product: product)
692+
viewModel.deselectProduct(product)
693+
configureNavigationBarTitleForEditing()
681694
}
682695

683696
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
@@ -690,6 +703,14 @@ extension ProductsViewController: UITableViewDelegate {
690703
// the actual value. AKA no flicker!
691704
//
692705
estimatedRowHeights[indexPath] = cell.frame.height
706+
707+
// Restore cell selection state
708+
let product = resultsController.object(at: indexPath)
709+
if self.viewModel.productIsSelected(product) {
710+
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
711+
} else {
712+
tableView.deselectRow(at: indexPath, animated: false)
713+
}
693714
}
694715

695716
func scrollViewDidScroll(_ scrollView: UIScrollView) {

0 commit comments

Comments
 (0)