|
| 1 | +// Created by Tyler Hedrick on 3/17/21. |
| 2 | +// Copyright © 2021 Airbnb Inc. All rights reserved. |
| 3 | + |
| 4 | +import EpoxyLayoutGroups |
| 5 | +import UIKit |
| 6 | + |
| 7 | +final class ComplexDeclarativeViewController: UIViewController { |
| 8 | + |
| 9 | + // MARK: Lifecycle |
| 10 | + |
| 11 | + init() { |
| 12 | + super.init(nibName: nil, bundle: nil) |
| 13 | + } |
| 14 | + |
| 15 | + required init?(coder: NSCoder) { |
| 16 | + fatalError("init(coder:) has not been implemented") |
| 17 | + } |
| 18 | + |
| 19 | + // MARK: Internal |
| 20 | + |
| 21 | + override func viewDidLoad() { |
| 22 | + super.viewDidLoad() |
| 23 | + |
| 24 | + let scrollView = UIScrollView() |
| 25 | + scrollView.translatesAutoresizingMaskIntoConstraints = false |
| 26 | + scrollView.backgroundColor = .systemBackground |
| 27 | + scrollView.layoutMargins = UIEdgeInsets(top: 24, left: 24, bottom: 24, right: 24) |
| 28 | + view.addSubview(scrollView) |
| 29 | + scrollView.constrainToSuperview() |
| 30 | + |
| 31 | + group.install(in: scrollView) |
| 32 | + NSLayoutConstraint.activate([ |
| 33 | + group.leadingAnchor.constraint(equalTo: scrollView.layoutMarginsGuide.leadingAnchor), |
| 34 | + group.trailingAnchor.constraint(equalTo: scrollView.layoutMarginsGuide.trailingAnchor), |
| 35 | + group.topAnchor.constraint(greaterThanOrEqualTo: scrollView.layoutMarginsGuide.topAnchor), |
| 36 | + group.bottomAnchor.constraint(lessThanOrEqualTo: scrollView.layoutMarginsGuide.bottomAnchor), |
| 37 | + group.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor), |
| 38 | + ]) |
| 39 | + |
| 40 | + updateGroup() |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: Private |
| 44 | + |
| 45 | + private enum DataID { |
| 46 | + case button |
| 47 | + case spacer |
| 48 | + } |
| 49 | + |
| 50 | + private lazy var group = VGroup(alignment: .fill, spacing: 8) |
| 51 | + |
| 52 | + private func updateGroup() { |
| 53 | + group.setItems({ |
| 54 | + Button.groupItem( |
| 55 | + dataID: DataID.button, |
| 56 | + content: .init(title: "Shuffle"), |
| 57 | + behaviors: .init { [weak self] _ in |
| 58 | + self?.updateGroup() |
| 59 | + }, |
| 60 | + style: .init()) |
| 61 | + randomColorItems() |
| 62 | + }, animated: true) |
| 63 | + } |
| 64 | + |
| 65 | + private func randomColorItems() -> [GroupItemModeling] { |
| 66 | + let possibleItems = [ |
| 67 | + ("Red", UIColor.systemRed), |
| 68 | + ("Orange", UIColor.systemOrange), |
| 69 | + ("Yellow", UIColor.systemYellow), |
| 70 | + ("Green", UIColor.systemGreen), |
| 71 | + ("Blue", UIColor.systemBlue), |
| 72 | + ("Purple", UIColor.systemPurple), |
| 73 | + ] |
| 74 | + let numberOfItems = Int.random(in: 1..<possibleItems.count) |
| 75 | + let allIndicies = Array(0...numberOfItems).shuffled() |
| 76 | + |
| 77 | + return allIndicies.map { index in |
| 78 | + let color = possibleItems[index] |
| 79 | + return HGroupItem( |
| 80 | + dataID: color.0, |
| 81 | + style: .init(spacing: 8)) |
| 82 | + { |
| 83 | + Label.groupItem( |
| 84 | + dataID: color.0 + "label", |
| 85 | + content: color.0, |
| 86 | + style: .style(with: .title3)) |
| 87 | + SpacerItem(dataID: DataID.spacer) |
| 88 | + ColorView.groupItem( |
| 89 | + dataID: color.1, |
| 90 | + style: .init(size: .init(width: 44, height: 44), color: color.1)) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments