Skip to content

Commit

Permalink
Do not refresh collection view when we are changing the display mode (#…
Browse files Browse the repository at this point in the history
…2895)

Description:

Do not refresh collection view when we are changing the display mode as it may result in a crash.

Steps to test this PR:

Open https://privacy-test-pages.site/features/auto-refresh.html in few tabs.
Open TabSwitcher.
Start toggling Grid/List mode quickly.
General TabSwitcher smoke tests
  • Loading branch information
bwaresiak authored May 28, 2024
1 parent 36cab6d commit bf918bf
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions DuckDuckGo/TabSwitcherViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,35 @@ class TabSwitcherViewController: UIViewController {
}

@IBAction func onDisplayModeButtonPressed(_ sender: UIButton) {
tabSwitcherSettings.isGridViewEnabled = !tabSwitcherSettings.isGridViewEnabled

if tabSwitcherSettings.isGridViewEnabled {
Pixel.fire(pixel: .tabSwitcherGridEnabled)
} else {
Pixel.fire(pixel: .tabSwitcherListEnabled)
guard isProcessingUpdates == false else { return }

isProcessingUpdates = true
// Idea is here to wait for any pending processing of reconfigureItems on a cells,
// so when transition to/from grid happens we can request cells without any issues
// related to mismatched identifiers.
// Alternative is to use reloadItems instead of reconfigureItems but it looks very bad
// when tabs are reloading in the background.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
guard let self else { return }

tabSwitcherSettings.isGridViewEnabled = !tabSwitcherSettings.isGridViewEnabled

if tabSwitcherSettings.isGridViewEnabled {
Pixel.fire(pixel: .tabSwitcherGridEnabled)
} else {
Pixel.fire(pixel: .tabSwitcherListEnabled)
}

self.refreshDisplayModeButton()

UIView.transition(with: view,
duration: 0.3,
options: .transitionCrossDissolve, animations: {
self.collectionView.reloadData()
}, completion: { _ in
self.isProcessingUpdates = false
})
}

refreshDisplayModeButton()

UIView.transition(with: view,
duration: 0.3,
options: .transitionCrossDissolve, animations: {
self.collectionView.reloadData()
}, completion: nil)
}

@IBAction func onAddPressed(_ sender: UIBarButtonItem) {
Expand Down

0 comments on commit bf918bf

Please sign in to comment.