RedirectRulesSettingsViewController: spruce up appearance
This commit is contained in:
@@ -117,15 +117,17 @@ class RedirectRulesSettingsViewController: UICollectionViewController
|
|||||||
|
|
||||||
class CreateRedirectRuleViewController: UICollectionViewController
|
class CreateRedirectRuleViewController: UICollectionViewController
|
||||||
{
|
{
|
||||||
enum Section {
|
enum Section: Int {
|
||||||
case hosts
|
case header
|
||||||
}
|
|
||||||
|
|
||||||
enum Item: String {
|
|
||||||
case fromHost
|
case fromHost
|
||||||
case toHost
|
case toHost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Item: Hashable {
|
||||||
|
case header(section: Section)
|
||||||
|
case textField(section: Section)
|
||||||
|
}
|
||||||
|
|
||||||
struct TextFieldCellConfiguration: UIContentConfiguration {
|
struct TextFieldCellConfiguration: UIContentConfiguration {
|
||||||
var textField: UITextField
|
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)?
|
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 fromHostField = UITextField(frame: .zero)
|
||||||
private let toHostField = UITextField(frame: .zero)
|
private let toHostField = UITextField(frame: .zero)
|
||||||
|
|
||||||
private let doneButton = UIBarButtonItem(systemItem: .done, primaryAction: nil, menu: nil)
|
private let doneButton = UIBarButtonItem(systemItem: .done, primaryAction: nil, menu: nil)
|
||||||
|
|
||||||
private lazy var dataSource: UICollectionViewDiffableDataSource<Section, Item> = {
|
private lazy var dataSource: UICollectionViewDiffableDataSource<Section, Item> = {
|
||||||
let registry = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { [unowned self] cell, indexPath, item in
|
let listCellRegistry = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { [unowned self] cell, indexPath, item in
|
||||||
if item == Item.fromHost {
|
let section = dataSource.snapshot().sectionIdentifier(containingItem: item)
|
||||||
cell.contentConfiguration = TextFieldCellConfiguration(textField: fromHostField)
|
|
||||||
} else if item == Item.toHost {
|
switch item {
|
||||||
cell.contentConfiguration = TextFieldCellConfiguration(textField: toHostField)
|
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
|
let headerCellRegistry = UICollectionView.CellRegistration<UICollectionViewCell, Item> { [unowned self] cell, indexPath, item in
|
||||||
collectionView.dequeueConfiguredReusableCell(using: registry, for: indexPath, item: itemIdentifier)
|
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() {
|
init() {
|
||||||
let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
|
var config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
|
||||||
let layout = UICollectionViewCompositionalLayout.list(using: config)
|
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)
|
super.init(collectionViewLayout: layout)
|
||||||
|
|
||||||
preferredContentSize = CGSize(width: -1, height: 320.0)
|
preferredContentSize = CGSize(width: -1, height: 320.0)
|
||||||
@@ -179,8 +227,15 @@ class CreateRedirectRuleViewController: UICollectionViewController
|
|||||||
|
|
||||||
navigationItem.rightBarButtonItem = doneButton
|
navigationItem.rightBarButtonItem = doneButton
|
||||||
|
|
||||||
fromHostField.placeholder = "From Host"
|
sectionHeaderImageView.contentMode = .scaleAspectFit
|
||||||
toHostField.placeholder = "To Host"
|
|
||||||
|
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) {
|
required init?(coder: NSCoder) {
|
||||||
@@ -189,8 +244,10 @@ class CreateRedirectRuleViewController: UICollectionViewController
|
|||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
var snapshot = dataSource.snapshot()
|
var snapshot = dataSource.snapshot()
|
||||||
snapshot.appendSections([ Section.hosts ])
|
snapshot.appendSections([ Section.header, Section.fromHost, Section.toHost ])
|
||||||
snapshot.appendItems([ Item.fromHost, Item.toHost ], toSection: .hosts)
|
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)
|
dataSource.applySnapshotUsingReloadData(snapshot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user