forked from amirdew/CollectionViewPagingLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutDesignerViewModel.swift
More file actions
364 lines (337 loc) · 19 KB
/
LayoutDesignerViewModel.swift
File metadata and controls
364 lines (337 loc) · 19 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// LayoutDesignerViewModel.swift
// PagingLayoutSamples
//
// Created by Amir on 27/06/2020.
// Copyright © 2020 Amir Khorsandi. All rights reserved.
//
import Foundation
import CollectionViewPagingLayout
class LayoutDesignerViewModel {
// MARK: Properties
var onCodePreviewViewModelChange: ((LayoutDesignerCodePreviewViewModel) -> Void)?
var onOptionsChange: (() -> Void)?
var selectedLayout: ShapeLayout? {
didSet {
refreshOptionViewModels()
}
}
var layouts: [ShapeLayout] = .scale
var shapesViewModel: ShapesViewModel {
ShapesViewModel(layouts: layouts, showBackButton: false, showPageControl: true)
}
var shouldShowIntro: Bool {
if let introShown = UserDefaults.standard.value(forKey: "IntroShown") as? Bool, introShown {
return false
}
UserDefaults.standard.setValue(true, forKey: "IntroShown")
return true
}
private(set) var optionViewModels: [LayoutDesignerOptionSectionViewModel] = []
private let codeGenerator = OptionsCodeGenerator()
// MARK: Public functions
func getIntroViewModel(showWelcome: Bool = true) -> LayoutDesignerIntroViewModel {
LayoutDesignerIntroViewModel(introPages: showWelcome ? .all : .allExceptWelcome)
}
// MARK: Private functions
private func updateCodePreview<T>(options: T) {
onCodePreviewViewModelChange?(.init(code: codeGenerator.generateCode(options: options)))
}
private func update<T>(options: inout T, closure: (inout T) -> Void) {
closure(&options)
updateCodePreview(options: options)
shapesViewModel.setCustomOptions(options)
onOptionsChange?()
}
private func refreshOptionViewModels() {
guard let selectedLayout = selectedLayout else {
optionViewModels = []
return
}
if let options = selectedLayout.scaleOptions {
optionViewModels = getOptionViewModels(scaleOptions: options)
} else if let options = selectedLayout.stackOptions {
optionViewModels = getOptionViewModels(stackOptions: options)
} else if let options = selectedLayout.snapshotOptions {
optionViewModels = getOptionViewModels(snapshotOptions: options)
}
}
private func getOptionViewModels(scaleOptions: ScaleTransformViewOptions) -> [LayoutDesignerOptionSectionViewModel] {
var options = scaleOptions
updateCodePreview(options: options)
let update: ((inout ScaleTransformViewOptions) -> Void) -> Void = { [weak self] in
self?.update(options: &options, closure: $0)
}
let generalOptions: [LayoutDesignerOptionViewModel] = [
.init(title: "Min scale", kind: .singleSlider(current: options.minScale, range: 0...2) { n in
update { $0.minScale = n! }
}),
.init(title: "Max scale", kind: .singleSlider(current: options.maxScale, range: 0...2) { n in
update { $0.maxScale = n! }
}),
.init(title: "Scale ratio", kind: .singleSlider(current: options.scaleRatio, range: 0...2) { n in
update { $0.scaleRatio = n! }
}),
.init(title: "Translation ratio", kind: .doubleSlider(current: options.translationRatio.pair, range: -2...2) { n in
update { $0.translationRatio = .by(pair: n!) }
}),
.init(title: "Min translation ratio", kind: .doubleSlider(current: options.minTranslationRatio?.pair, range: -5...5, optional: true) { n in
update { $0.minTranslationRatio = n.map { .by(pair: $0) } }
}),
.init(title: "Max translation ratio", kind: .doubleSlider(current: options.maxTranslationRatio?.pair, range: -5...5, optional: true) { n in
update { $0.maxTranslationRatio = n.map { .by(pair: $0) } }
}),
.init(title: "Keep vertical spacing equal", kind: .toggleSwitch(current: options.keepVerticalSpacingEqual) { n in
update { $0.keepVerticalSpacingEqual = n }
}),
.init(title: "Keep horizontal spacing equal", kind: .toggleSwitch(current: options.keepHorizontalSpacingEqual) { n in
update { $0.keepHorizontalSpacingEqual = n }
}),
.init(title: "Scale curve", kind: .segmented(options: TransformCurve.all.map(\.name), current: options.scaleCurve.name) { n in
update { $0.scaleCurve = .by(name: n)! }
}),
.init(title: "Translation curve", kind: .segmented(options: TransformCurve.all.map(\.name), current: options.translationCurve.name) { n in
update { $0.translationCurve = .by(name: n)! }
}),
.init(title: "Shadow enabled", kind: .toggleSwitch(current: options.shadowEnabled) { n in
update { $0.shadowEnabled = n }
}),
.init(title: "Shadow opacity", kind: .singleSlider(current: CGFloat(options.shadowOpacity)) { n in
update { $0.shadowOpacity = Float(n!) }
}),
.init(title: "Shadow opacity min", kind: .singleSlider(current: CGFloat(options.shadowOpacityMin)) { n in
update { $0.shadowOpacityMin = Float(n!) }
}),
.init(title: "Shadow opacity max", kind: .singleSlider(current: CGFloat(options.shadowOpacityMax)) { n in
update { $0.shadowOpacityMax = Float(n!) }
}),
.init(title: "Shadow radius max", kind: .singleSlider(current: options.shadowRadiusMax, range: 0...15) { n in
update { $0.shadowRadiusMax = n! }
}),
.init(title: "Shadow radius min", kind: .singleSlider(current: options.shadowRadiusMin, range: 0...15) { n in
update { $0.shadowRadiusMin = n! }
}),
.init(title: "Shadow offset min", kind: .doubleSlider(current: options.shadowOffsetMin.pair, range: -7...7) { n in
update { $0.shadowOffsetMin = .by(pair: n!) }
}),
.init(title: "Shadow offset max", kind: .doubleSlider(current: options.shadowOffsetMax.pair, range: -7...7) { n in
update { $0.shadowOffsetMax = .by(pair: n!) }
}),
.init(title: "Blur effect enabled", kind: .toggleSwitch(current: options.blurEffectEnabled) { n in
update { $0.blurEffectEnabled = n }
}),
.init(title: "Blur effect radius ratio", kind: .singleSlider(current: options.blurEffectRadiusRatio) { n in
update { $0.blurEffectRadiusRatio = n! }
}),
.init(title: "Blur effect style", kind: .segmented(options: UIBlurEffect.Style.all.map(\.name), current: options.blurEffectStyle.name) { n in
update { $0.blurEffectStyle = .by(name: n)! }
})
]
let originalRotation3dOptions = options.rotation3d ?? ScaleTransformViewOptions.Rotation3dOptions(
angle: .pi / 3,
minAngle: 0,
maxAngle: .pi,
x: 0,
y: 1,
z: 0,
m34: -0.000_1
)
let rotation3dOptions: [LayoutDesignerOptionViewModel] = [
.init(title: "Enabled", kind: .toggleSwitch(current: options.rotation3d != nil) { n in
update { $0.rotation3d = !n ? nil : originalRotation3dOptions }
}),
.init(title: "Angle", kind: .singleSlider(current: options.rotation3d?.angle, range: -.pi...CGFloat.pi) { n in
update { $0.rotation3d?.angle = n! }
}),
.init(title: "Min angle", kind: .singleSlider(current: options.rotation3d?.minAngle, range: -.pi...CGFloat.pi) { n in
update { $0.rotation3d?.minAngle = n! }
}),
.init(title: "Max angle", kind: .singleSlider(current: options.rotation3d?.maxAngle, range: -.pi...CGFloat.pi) { n in
update { $0.rotation3d?.maxAngle = n! }
}),
.init(title: "X", kind: .singleSlider(current: options.rotation3d?.x, range: -1...1) { n in
update { $0.rotation3d?.x = n! }
}),
.init(title: "Y", kind: .singleSlider(current: options.rotation3d?.y, range: -1...1) { n in
update { $0.rotation3d?.y = n! }
}),
.init(title: "Z", kind: .singleSlider(current: options.rotation3d?.z, range: -1...1) { n in
update { $0.rotation3d?.z = n! }
}),
.init(title: "m34", kind: .singleSlider(current: options.rotation3d.map { $0.m34 * 1_000 }, range: -2...2) { n in
update { $0.rotation3d?.m34 = n! / 1_000 }
})
]
let originalTranslation3dOptions = options.translation3d ?? ScaleTransformViewOptions.Translation3dOptions(
translateRatios: (0.1, 0, 0),
minTranslateRatios: (-0.05, 0, 0.86),
maxTranslateRatios: (0.05, 0, -0.86)
)
let translation3dOptions: [LayoutDesignerOptionViewModel] = [
.init(title: "Enabled", kind: .toggleSwitch(current: options.translation3d != nil) { n in
update { $0.translation3d = !n ? nil : originalTranslation3dOptions }
}),
.init(title: "X ratio", kind: .singleSlider(current: options.translation3d?.translateRatios.0, range: -5...5) { n in
update {
guard let current = $0.translation3d?.translateRatios else { return }
$0.translation3d?.translateRatios = (n!, current.1, current.2)
}
}),
.init(title: "X min ratio", kind: .singleSlider(current: options.translation3d?.minTranslateRatios.0, range: -10...10) { n in
update {
guard let current = $0.translation3d?.minTranslateRatios else { return }
$0.translation3d?.minTranslateRatios = (n!, current.1, current.2)
}
}),
.init(title: "X max ratio", kind: .singleSlider(current: options.translation3d?.maxTranslateRatios.0, range: -10...10) { n in
update {
guard let current = $0.translation3d?.maxTranslateRatios else { return }
$0.translation3d?.maxTranslateRatios = (n!, current.1, current.2)
}
}),
.init(title: "Y ratio", kind: .singleSlider(current: options.translation3d?.translateRatios.1, range: -5...5) { n in
update {
guard let current = $0.translation3d?.translateRatios else { return }
$0.translation3d?.translateRatios = (current.0, n!, current.2)
}
}),
.init(title: "Y min ratio", kind: .singleSlider(current: options.translation3d?.minTranslateRatios.1, range: -10...10) { n in
update {
guard let current = $0.translation3d?.minTranslateRatios else { return }
$0.translation3d?.minTranslateRatios = (current.0, n!, current.2)
}
}),
.init(title: "Y max ratio", kind: .singleSlider(current: options.translation3d?.maxTranslateRatios.1, range: -10...10) { n in
update {
guard let current = $0.translation3d?.maxTranslateRatios else { return }
$0.translation3d?.maxTranslateRatios = (current.0, n!, current.2)
}
}),
.init(title: "Z ratio", kind: .singleSlider(current: options.translation3d?.translateRatios.2, range: -5...5) { n in
update {
guard let current = $0.translation3d?.translateRatios else { return }
$0.translation3d?.translateRatios = (current.0, current.1, n!)
}
}),
.init(title: "Z min ratio", kind: .singleSlider(current: options.translation3d?.minTranslateRatios.2, range: -10...10) { n in
update {
guard let current = $0.translation3d?.minTranslateRatios else { return }
$0.translation3d?.minTranslateRatios = (current.0, current.1, n!)
}
}),
.init(title: "Z max ratio", kind: .singleSlider(current: options.translation3d?.maxTranslateRatios.2, range: -10...10) { n in
update {
guard let current = $0.translation3d?.maxTranslateRatios else { return }
$0.translation3d?.maxTranslateRatios = (current.0, current.1, n!)
}
})
]
return [
.init(title: "Options", items: generalOptions),
.init(title: "Rotation 3D options", items: rotation3dOptions),
.init(title: "Translation 3D options", items: translation3dOptions)
]
}
private func getOptionViewModels(stackOptions: StackTransformViewOptions) -> [LayoutDesignerOptionSectionViewModel] {
var options = stackOptions
updateCodePreview(options: options)
let update: ((inout StackTransformViewOptions) -> Void) -> Void = { [weak self] in
self?.update(options: &options, closure: $0)
}
let viewModels: [LayoutDesignerOptionViewModel] = [
.init(title: "Scale factor", kind: .singleSlider(current: options.scaleFactor, range: -1...1) { n in
update { $0.scaleFactor = n! }
}),
.init(title: "Min scale", kind: .singleSlider(current: options.minScale, optional: true) { n in
update { $0.minScale = n }
}),
.init(title: "Max scale", kind: .singleSlider(current: options.maxScale, optional: true) { n in
update { $0.maxScale = n }
}),
.init(title: "Spacing factor", kind: .singleSlider(current: options.spacingFactor, range: 0...0.5) { n in
update { $0.spacingFactor = n! }
}),
.init(title: "Max spacing", kind: .singleSlider(current: options.maxSpacing, optional: true) { n in
update { $0.maxSpacing = n }
}),
.init(title: "Alpha factor", kind: .singleSlider(current: options.alphaFactor) { n in
update { $0.alphaFactor = n! }
}),
.init(title: "Bottom stack alpha speed factor", kind: .singleSlider(current: options.bottomStackAlphaSpeedFactor, range: 0...10) { n in
update { $0.bottomStackAlphaSpeedFactor = n! }
}),
.init(title: "Top stack alpha speed factor", kind: .singleSlider(current: options.topStackAlphaSpeedFactor, range: 0...10) { n in
update { $0.topStackAlphaSpeedFactor = n! }
}),
.init(title: "Perspective ratio", kind: .singleSlider(current: options.perspectiveRatio, range: -1...1) { n in
update { $0.perspectiveRatio = n! }
}),
.init(title: "Shadow enabled", kind: .toggleSwitch(current: options.shadowEnabled) { n in
update { $0.shadowEnabled = n }
}),
.init(title: "Shadow opacity", kind: .singleSlider(current: CGFloat(options.shadowOpacity)) { n in
update { $0.shadowOpacity = Float(n!) }
}),
.init(title: "Shadow offset", kind: .doubleSlider(current: options.shadowOffset.pair) { n in
update { $0.shadowOffset = .by(pair: n!) }
}),
.init(title: "Shadow radius", kind: .singleSlider(current: options.shadowRadius, range: 1...10) { n in
update { $0.shadowRadius = n! }
}),
.init(title: "Rotate angel", kind: .singleSlider(current: options.stackRotateAngel, range: -CGFloat.pi...CGFloat.pi) { n in
update { $0.stackRotateAngel = n! }
}),
.init(title: "Pop angle", kind: .singleSlider(current: options.popAngle, range: -CGFloat.pi...CGFloat.pi) { n in
update { $0.popAngle = n! }
}),
.init(title: "Pop offset ratio", kind: .doubleSlider(current: options.popOffsetRatio.pair, range: -2...2) { n in
update { $0.popOffsetRatio = .by(pair: n!) }
}),
.init(title: "Stack position", kind: .doubleSlider(current: options.stackPosition.pair, range: -1...1) { n in
update { $0.stackPosition = .by(pair: n!) }
}),
.init(title: "Reverse", kind: .toggleSwitch(current: options.reverse) { n in
update { $0.reverse = n }
}),
.init(title: "Blur effect enabled", kind: .toggleSwitch(current: options.blurEffectEnabled) { n in
update { $0.blurEffectEnabled = n }
}),
.init(title: "Max blur radius", kind: .singleSlider(current: options.maxBlurEffectRadius) { n in
update { $0.maxBlurEffectRadius = n! }
}),
.init(title: "Blur effect style", kind: .segmented(options: UIBlurEffect.Style.all.map(\.name), current: options.blurEffectStyle.name) { n in
update { $0.blurEffectStyle = .by(name: n)! }
})
]
return [
.init(title: "Options", items: viewModels)
]
}
private func getOptionViewModels(snapshotOptions: SnapshotTransformViewOptions) -> [LayoutDesignerOptionSectionViewModel] {
var options = snapshotOptions
updateCodePreview(options: options)
let update: ((inout SnapshotTransformViewOptions) -> Void) -> Void = { [weak self] in
self?.update(options: &options, closure: $0)
}
let viewModels: [LayoutDesignerOptionViewModel] = [
.init(title: "Piece size ratio", kind: .doubleSlider(current: options.pieceSizeRatio.pair, range: 0.01...1) { n in
update { $0.pieceSizeRatio = .by(pair: n!) }
}),
.init(title: "Container scale ratio", kind: .singleSlider(current: options.containerScaleRatio) { n in
update { $0.containerScaleRatio = n! }
}),
.init(title: "Container translation ratio", kind: .doubleSlider(current: options.containerTranslationRatio.pair, range: 0...2) { n in
update { $0.containerTranslationRatio = .by(pair: n!) }
}),
.init(title: "Container min translation ratio", kind: .doubleSlider(current: options.containerMinTranslationRatio?.pair, range: 0...2, optional: true) { n in
update { $0.containerMinTranslationRatio = n.map { .by(pair: $0) } }
}),
.init(title: "Container max translation ratio", kind: .doubleSlider(current: options.containerMaxTranslationRatio?.pair, range: 0...2, optional: true) { n in
update { $0.containerMaxTranslationRatio = n.map { .by(pair: $0) } }
})
]
return [
.init(title: "Options", items: viewModels)
]
}
}