Skip to content

Commit

Permalink
Fix brave/brave-ios#7996: Switching Private Mode Tab Tray cause posit…
Browse files Browse the repository at this point in the history
…ion loss in Normal Mode (brave/brave-ios#8015)
  • Loading branch information
soner-yuksel committed Sep 2, 2023
1 parent 731a35f commit 0a3e173
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions Sources/Brave/Frontend/Browser/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class TabManager: NSObject {
return _selectedIndex
}
var normalTabSelectedIndex: Int = 0
var privateTabSelectedIndex: Int = 0
var tempTabs: [Tab]?
private weak var rewards: BraveRewards?
private weak var tabGeneratorAPI: BraveTabGeneratorAPI?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,10 @@ class TabTrayController: AuthenticationController {
return
}

// Record the slected index before private mode navigation
if !privateMode {
// Record the selected index before mode navigation
if privateMode {
tabManager.privateTabSelectedIndex = Preferences.Privacy.persistentPrivateBrowsing.value ? tabManager.selectedIndex : 0
} else {
tabManager.normalTabSelectedIndex = tabManager.selectedIndex
}

Expand All @@ -646,17 +648,32 @@ class TabTrayController: AuthenticationController {
tabManager.addTabAndSelect(isPrivate: true)
}

let privateModeTabSelected = tabManager.allTabs[safe: tabManager.privateTabSelectedIndex]

if Preferences.Privacy.persistentPrivateBrowsing.value {
tabManager.selectTab(privateModeTabSelected)
}
tabTrayView.hidePrivateModeInfo()
tabTrayView.collectionView.reloadData()

// When you go back from normal mode, current tab should be selected
// in case private tabs are persistent
if Preferences.Privacy.persistentPrivateBrowsing.value {
scrollToSelectedTab(privateModeTabSelected)
}
navigationController?.setNavigationBarHidden(false, animated: false)
}
} else {
tabTrayView.hidePrivateModeInfo()

// When you go back from private mode, a previous current tab is selected
// Reloding the collection view in order to mark the selecte the tab
tabManager.selectTab(tabManager.tabsForCurrentMode[safe: tabManager.normalTabSelectedIndex])
let normalModeTabSelected = tabManager.allTabs[safe: tabManager.normalTabSelectedIndex]

tabManager.selectTab(normalModeTabSelected)
tabTrayView.collectionView.reloadData()

scrollToSelectedTab(normalModeTabSelected)
navigationController?.setNavigationBarHidden(false, animated: false)
}

Expand Down Expand Up @@ -724,6 +741,15 @@ class TabTrayController: AuthenticationController {
present(settingsNavigationController, animated: true)
}

private func scrollToSelectedTab(_ tab: Tab?) {
if let selectedTab = tab,
let selectedIndexPath = dataSource.indexPath(for: selectedTab) {
DispatchQueue.main.async {
self.tabTrayView.collectionView.scrollToItem(at: selectedIndexPath, at: .centeredVertically, animated: false)
}
}
}

@objc private func tappedCollectionViewBackground() {
if traitCollection.horizontalSizeClass == .compact {
doneAction()
Expand Down

0 comments on commit 0a3e173

Please sign in to comment.