Skip to content

Commit

Permalink
Reload products table view on view appear if we detect change in favo…
Browse files Browse the repository at this point in the history
…rite product IDs.
  • Loading branch information
selanthiraiyan committed Sep 25, 2024
1 parent d32eb09 commit c468a3e
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ final class ProductsViewController: UIViewController, GhostableViewController {
}

navigationController?.navigationBar.removeShadow()

reloadFavoriteProductsIfNeeded()
}

override func viewWillDisappear(_ animated: Bool) {
Expand Down Expand Up @@ -1030,6 +1032,32 @@ private extension ProductsViewController {
return indexPathsForVisibleRows.contains(indexPath)
}

func reloadFavoriteProductsIfNeeded() {
Task { @MainActor [weak self] in
guard let self else { return }

// Reload only if favorite products filter is turned on
guard filters.favoriteProduct != nil else {
return
}

let previousFavoriteProductIDs = viewModel.favoriteProductIDs
await viewModel.loadFavoriteProductIDs()

// Reload only if favorite product IDs changed
guard viewModel.favoriteProductIDs != previousFavoriteProductIDs else {
return
}

await updatePredicate(filters: filters)

/// Reload because `updatePredicate` calls `performFetch` when creating a new predicate
tableView.reloadData()

paginationTracker.resync()
}
}

func updatePredicate(filters: FilterProductListViewModel.Filters) async {
let productIDs: [Int64]? = await {
guard filters.favoriteProduct != nil else {
Expand Down

0 comments on commit c468a3e

Please sign in to comment.