Files
Attractor/App/Tabs/TabPickerViewController.swift
2022-08-05 15:56:30 -07:00

234 lines
8.9 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 static var localHostIdentifier = "__localhost__";
public var selectedTabIdentifier: UUID?
public var selectedTabHost: String? { didSet { didChangeSelectedTabHost(selectedTabHost!) } }
weak var delegate: TabPickerViewControllerDelegate?
public var tabObserver: AnyCancellable?
private var selectedTabIdentifiersForEditing: Set<UUID> = []
private var tabIdentifiersByHost: [String: [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() }
}
public lazy var newTabButton: UIBarButtonItem = {
UIBarButtonItem(systemItem: .add, primaryAction: UIAction(handler: { [unowned self] _ in
self.delegate?.tabPicker(self, createNewTabWithURL: nil)
}), menu: nil)
}()
private lazy var deleteTabButton: UIBarButtonItem = {
UIBarButtonItem(systemItem: .trash, primaryAction: UIAction(handler: { [unowned self] _ in
deleteSelectedTabs()
}), menu: nil)
}()
private lazy var hostPickerButton: UIButton = {
var buttonConfiguration = UIButton.Configuration.filled()
buttonConfiguration.title = "Host"
let button = UIButton(configuration: buttonConfiguration)
button.changesSelectionAsPrimaryAction = true
button.showsMenuAsPrimaryAction = true
return button
}()
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)
}
public func setTabIdentifiers(_ identifiers: [UUID], forHost host: String) {
tabIdentifiersByHost[host] = identifiers
if host == selectedTabHost {
var snapshot = dataSource.snapshot()
snapshot.deleteAllItems()
snapshot.appendSections([ 0 ])
snapshot.appendItems(identifiers)
dataSource.apply(snapshot)
}
reloadHostPickerButtonMenu()
}
private func reloadHostPickerButtonMenu() {
var menuChildren: [UIAction] = []
for host in tabIdentifiersByHost.keys {
menuChildren.append(UIAction(title: host, handler: { [unowned self] _ in
selectedTabHost = host
}))
}
hostPickerButton.menu = UIMenu(children: menuChildren)
if tabIdentifiersByHost.keys.count > 0 && tabIdentifiersByHost.keys.first != Self.localHostIdentifier {
navigationItem.titleView = hostPickerButton
} else {
navigationItem.titleView = nil
}
}
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)
}
private func didChangeSelectedTabHost(_ tabHost: String) {
guard let tabIdentifiers = tabIdentifiersByHost[tabHost] else { return }
var snapshot = dataSource.snapshot()
snapshot.deleteAllItems()
snapshot.appendSections([ 0 ])
snapshot.appendItems(tabIdentifiers)
dataSource.applySnapshotUsingReloadData(snapshot)
}
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
}
}