|
| 1 | +// |
| 2 | +// StackView.swift |
| 3 | +// ReactiveDataDisplayManager |
| 4 | +// |
| 5 | +// Created by Никита Коробейников on 19.07.2023. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import ReactiveDataDisplayManager |
| 10 | + |
| 11 | +final public class StackView: UIView { |
| 12 | + |
| 13 | + // MARK: - Private Properties |
| 14 | + |
| 15 | + private let stackView = UIStackView() |
| 16 | + private lazy var adapter = stackView.rddm.baseBuilder.build() |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +// MARK: - ConfigurableItem |
| 21 | + |
| 22 | +extension StackView: ConfigurableItem { |
| 23 | + |
| 24 | + // MARK: - Model |
| 25 | + |
| 26 | + public struct Model: AlignmentProvider { |
| 27 | + |
| 28 | + // MARK: - Editor |
| 29 | + |
| 30 | + public struct Property: Editor { |
| 31 | + public typealias Model = StackView.Model |
| 32 | + |
| 33 | + private let closure: (Model) -> Model |
| 34 | + |
| 35 | + public init(closure: @escaping (Model) -> Model) { |
| 36 | + self.closure = closure |
| 37 | + } |
| 38 | + |
| 39 | + public func edit(_ model: Model) -> Model { |
| 40 | + return closure(model) |
| 41 | + } |
| 42 | + |
| 43 | + public static func children(_ value: [StackCellGenerator]) -> Property { |
| 44 | + .init(closure: { model in |
| 45 | + var model = model |
| 46 | + model.set(children: value) |
| 47 | + return model |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + public static func style(_ value: StackStyle) -> Property { |
| 52 | + .init(closure: { model in |
| 53 | + var model = model |
| 54 | + model.set(style: value) |
| 55 | + return model |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + public static func background(_ value: BackgroundStyle?) -> Property { |
| 60 | + .init(closure: { model in |
| 61 | + var model = model |
| 62 | + model.set(background: value) |
| 63 | + return model |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + public static func alignment(_ value: Alignment) -> Property { |
| 68 | + .init(closure: { model in |
| 69 | + var model = model |
| 70 | + model.set(alignment: value) |
| 71 | + return model |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + // MARK: - Public properties |
| 78 | + |
| 79 | + private(set) public var children: [StackCellGenerator] = [] |
| 80 | + private(set) public var style: StackStyle = .init(axis: .horizontal, |
| 81 | + spacing: 0, |
| 82 | + alignment: .fill, |
| 83 | + distribution: .fill) |
| 84 | + private(set) public var background: BackgroundStyle? |
| 85 | + private(set) public var alignment: Alignment = .all(.zero) |
| 86 | + |
| 87 | + // MARK: - Mutation |
| 88 | + |
| 89 | + mutating func set(children: [StackCellGenerator]) { |
| 90 | + self.children = children |
| 91 | + } |
| 92 | + |
| 93 | + mutating func set(style: StackStyle) { |
| 94 | + self.style = style |
| 95 | + } |
| 96 | + |
| 97 | + mutating func set(background: BackgroundStyle?) { |
| 98 | + self.background = background |
| 99 | + } |
| 100 | + |
| 101 | + mutating func set(alignment: Alignment) { |
| 102 | + self.alignment = alignment |
| 103 | + } |
| 104 | + |
| 105 | + // MARK: - Builder |
| 106 | + |
| 107 | + public static func build(@EditorBuilder<Property> content: (Property.Type) -> [Property]) -> Self { |
| 108 | + return content(Property.self).reduce(.init(), { model, editor in |
| 109 | + editor.edit(model) |
| 110 | + }) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // MARK: - Methods |
| 115 | + |
| 116 | + public func configure(with model: Model) { |
| 117 | + configureConstraints() |
| 118 | + apply(style: model.style) |
| 119 | + applyBackground(style: model.background) |
| 120 | + |
| 121 | + adapter -= .all |
| 122 | + adapter += model.children |
| 123 | + adapter => .reload |
| 124 | + } |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +// MARK: - Private |
| 129 | + |
| 130 | +private extension StackView { |
| 131 | + |
| 132 | + func configureConstraints() { |
| 133 | + wrap(subview: stackView, with: .zero) |
| 134 | + } |
| 135 | + |
| 136 | + func apply(style: StackStyle) { |
| 137 | + stackView.axis = style.axis |
| 138 | + stackView.spacing = style.spacing |
| 139 | + stackView.alignment = style.alignment |
| 140 | + stackView.distribution = style.distribution |
| 141 | + } |
| 142 | + |
| 143 | + func applyBackground(style: BackgroundStyle?) { |
| 144 | + switch style { |
| 145 | + case .solid(let color): |
| 146 | + stackView.backgroundColor = color |
| 147 | + case .none: |
| 148 | + stackView.backgroundColor = nil |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments