TabPickerViewController: Refactor to not reference tab controller directly.
This commit is contained in:
@@ -6,25 +6,83 @@
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import Combine
|
||||
|
||||
protocol TabPickerViewControllerDelegate: AnyObject
|
||||
{
|
||||
func tabPicker(_ picker: TabPickerViewController, didSelectTab tab: Tab)
|
||||
func tabPicker(_ picker: TabPickerViewController, willCloseTab tab: Tab)
|
||||
func tabPicker(_ picker: TabPickerViewController, createNewTabWithURL: URL?)
|
||||
func tabPicker(_ picker: TabPickerViewController, didSelectTabIdentifier tab: UUID)
|
||||
func tabPicker(_ picker: TabPickerViewController, closeTabWithIdentifier tab: UUID)
|
||||
func tabPicker(_ picker: TabPickerViewController, tabInfoForIdentifier: UUID) -> TabInfo
|
||||
}
|
||||
|
||||
class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
{
|
||||
let tabController: TabController!
|
||||
var selectedTab: Tab?
|
||||
|
||||
weak var delegate: TabPickerViewControllerDelegate?
|
||||
|
||||
typealias TabID = UUID
|
||||
|
||||
private var selectedTabsForEditing: Set<Tab> = []
|
||||
private var collectionView: UICollectionView?
|
||||
private var dataSource: UICollectionViewDiffableDataSource<Int, TabID>?
|
||||
public var selectedTabIdentifier: UUID?
|
||||
public var tabIdentifiers: [ UUID ] = [] {
|
||||
didSet {
|
||||
var snapshot = dataSource.snapshot()
|
||||
snapshot.deleteAllItems()
|
||||
snapshot.appendSections([ 0 ])
|
||||
snapshot.appendItems(tabIdentifiers)
|
||||
dataSource.apply(snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
weak var delegate: TabPickerViewControllerDelegate?
|
||||
public var tabObserver: AnyCancellable?
|
||||
private var selectedTabIdentifiersForEditing: Set<UUID> = []
|
||||
|
||||
private var listConfiguration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
|
||||
private lazy var listLayout = UICollectionViewCompositionalLayout.list(using: listConfiguration)
|
||||
private lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout).conf { collectionView in
|
||||
collectionView.allowsMultipleSelectionDuringEditing = true
|
||||
collectionView.backgroundColor = .systemGroupedBackground
|
||||
collectionView.delegate = self
|
||||
}
|
||||
|
||||
private lazy var cellRegistry = UICollectionView.CellRegistration<UICollectionViewListCell, TabID> { [unowned self] (listCell, indexPath, item) in
|
||||
var config = listCell.defaultContentConfiguration()
|
||||
|
||||
if let tab = delegate?.tabPicker(self, tabInfoForIdentifier: item) {
|
||||
if let title = tab.title, title.count > 0 {
|
||||
config.text = title
|
||||
config.secondaryText = tab.url?.absoluteString
|
||||
} else if let url = tab.url {
|
||||
config.text = url.absoluteString
|
||||
config.secondaryText = url.absoluteString
|
||||
} else {
|
||||
config.text = "New Tab"
|
||||
}
|
||||
|
||||
config.textProperties.numberOfLines = 1
|
||||
config.secondaryTextProperties.numberOfLines = 1
|
||||
|
||||
if let image = tab.favicon {
|
||||
config.image = image
|
||||
} else {
|
||||
config.image = UIImage(systemName: "safari")
|
||||
}
|
||||
|
||||
config.imageProperties.maximumSize = CGSize(width: 21.0, height: 21.0)
|
||||
config.imageProperties.cornerRadius = 3.0
|
||||
|
||||
if let selectedTabIdentifier, selectedTabIdentifier == item {
|
||||
listCell.accessories = [ .checkmark() ]
|
||||
} else {
|
||||
listCell.accessories = []
|
||||
}
|
||||
}
|
||||
|
||||
listCell.contentConfiguration = config
|
||||
}
|
||||
|
||||
private lazy var dataSource = UICollectionViewDiffableDataSource<Int, TabID>(collectionView: collectionView)
|
||||
{ [unowned self] (collectionView, indexPath, item) -> UICollectionViewCell? in
|
||||
return collectionView.dequeueConfiguredReusableCell(using: cellRegistry, for: indexPath, item: item)
|
||||
}
|
||||
|
||||
override var traitCollection: UITraitCollection {
|
||||
get { return super.traitCollection.alwaysPadLike() }
|
||||
@@ -32,8 +90,7 @@ class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
|
||||
private lazy var newTabButton: UIBarButtonItem = {
|
||||
UIBarButtonItem(systemItem: .add, primaryAction: UIAction(handler: { [unowned self] _ in
|
||||
let newTab = self.tabController.createNewTab(url: nil)
|
||||
self.delegate?.tabPicker(self, didSelectTab: newTab)
|
||||
self.delegate?.tabPicker(self, createNewTabWithURL: nil)
|
||||
}), menu: nil)
|
||||
}()
|
||||
|
||||
@@ -43,10 +100,8 @@ class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
}), menu: nil)
|
||||
}()
|
||||
|
||||
init(tabController: TabController) {
|
||||
self.tabController = tabController
|
||||
init() {
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
|
||||
self.title = "Tabs"
|
||||
}
|
||||
|
||||
@@ -55,83 +110,21 @@ class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
}
|
||||
|
||||
override func loadView() {
|
||||
var listConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
|
||||
listConfig.trailingSwipeActionsConfigurationProvider = { [unowned self] indexPath in
|
||||
if self.dataSource?.snapshot().numberOfItems ?? 0 <= 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load this lazy var now.
|
||||
_ = cellRegistry.self
|
||||
|
||||
listConfiguration.trailingSwipeActionsConfigurationProvider = { [unowned self] indexPath in
|
||||
return UISwipeActionsConfiguration(actions: [ UIContextualAction(style: .destructive, title: "Close", handler: { [unowned self] (action, view, completionHandler) in
|
||||
if let item = self.dataSource?.itemIdentifier(for: indexPath), var snapshot = self.dataSource?.snapshot() {
|
||||
if let tab = self.tabController.tab(forIdentifier: item) {
|
||||
self.delegate?.tabPicker(self, willCloseTab: tab)
|
||||
|
||||
self.tabController.closeTab(tab)
|
||||
snapshot.deleteItems([ item ])
|
||||
self.dataSource?.apply(snapshot, animatingDifferences: true)
|
||||
}
|
||||
if let item = dataSource.itemIdentifier(for: indexPath) {
|
||||
var snapshot = dataSource.snapshot()
|
||||
delegate?.tabPicker(self, closeTabWithIdentifier: item)
|
||||
|
||||
snapshot.deleteItems([ item ])
|
||||
dataSource.apply(snapshot, animatingDifferences: true)
|
||||
}
|
||||
})])
|
||||
}
|
||||
|
||||
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfig)
|
||||
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
|
||||
collectionView.allowsMultipleSelectionDuringEditing = true
|
||||
collectionView.backgroundColor = .systemGroupedBackground
|
||||
|
||||
let registry = UICollectionView.CellRegistration<UICollectionViewListCell, TabID> { [unowned self] (listCell, indexPath, item) in
|
||||
var config = listCell.defaultContentConfiguration()
|
||||
|
||||
if let tab = self.tabController.tab(forIdentifier: item) {
|
||||
if let title = tab.title, title.count > 0 {
|
||||
config.text = title
|
||||
config.secondaryText = tab.url?.absoluteString
|
||||
} else if let url = tab.url {
|
||||
config.text = url.absoluteString
|
||||
config.secondaryText = url.absoluteString
|
||||
} else {
|
||||
config.text = "New Tab"
|
||||
}
|
||||
|
||||
config.textProperties.numberOfLines = 1
|
||||
config.secondaryTextProperties.numberOfLines = 1
|
||||
|
||||
if let image = tab.favicon {
|
||||
config.image = image
|
||||
} else {
|
||||
config.image = UIImage(systemName: "safari")
|
||||
}
|
||||
|
||||
config.imageProperties.maximumSize = CGSize(width: 21.0, height: 21.0)
|
||||
config.imageProperties.cornerRadius = 3.0
|
||||
|
||||
if self.selectedTab == tab {
|
||||
listCell.accessories = [ .checkmark() ]
|
||||
} else {
|
||||
listCell.accessories = []
|
||||
}
|
||||
}
|
||||
|
||||
listCell.contentConfiguration = config
|
||||
}
|
||||
|
||||
let dataSource = UICollectionViewDiffableDataSource<Int, TabID>(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
|
||||
return collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: item)
|
||||
}
|
||||
|
||||
collectionView.dataSource = dataSource
|
||||
collectionView.delegate = self
|
||||
|
||||
var snapshot = dataSource.snapshot()
|
||||
snapshot.appendSections([ 0 ])
|
||||
tabController.tabs.forEach { tab in
|
||||
snapshot.appendItems([ tab.identifier ])
|
||||
}
|
||||
|
||||
dataSource.apply(snapshot)
|
||||
|
||||
self.dataSource = dataSource
|
||||
self.collectionView = collectionView
|
||||
|
||||
self.view = self.collectionView
|
||||
|
||||
configureNavigationButtons(forEditing: isEditing)
|
||||
@@ -141,7 +134,7 @@ class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
if !forEditing {
|
||||
navigationItem.rightBarButtonItem = newTabButton
|
||||
} else {
|
||||
deleteTabButton.isEnabled = collectionView?.indexPathsForSelectedItems?.count ?? 0 > 0
|
||||
deleteTabButton.isEnabled = collectionView.indexPathsForSelectedItems?.count ?? 0 > 0
|
||||
navigationItem.rightBarButtonItem = deleteTabButton
|
||||
}
|
||||
|
||||
@@ -150,43 +143,37 @@ class TabPickerViewController: UIViewController, UICollectionViewDelegate
|
||||
|
||||
override func setEditing(_ editing: Bool, animated: Bool) {
|
||||
super.setEditing(editing, animated: animated)
|
||||
|
||||
if let collectionView = collectionView {
|
||||
collectionView.isEditing = editing
|
||||
}
|
||||
collectionView.isEditing = editing
|
||||
|
||||
configureNavigationButtons(forEditing: editing)
|
||||
}
|
||||
|
||||
private func deleteSelectedTabs() {
|
||||
guard let dataSource = dataSource else { return }
|
||||
|
||||
var snapshot = dataSource.snapshot()
|
||||
for tab in selectedTabsForEditing {
|
||||
snapshot.deleteItems([ tab.identifier ])
|
||||
self.delegate?.tabPicker(self, willCloseTab: tab)
|
||||
self.tabController.closeTab(tab)
|
||||
for tab in selectedTabIdentifiersForEditing {
|
||||
snapshot.deleteItems([ tab ])
|
||||
delegate?.tabPicker(self, closeTabWithIdentifier: tab)
|
||||
}
|
||||
|
||||
dataSource.apply(snapshot, animatingDifferences: true)
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
let tab = tabController.tabs[indexPath.row]
|
||||
guard let tab = dataSource.itemIdentifier(for: indexPath) else { return }
|
||||
|
||||
if !isEditing {
|
||||
delegate?.tabPicker(self, didSelectTab: tab)
|
||||
delegate?.tabPicker(self, didSelectTabIdentifier: tab)
|
||||
} else {
|
||||
deleteTabButton.isEnabled = collectionView.indexPathsForSelectedItems?.count ?? 0 > 0
|
||||
selectedTabsForEditing.update(with: tab)
|
||||
selectedTabIdentifiersForEditing.update(with: tab)
|
||||
}
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
|
||||
guard let tabIdentifier = dataSource.itemIdentifier(for: indexPath) else { return }
|
||||
|
||||
if isEditing {
|
||||
let tab = tabController.tabs[indexPath.row]
|
||||
selectedTabsForEditing.remove(tab)
|
||||
|
||||
selectedTabIdentifiersForEditing.remove(tabIdentifier)
|
||||
deleteTabButton.isEnabled = collectionView.indexPathsForSelectedItems?.count ?? 0 > 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user