diff --git a/Client/Frontend/Browser/TabTrayController.swift b/Client/Frontend/Browser/TabTrayController.swift index 952c7bef9b4..c24a16185a2 100644 --- a/Client/Frontend/Browser/TabTrayController.swift +++ b/Client/Frontend/Browser/TabTrayController.swift @@ -663,18 +663,14 @@ extension TabTrayController: TabManagerDelegate { } tabDataSource.addTab(tab) - if navigationController?.visibleViewController === self { - self.collectionView?.performBatchUpdates({ - self.collectionView.insertItems(at: [IndexPath(item: index, section: 0)]) - }, completion: { finished in - if finished { - tabManager.selectTab(tab) - // don't pop the tab tray view controller if it is not in the foreground - if self.presentedViewController == nil { - _ = self.navigationController?.popViewController(animated: true) - } + + insertTabToCollection(tab, index: index) { + if let controller = self.navigationController?.visibleViewController, controller is TabTrayController { + // don't pop the tab tray view controller if it is not in the foreground + if self.presentedViewController == nil { + _ = self.navigationController?.popViewController(animated: true) } - }) + } } } @@ -712,6 +708,18 @@ extension TabTrayController: TabManagerDelegate { }) } } + + private func insertTabToCollection(_ tab: Tab, index: Int, completion: @escaping () -> Void ) { + collectionView?.performBatchUpdates({ + self.collectionView.insertItems(at: [IndexPath(item: index, section: 0)]) + }, completion: { finished in + if finished { + self.tabManager.selectTab(tab) + + completion() + } + }) + } } extension TabTrayController: UIScrollViewAccessibilityDelegate {