RedirectRulesSettingsViewController: spruce up appearance

This commit is contained in:
James Magahern
2022-02-22 22:01:39 -08:00
parent 77df1d66b6
commit 54c8453ffc

View File

@@ -117,15 +117,17 @@ class RedirectRulesSettingsViewController: UICollectionViewController
class CreateRedirectRuleViewController: UICollectionViewController
{
enum Section {
case hosts
}
enum Item: String {
enum Section: Int {
case header
case fromHost
case toHost
}
enum Item: Hashable {
case header(section: Section)
case textField(section: Section)
}
struct TextFieldCellConfiguration: UIContentConfiguration {
var textField: UITextField
@@ -138,30 +140,76 @@ class CreateRedirectRuleViewController: UICollectionViewController
}
}
struct ImageViewCellConfiguration: UIContentConfiguration {
var imageView: UIImageView
func makeContentView() -> UIView & UIContentView {
return GenericContentView<UIImageView, Self>(configuration: self, view: imageView) { _,_ in }
}
func updated(for state: UIConfigurationState) -> Self {
self
}
}
public var finishedCreatingRule: ((_ fromHost: String, _ toHost: String) -> Void)?
private let sectionHeaderImageView = UIImageView(image: .init(systemName: "arrowshape.zigzag.forward"))
private let fromHostField = UITextField(frame: .zero)
private let toHostField = UITextField(frame: .zero)
private let doneButton = UIBarButtonItem(systemItem: .done, primaryAction: nil, menu: nil)
private lazy var dataSource: UICollectionViewDiffableDataSource<Section, Item> = {
let registry = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { [unowned self] cell, indexPath, item in
if item == Item.fromHost {
cell.contentConfiguration = TextFieldCellConfiguration(textField: fromHostField)
} else if item == Item.toHost {
cell.contentConfiguration = TextFieldCellConfiguration(textField: toHostField)
let listCellRegistry = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { [unowned self] cell, indexPath, item in
let section = dataSource.snapshot().sectionIdentifier(containingItem: item)
switch item {
case .header(_):
var contentConfig = cell.defaultContentConfiguration()
contentConfig.text = section == .fromHost ? "From Host" : "To Host"
cell.contentConfiguration = contentConfig
case .textField(_):
cell.contentConfiguration = TextFieldCellConfiguration(textField:
section == .fromHost ? fromHostField
: toHostField)
}
}
return UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { (collectionView, indexPath, itemIdentifier) in
collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: itemIdentifier)
let headerCellRegistry = UICollectionView.CellRegistration<UICollectionViewCell, Item> { [unowned self] cell, indexPath, item in
let config = ImageViewCellConfiguration(imageView: sectionHeaderImageView)
cell.contentConfiguration = config
}
return UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { [unowned self] (collectionView, indexPath, itemIdentifier) in
let section = self.dataSource.sectionIdentifier(for: indexPath.section)
if section == .header {
return collectionView.dequeueConfiguredReusableCell(using: headerCellRegistry, for: indexPath, item: itemIdentifier)
} else {
return collectionView.dequeueConfiguredReusableCell(using: listCellRegistry, for: indexPath, item: itemIdentifier)
}
}
}()
init() {
let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
let layout = UICollectionViewCompositionalLayout.list(using: config)
var config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
config.headerMode = .firstItemInSection
let layout = UICollectionViewCompositionalLayout { (sectionIndex, environment) in
let section = Section(rawValue: sectionIndex)
if section == .header {
let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(80.0))
let group = NSCollectionLayoutGroup.vertical(
layoutSize: size,
subitems: [
NSCollectionLayoutItem(layoutSize: size)
])
return NSCollectionLayoutSection(group: group)
} else {
return NSCollectionLayoutSection.list(using: config, layoutEnvironment: environment)
}
}
super.init(collectionViewLayout: layout)
preferredContentSize = CGSize(width: -1, height: 320.0)
@@ -179,8 +227,15 @@ class CreateRedirectRuleViewController: UICollectionViewController
navigationItem.rightBarButtonItem = doneButton
fromHostField.placeholder = "From Host"
toHostField.placeholder = "To Host"
sectionHeaderImageView.contentMode = .scaleAspectFit
fromHostField.placeholder = "https://fromhostname.com"
fromHostField.autocorrectionType = .no
fromHostField.autocapitalizationType = .none
toHostField.placeholder = "https://tohostname.com"
toHostField.autocorrectionType = .no
toHostField.autocapitalizationType = .none
}
required init?(coder: NSCoder) {
@@ -189,8 +244,10 @@ class CreateRedirectRuleViewController: UICollectionViewController
override func viewDidLoad() {
var snapshot = dataSource.snapshot()
snapshot.appendSections([ Section.hosts ])
snapshot.appendItems([ Item.fromHost, Item.toHost ], toSection: .hosts)
snapshot.appendSections([ Section.header, Section.fromHost, Section.toHost ])
snapshot.appendItems([ .header(section: .header) ], toSection: .header)
snapshot.appendItems([ .header(section: .fromHost), .textField(section: .fromHost) ], toSection: .fromHost)
snapshot.appendItems([ .header(section: .toHost), .textField(section: .toHost) ], toSection: .toHost)
dataSource.applySnapshotUsingReloadData(snapshot)
}