Files
Attractor/App/Autocomplete/AutocompleteViewController.swift

98 lines
3.3 KiB
Swift
Raw Normal View History

2020-09-21 17:56:22 -07:00
//
// AutocompleteViewController.swift
// App
//
// Created by James Magahern on 9/21/20.
//
import UIKit
protocol AutocompleteViewControllerDelegate: class {
func autocompleteController(_: AutocompleteViewController, didSelectHistoryItem: HistoryItem)
}
class AutocompleteViewController: UIViewController, UICollectionViewDelegate
{
public var historyItems: [HistoryItem] = [] {
didSet {
var snapshot = dataSource.snapshot()
snapshot.deleteAllItems()
snapshot.appendSections([ .HistoryItems ])
snapshot.appendItems(historyItems, toSection: .HistoryItems)
dataSource.apply(snapshot, animatingDifferences: false)
}
}
public weak var delegate: AutocompleteViewControllerDelegate?
private enum Section: Int {
case HistoryItems
}
public let collectionView: UICollectionView
2020-09-21 18:09:00 -07:00
private let autocompleteView: AutocompleteView
2020-09-21 17:56:22 -07:00
private let dataSource: UICollectionViewDiffableDataSource<Section, HistoryItem>
init() {
let listConfiguration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
let listLayout = UICollectionViewCompositionalLayout.list(using: listConfiguration)
collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout)
let cellRegistry = UICollectionView.CellRegistration<UICollectionViewListCell, HistoryItem> { (cell, indexPath, item) in
var config = cell.defaultContentConfiguration()
config.text = item.title
config.secondaryText = item.url.absoluteString
cell.contentConfiguration = config
}
dataSource = UICollectionViewDiffableDataSource<Section, HistoryItem>(collectionView: collectionView, cellProvider:
{ (collectionView, indexPath, item) -> UICollectionViewCell? in
collectionView.dequeueConfiguredReusableCell(using: cellRegistry, for: indexPath, item: item)
})
2020-09-21 18:09:00 -07:00
autocompleteView = AutocompleteView(collectionView: collectionView)
2020-09-21 17:56:22 -07:00
super.init(nibName: nil, bundle: nil)
collectionView.delegate = self
view.backgroundColor = .systemBackground
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func loadView() {
2020-09-21 18:09:00 -07:00
self.view = autocompleteView
2020-09-21 17:56:22 -07:00
}
// MARK: UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
if let item = dataSource.itemIdentifier(for: indexPath) {
delegate?.autocompleteController(self, didSelectHistoryItem: item)
}
}
2020-09-21 18:09:00 -07:00
private class AutocompleteView: UIView {
let collectionView: UICollectionView
init(collectionView: UICollectionView) {
self.collectionView = collectionView
super.init(frame: .zero)
addSubview(collectionView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.frame = bounds
}
}
2020-09-21 17:56:22 -07:00
}