-
Notifications
You must be signed in to change notification settings - Fork 2
/
Component.swift
189 lines (130 loc) · 5.63 KB
/
Component.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Component.swift
// PortalView
//
// Created by Guido Marucci Blas on 1/27/17.
//
//
import Foundation
public enum RootComponent<MessageType> {
case simple
case stack(NavigationBar<MessageType>)
case tab(TabBar<MessageType>)
}
public enum SupportedOrientations {
case portrait
case landscape
case all
}
public enum Gesture<MessageType> {
case tap(message: MessageType)
}
public extension Gesture {
public func map<NewMessageType>(_ transform: @escaping (MessageType) -> NewMessageType) -> Gesture<NewMessageType> {
switch self {
case .tap(let message):
return .tap(message: transform(message))
}
}
}
public indirect enum Component<MessageType> {
case button(ButtonProperties<MessageType>, StyleSheet<ButtonStyleSheet>, Layout)
case label(LabelProperties, StyleSheet<LabelStyleSheet>, Layout)
case mapView(MapProperties, StyleSheet<EmptyStyleSheet>, Layout)
case imageView(Image, StyleSheet<EmptyStyleSheet>, Layout)
case container([Component<MessageType>], StyleSheet<EmptyStyleSheet>, Layout)
case table(TableProperties<MessageType>, StyleSheet<TableStyleSheet>, Layout)
case collection(CollectionProperties<MessageType>, StyleSheet<EmptyStyleSheet>, Layout)
case carousel(CarouselProperties<MessageType>, StyleSheet<EmptyStyleSheet>, Layout)
case touchable(gesture: Gesture<MessageType>, child: Component<MessageType>)
case segmented(ZipList<SegmentProperties<MessageType>>, StyleSheet<SegmentedStyleSheet>, Layout)
case progress(ProgressCounter, StyleSheet<ProgressStyleSheet>, Layout)
case textField(TextFieldProperties<MessageType>, StyleSheet<TextFieldStyleSheet>, Layout)
case custom(componentIdentifier: String, layout: Layout)
case spinner(Bool, StyleSheet<SpinnerStyleSheet>, Layout)
public var layout: Layout {
switch self {
case .button(_, _, let layout):
return layout
case .label(_, _, let layout):
return layout
case .textField(_, _, let layout):
return layout
case .mapView(_, _, let layout):
return layout
case .imageView(_, _, let layout):
return layout
case .container(_, _, let layout):
return layout
case .table(_, _, let layout):
return layout
case .touchable(_, let child):
return child.layout
case .segmented(_, _, let layout):
return layout
case .progress(_, _, let layout):
return layout
case .collection(_, _, let layout):
return layout
case .carousel(_, _, let layout):
return layout
case .custom(_, let layout):
return layout
case .spinner(_, _, let layout):
return layout
}
}
}
extension Component {
public func map<NewMessageType>(_ transform: @escaping (MessageType) -> NewMessageType) -> Component<NewMessageType> {
switch self {
case .button(let properties, let style, let layout):
return .button(properties.map(transform), style, layout)
case .label(let properties, let style, let layout):
return .label(properties, style, layout)
case .textField(let properties, let style, let layout):
return .textField(properties.map(transform), style, layout)
case .mapView(let properties, let style, let layout):
return .mapView(properties, style, layout)
case .imageView(let image, let style, let layout):
return .imageView(image, style, layout)
case .container(let children, let style, let layout):
return .container(children.map { $0.map(transform) }, style, layout)
case .table(let properties, let style, let layout):
return .table(properties.map(transform), style, layout)
case .touchable(let gesture, let child):
return .touchable(gesture: gesture.map(transform), child: child.map(transform))
case .segmented(let segments, let style, let layout):
return .segmented(segments.map { $0.map(transform) }, style, layout)
case .progress(let progress, let style, let layout):
return .progress(progress, style, layout)
case .collection(let properties, let style, let layout):
return .collection(properties.map(transform), style, layout)
case .carousel(let properties, let style, let layout):
return .carousel(properties.map(transform), style, layout)
case .custom(let componentIdentifier, let layout):
return .custom(componentIdentifier: componentIdentifier, layout: layout)
case .spinner(let isActive, let style, let layout):
return .spinner(isActive, style, layout)
}
}
public var customComponentIdentifiers: [String] {
switch self {
case .container(let children, _, _):
return children.flatMap { $0.customComponentIdentifiers }
case .custom(let componentIdentifier, _):
return [componentIdentifier]
default:
return []
}
}
}
public func container<MessageType>(
children: [Component<MessageType>] = [],
style: StyleSheet<EmptyStyleSheet> = EmptyStyleSheet.`default`,
layout: Layout = layout()) -> Component<MessageType> {
return .container(children, style, layout)
}
public func touchable<MessageType>(gesture: Gesture<MessageType>, child: Component<MessageType>) -> Component<MessageType> {
return .touchable(gesture: gesture, child: child)
}