189 lines
7.2 KiB
Swift
189 lines
7.2 KiB
Swift
//
|
|
// TabPickerViewController.swift
|
|
// SBrowser
|
|
//
|
|
// Created by James Magahern on 7/30/20.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
|
|
protocol TabPickerViewControllerDelegate: AnyObject
|
|
{
|
|
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
|
|
{
|
|
typealias TabID = UUID
|
|
|
|
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() }
|
|
}
|
|
|
|
private lazy var newTabButton: UIBarButtonItem = {
|
|
UIBarButtonItem(systemItem: .add, primaryAction: UIAction(handler: { [unowned self] _ in
|
|
self.delegate?.tabPicker(self, createNewTabWithURL: nil)
|
|
}), menu: nil)
|
|
}()
|
|
|
|
lazy var deleteTabButton: UIBarButtonItem = {
|
|
UIBarButtonItem(systemItem: .trash, primaryAction: UIAction(handler: { [unowned self] _ in
|
|
deleteSelectedTabs()
|
|
}), menu: nil)
|
|
}()
|
|
|
|
init() {
|
|
super.init(nibName: nil, bundle: nil)
|
|
self.title = "Tabs"
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func loadView() {
|
|
// 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 = dataSource.itemIdentifier(for: indexPath) {
|
|
var snapshot = dataSource.snapshot()
|
|
delegate?.tabPicker(self, closeTabWithIdentifier: item)
|
|
|
|
snapshot.deleteItems([ item ])
|
|
dataSource.apply(snapshot, animatingDifferences: true)
|
|
}
|
|
})])
|
|
}
|
|
|
|
self.view = self.collectionView
|
|
|
|
configureNavigationButtons(forEditing: isEditing)
|
|
}
|
|
|
|
private func configureNavigationButtons(forEditing: Bool) {
|
|
if !forEditing {
|
|
navigationItem.rightBarButtonItem = newTabButton
|
|
} else {
|
|
deleteTabButton.isEnabled = collectionView.indexPathsForSelectedItems?.count ?? 0 > 0
|
|
navigationItem.rightBarButtonItem = deleteTabButton
|
|
}
|
|
|
|
navigationItem.leftBarButtonItem = editButtonItem
|
|
}
|
|
|
|
override func setEditing(_ editing: Bool, animated: Bool) {
|
|
super.setEditing(editing, animated: animated)
|
|
collectionView.isEditing = editing
|
|
|
|
configureNavigationButtons(forEditing: editing)
|
|
}
|
|
|
|
private func deleteSelectedTabs() {
|
|
var snapshot = dataSource.snapshot()
|
|
for tab in selectedTabIdentifiersForEditing {
|
|
snapshot.deleteItems([ tab ])
|
|
delegate?.tabPicker(self, closeTabWithIdentifier: tab)
|
|
}
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: true)
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
guard let tab = dataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
if !isEditing {
|
|
delegate?.tabPicker(self, didSelectTabIdentifier: tab)
|
|
} else {
|
|
deleteTabButton.isEnabled = collectionView.indexPathsForSelectedItems?.count ?? 0 > 0
|
|
selectedTabIdentifiersForEditing.update(with: tab)
|
|
}
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
|
|
guard let tabIdentifier = dataSource.itemIdentifier(for: indexPath) else { return }
|
|
|
|
if isEditing {
|
|
selectedTabIdentifiersForEditing.remove(tabIdentifier)
|
|
deleteTabButton.isEnabled = collectionView.indexPathsForSelectedItems?.count ?? 0 > 0
|
|
}
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, shouldBeginMultipleSelectionInteractionAt indexPath: IndexPath) -> Bool {
|
|
true
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didBeginMultipleSelectionInteractionAt indexPath: IndexPath) {
|
|
isEditing = true
|
|
}
|
|
}
|