From 54c8453ffceebbc834d5235d4efbf5582ba5d490 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 22 Feb 2022 22:01:39 -0800 Subject: [PATCH] RedirectRulesSettingsViewController: spruce up appearance --- .../RedirectRulesSettingsViewController.swift | 93 +++++++++++++++---- 1 file changed, 75 insertions(+), 18 deletions(-) diff --git a/App/Settings/RedirectRulesSettingsViewController.swift b/App/Settings/RedirectRulesSettingsViewController.swift index 94e7755..281e2fd 100644 --- a/App/Settings/RedirectRulesSettingsViewController.swift +++ b/App/Settings/RedirectRulesSettingsViewController.swift @@ -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(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 = { - let registry = UICollectionView.CellRegistration { [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 { [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(collectionView: collectionView) { (collectionView, indexPath, itemIdentifier) in - collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: itemIdentifier) + let headerCellRegistry = UICollectionView.CellRegistration { [unowned self] cell, indexPath, item in + let config = ImageViewCellConfiguration(imageView: sectionHeaderImageView) + cell.contentConfiguration = config + } + + return UICollectionViewDiffableDataSource(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) }