|
1 | 1 | import UIKit
|
2 | 2 |
|
3 |
| -extension MondrianNamespace where Base : UIView { |
| 3 | +@resultBuilder |
| 4 | +public enum EntrypointBuilder { |
4 | 5 |
|
5 |
| - /** |
6 |
| - Applies the layout constraints |
7 |
| - Adding subviews included in layout |
| 6 | + public enum Either { |
| 7 | + case block(_LayoutBlockType) |
| 8 | + case container(LayoutContainer) |
| 9 | + } |
8 | 10 |
|
9 |
| - You might use ``LayoutContainer`` from describe beginning in order to support safe-area. |
10 |
| - */ |
11 |
| - @discardableResult |
12 |
| - public func buildSubviews<Block: _LayoutBlockType>( |
13 |
| - build: () -> Block |
14 |
| - ) -> LayoutBuilderContext { |
| 11 | + public static func buildBlock(_ components: [Either]...) -> [Either] { |
| 12 | + return components.flatMap { $0 } |
| 13 | + } |
15 | 14 |
|
16 |
| - let context = LayoutBuilderContext(targetView: base) |
17 |
| - let container = build() |
18 |
| - container.setupConstraints(parent: .init(view: base), in: context) |
19 |
| - context.prepareViewHierarchy() |
20 |
| - context.activate() |
| 15 | + public static func buildExpression<Block: _LayoutBlockType>(_ components: Block...) -> [Either] { |
| 16 | + return components.map { .block($0) } |
| 17 | + } |
21 | 18 |
|
22 |
| - return context |
| 19 | + public static func buildExpression(_ components: LayoutContainer...) -> [EntrypointBuilder.Either] { |
| 20 | + return components.map { .container($0) } |
23 | 21 | }
|
24 | 22 |
|
| 23 | +} |
| 24 | + |
| 25 | +extension MondrianNamespace where Base : UIView { |
| 26 | + |
25 | 27 | /**
|
26 |
| - Applies the layout constraints |
27 |
| - Adding subviews included in layout |
| 28 | + Builds subviews of this view. |
| 29 | + - activating layout-constraints |
| 30 | + - adding layout-guides |
| 31 | + - applying content-hugging, content-compression-resistance |
| 32 | + |
| 33 | + You can start to describe like followings: |
| 34 | + |
| 35 | + ```swift |
| 36 | + view.mondrian.buildSubviews { |
| 37 | + ZStackBlock { |
| 38 | + ... |
| 39 | + } |
| 40 | + } |
| 41 | + ``` |
| 42 | + |
| 43 | + ```swift |
| 44 | + view.mondrian.buildSubviews { |
| 45 | + LayoutContainer(attachedSafeAreaEdges: .vertical) { |
| 46 | + ... |
| 47 | + } |
| 48 | + } |
| 49 | + ``` |
| 50 | + |
| 51 | + |
| 52 | + ```swift |
| 53 | + view.mondrian.buildSubviews { |
| 54 | + LayoutContainer(attachedSafeAreaEdges: .vertical) { |
| 55 | + ... |
| 56 | + } |
| 57 | + ZStackBlock { |
| 58 | + ... |
| 59 | + } |
| 60 | + } |
| 61 | + ``` |
28 | 62 | */
|
29 | 63 | @discardableResult
|
30 |
| - public func buildSubviews( |
31 |
| - build: () -> LayoutContainer |
32 |
| - ) -> LayoutBuilderContext { |
| 64 | + public func buildSubviews(@EntrypointBuilder _ build: () -> [EntrypointBuilder.Either]) -> LayoutBuilderContext { |
| 65 | + |
| 66 | + let entrypoints = build() |
33 | 67 |
|
34 | 68 | let context = LayoutBuilderContext(targetView: base)
|
35 |
| - let container = build() |
36 |
| - container.setupConstraints(parent: base, in: context) |
| 69 | + |
| 70 | + for entrypoint in entrypoints { |
| 71 | + switch entrypoint { |
| 72 | + case .block(let block): |
| 73 | + block.setupConstraints(parent: .init(view: base), in: context) |
| 74 | + case .container(let container): |
| 75 | + container.setupConstraints(parent: base, in: context) |
| 76 | + } |
| 77 | + } |
| 78 | + |
37 | 79 | context.prepareViewHierarchy()
|
38 | 80 | context.activate()
|
39 | 81 |
|
|
0 commit comments