Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #8058, Fix #8074: Implement tab bar reorder via drag & drop for i…
Browse files Browse the repository at this point in the history
…Pad (#8164)
  • Loading branch information
kylehickinson authored Sep 28, 2023
1 parent 7c3861f commit 43fce87
Showing 1 changed file with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,23 @@ class TabsBarViewController: UIViewController {
super.viewDidLoad()

collectionView.backgroundColor = view.backgroundColor
collectionView.dragDelegate = UIApplication.shared.supportsMultipleScenes ? self : nil
collectionView.dropDelegate = UIApplication.shared.supportsMultipleScenes ? self : nil

tabManager?.addDelegate(self)

// Can't get view.frame inside of lazy property, need to put this code here.
collectionView.frame = view.frame
(collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.itemSize = CGSize(width: UX.TabsBar.minimumWidth, height: view.frame.height)
view.addSubview(collectionView)

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture(gesture:)))
longPressGesture.minimumPressDuration = 0.2
longPressGesture.delaysTouchesBegan = true
collectionView.addGestureRecognizer(longPressGesture)
if UIApplication.shared.supportsMultipleScenes {
collectionView.dragInteractionEnabled = true
collectionView.dragDelegate = self
collectionView.dropDelegate = self
} else {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture(gesture:)))
longPressGesture.minimumPressDuration = 0.2
longPressGesture.delaysTouchesBegan = true
collectionView.addGestureRecognizer(longPressGesture)
}

NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: UIDevice.orientationDidChangeNotification, object: nil)

Expand Down Expand Up @@ -209,7 +212,7 @@ class TabsBarViewController: UIViewController {
}
}

func updateData() {
func updateData(reloadingCollectionView: Bool = true) {
// Don't waste time/resources updating data when we're in the middle of a restore or bulk delete
guard let tabManager = tabManager, !tabManager.isRestoring && !tabManager.isBulkDeleting else {
return
Expand All @@ -218,7 +221,10 @@ class TabsBarViewController: UIViewController {
tabList = WeakList<Tab>(tabManager.tabsForCurrentMode)

overflowIndicators()
reloadDataAndRestoreSelectedTab()

if reloadingCollectionView {
reloadDataAndRestoreSelectedTab()
}
}

func updateSelectedTabTitle() {
Expand Down Expand Up @@ -454,7 +460,6 @@ extension TabsBarViewController: UICollectionViewDragDelegate, UICollectionViewD
}

func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
guard coordinator.items.first?.sourceIndexPath == nil else { return }
let destinationIndexPath: IndexPath

if let indexPath = coordinator.destinationIndexPath {
Expand All @@ -468,10 +473,27 @@ extension TabsBarViewController: UICollectionViewDragDelegate, UICollectionViewD
if coordinator.proposal.operation == .move {
guard let item = coordinator.items.first else { return }

// TODO: Figure out how to get the item here...
// TODO: Figure out how to get the item here from other Brave scene...
// LocalObject is nil because the tab is in another process? :S
if let sourceIndexPath = item.sourceIndexPath {
guard let manager = tabManager, let fromTab = tabList[sourceIndexPath.row],
let toTab = tabList[destinationIndexPath.row]
else { return }

// Find original from/to index... we need to target the full list not partial.
let tabs = manager.tabsForCurrentMode
guard let to = tabs.firstIndex(where: { $0 === toTab }) else { return }

manager.moveTab(fromTab, toIndex: to)
updateData(reloadingCollectionView: false)
collectionView.moveItem(at: sourceIndexPath, to: destinationIndexPath)
collectionView.reloadSections(IndexSet(integer: 0)) // Updates selection states
guard let selectedTab = tabList[destinationIndexPath.row] else { return }
manager.selectTab(selectedTab)
}

_ = coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
reloadDataAndRestoreSelectedTab()
}
}

Expand Down

0 comments on commit 43fce87

Please sign in to comment.