Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit 52c75e0

Browse files
author
Amir Khorsandi
committed
Complete generating codes and few options for ScaleTransformView
1 parent e661a67 commit 52c75e0

14 files changed

+452
-40
lines changed

Samples/PagingLayoutSamples.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
2993722324A79A9C0026D52F /* ShapeLayout+ScaleOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2993722224A79A9C0026D52F /* ShapeLayout+ScaleOptions.swift */; };
2222
29B5A72024A7B02900C9843E /* ShapeLayout+StackOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A71F24A7B02900C9843E /* ShapeLayout+StackOptions.swift */; };
2323
29B5A72224A7B06300C9843E /* ShapeLayout+SnapshotOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A72124A7B06300C9843E /* ShapeLayout+SnapshotOptions.swift */; };
24+
29B5A72424A8CC4B00C9843E /* CGFloat+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A72324A8CC4B00C9843E /* CGFloat+String.swift */; };
25+
29B5A72624A8D8B300C9843E /* OptionsCodeGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A72524A8D8B300C9843E /* OptionsCodeGenerator.swift */; };
26+
29B5A72824A8D8F700C9843E /* TransformCurve+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A72724A8D8F700C9843E /* TransformCurve+Name.swift */; };
27+
29B5A72A24A8D94300C9843E /* Values+Pair.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A72924A8D94300C9843E /* Values+Pair.swift */; };
28+
29B5A72C24A8DD7100C9843E /* UIBlurEffect.Style+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B5A72B24A8DD7100C9843E /* UIBlurEffect.Style+Name.swift */; };
2429
29BEC4D52476DD9D004BA505 /* LayoutDesignerOptionsTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BEC4D42476DD9D004BA505 /* LayoutDesignerOptionsTableView.swift */; };
2530
29D9F94323F7F98800656A67 /* ShapesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D9F94223F7F98800656A67 /* ShapesViewController.swift */; };
2631
29D9F94523F7F99400656A67 /* ShapesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D9F94423F7F99400656A67 /* ShapesViewModel.swift */; };
@@ -119,6 +124,11 @@
119124
2993722224A79A9C0026D52F /* ShapeLayout+ScaleOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ShapeLayout+ScaleOptions.swift"; sourceTree = "<group>"; };
120125
29B5A71F24A7B02900C9843E /* ShapeLayout+StackOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ShapeLayout+StackOptions.swift"; sourceTree = "<group>"; };
121126
29B5A72124A7B06300C9843E /* ShapeLayout+SnapshotOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ShapeLayout+SnapshotOptions.swift"; sourceTree = "<group>"; };
127+
29B5A72324A8CC4B00C9843E /* CGFloat+String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGFloat+String.swift"; sourceTree = "<group>"; };
128+
29B5A72524A8D8B300C9843E /* OptionsCodeGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptionsCodeGenerator.swift; sourceTree = "<group>"; };
129+
29B5A72724A8D8F700C9843E /* TransformCurve+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TransformCurve+Name.swift"; sourceTree = "<group>"; };
130+
29B5A72924A8D94300C9843E /* Values+Pair.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Values+Pair.swift"; sourceTree = "<group>"; };
131+
29B5A72B24A8DD7100C9843E /* UIBlurEffect.Style+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIBlurEffect.Style+Name.swift"; sourceTree = "<group>"; };
122132
29BEC4D42476DD9D004BA505 /* LayoutDesignerOptionsTableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutDesignerOptionsTableView.swift; sourceTree = "<group>"; };
123133
29D9F94223F7F98800656A67 /* ShapesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapesViewController.swift; sourceTree = "<group>"; };
124134
29D9F94423F7F99400656A67 /* ShapesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapesViewModel.swift; sourceTree = "<group>"; };
@@ -212,6 +222,7 @@
212222
292489AF2461A97900A316B0 /* LayoutDesignerViewController.swift */,
213223
294BA3C224A77A73008D0569 /* LayoutDesignerViewModel.swift */,
214224
292489B12461E31300A316B0 /* LayoutDesignerViewController.xib */,
225+
29B5A72524A8D8B300C9843E /* OptionsCodeGenerator.swift */,
215226
);
216227
path = LayoutDesigner;
217228
sourceTree = "<group>";
@@ -413,6 +424,10 @@
413424
2949CB262476EA8C000CC073 /* UITableView+Utilities.swift */,
414425
AB500A5423B152500056BE37 /* ViewModelBased.swift */,
415426
29D9F94F23F806C400656A67 /* Optional+Let.swift */,
427+
29B5A72324A8CC4B00C9843E /* CGFloat+String.swift */,
428+
29B5A72724A8D8F700C9843E /* TransformCurve+Name.swift */,
429+
29B5A72B24A8DD7100C9843E /* UIBlurEffect.Style+Name.swift */,
430+
29B5A72924A8D94300C9843E /* Values+Pair.swift */,
416431
);
417432
path = Utilities;
418433
sourceTree = "<group>";
@@ -684,10 +699,12 @@
684699
AB500A5023B151780056BE37 /* FruitsViewModel.swift in Sources */,
685700
2977657C2474531200835DBD /* LayoutDesignerOptionCell.swift in Sources */,
686701
2949CB272476EA8C000CC073 /* UITableView+Utilities.swift in Sources */,
702+
29B5A72A24A8D94300C9843E /* Values+Pair.swift in Sources */,
687703
AB500A4A23B13BBD0056BE37 /* FruitsViewController.swift in Sources */,
688704
ABC242C323B6822200DBD4D6 /* GalleryViewController.swift in Sources */,
689705
2993722324A79A9C0026D52F /* ShapeLayout+ScaleOptions.swift in Sources */,
690706
AB1BBAA023CA7BE6004E5C3B /* CardsViewModel.swift in Sources */,
707+
29B5A72824A8D8F700C9843E /* TransformCurve+Name.swift in Sources */,
691708
AB500A4E23B13C5D0056BE37 /* NibBased.swift in Sources */,
692709
ABC242C723B6823600DBD4D6 /* GalleryViewModel.swift in Sources */,
693710
AB1BBA9E23CA7BD9004E5C3B /* CardsViewController.swift in Sources */,
@@ -704,9 +721,12 @@
704721
29D9F95323F8685C00656A67 /* BaseShapeCollectionViewCell.swift in Sources */,
705722
ABA0DA0823F98B65004A9C18 /* ShapeCardView.swift in Sources */,
706723
ABC242CC23B6831400DBD4D6 /* Photo.swift in Sources */,
724+
29B5A72424A8CC4B00C9843E /* CGFloat+String.swift in Sources */,
707725
29FF296224A6321100C83DF9 /* LayoutDesignerCodePreviewViewController.swift in Sources */,
708726
29D9F94323F7F98800656A67 /* ShapesViewController.swift in Sources */,
709727
AB500A5823B154210056BE37 /* Fruit.swift in Sources */,
728+
29B5A72C24A8DD7100C9843E /* UIBlurEffect.Style+Name.swift in Sources */,
729+
29B5A72624A8D8B300C9843E /* OptionsCodeGenerator.swift in Sources */,
710730
29D9F94523F7F99400656A67 /* ShapesViewModel.swift in Sources */,
711731
29B5A72024A7B02900C9843E /* ShapeLayout+StackOptions.swift in Sources */,
712732
AB500A5B23B154640056BE37 /* FruitsCollectionViewCell.swift in Sources */,

Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewController.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased {
5656
default:
5757
viewModel.layouts = []
5858
}
59-
60-
previewViewController.viewModel = viewModel.getPreviewViewModel()
59+
previewViewController.viewModel = viewModel.shapesViewModel
6160

6261
setLayoutButtonSelected(view: stackButtonView, isSelected: view == stackButtonView)
6362
setLayoutButtonSelected(view: scaleButtonView, isSelected: view == scaleButtonView)
@@ -107,13 +106,16 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased {
107106
}
108107

109108
private func addPreviewController() {
110-
previewViewController = ShapesViewController.instantiate(viewModel: ShapesViewModel(layouts: .stack, showBackButton: false))
109+
previewViewController = ShapesViewController.instantiate(viewModel: viewModel.shapesViewModel)
111110
addChild(previewViewController)
112111
previewViewController.view.layer.cornerRadius = 30
113112
previewViewController.view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
114113
previewContainerView.fill(with: previewViewController.view)
115114
previewViewController.didMove(toParent: self)
116115
previewViewController.delegate = self
116+
viewModel.onOptionsChange = { [weak self] in
117+
self?.previewViewController.reloadAndInvalidateShapes()
118+
}
117119
}
118120

119121
private func addCodePreviewController() {
@@ -127,7 +129,7 @@ class LayoutDesignerViewController: UIViewController, ViewModelBased, NibBased {
127129
}
128130

129131
private func setOptionsList() {
130-
optionsTableView.optionViewModels = viewModel.getOptionViewModels()
132+
optionsTableView.optionViewModels = viewModel.optionViewModels
131133
}
132134

133135
}

Samples/PagingLayoutSamples/Modules/LayoutDesigner/LayoutDesignerViewModel.swift

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,91 @@
77
//
88

99
import Foundation
10+
import CollectionViewPagingLayout
1011

1112
class LayoutDesignerViewModel {
1213

1314
// MARK: Properties
1415

1516
var onCodePreviewViewModelChange: ((LayoutDesignerCodePreviewViewModel) -> Void)?
16-
var selectedLayout: ShapeLayout?
17-
var layouts: [ShapeLayout] = []
17+
var onOptionsChange: (() -> Void)?
18+
var selectedLayout: ShapeLayout? {
19+
didSet {
20+
refreshOptionViewModels()
21+
}
22+
}
23+
var layouts: [ShapeLayout] = .stack
24+
var shapesViewModel: ShapesViewModel {
25+
ShapesViewModel(layouts: layouts, showBackButton: false)
26+
}
27+
private(set) var optionViewModels: [LayoutDesignerOptionCellViewModel] = []
28+
private let codeGenerator = OptionsCodeGenerator()
1829

1930

2031
// MARK: Public functions
2132

22-
func getPreviewViewModel() -> ShapesViewModel {
23-
ShapesViewModel(layouts: layouts, showBackButton: false)
33+
private func updateCodePreview<T>(options: T) {
34+
onCodePreviewViewModelChange?(.init(code: codeGenerator.generateCode(options: options)))
35+
}
36+
private func update<T>(options: inout T, closure: (inout T) -> Void) {
37+
closure(&options)
38+
updateCodePreview(options: options)
39+
shapesViewModel.setCustomOptions(options)
40+
onOptionsChange?()
2441
}
2542

26-
func getOptionViewModels() -> [LayoutDesignerOptionCellViewModel] {
27-
[
28-
.init(title: "Min scale", kind: .singleSlider(current: 0.3, onChange: { _ in })),
29-
.init(title: "Scale ratio", kind: .singleSlider(current: 0.7, onChange: { _ in })),
30-
.init(title: "Translate ratio", kind: .doubleSlider(current: (0.2, 0.7), onChange: { _, _ in })),
31-
.init(title: "Keep vertical spacing equal", kind: .toggleSwitch(current: true, onChange: { _ in })),
32-
.init(title: "Keep horizontal spacing equal", kind: .toggleSwitch(current: false, onChange: { _ in })),
33-
.init(title: "Scale curve", kind: .segmented(options: ["Linear", "EaseIn", "EeaseOut"], current: "Linear", onChange: { _ in }))
34-
]
43+
private func refreshOptionViewModels() {
44+
guard let selectedLayout = selectedLayout else {
45+
optionViewModels = []
46+
return
47+
}
48+
49+
if var options = selectedLayout.scaleOptions {
50+
updateCodePreview(options: options)
51+
let update: ((inout ScaleTransformViewOptions) -> Void) -> Void = { [weak self] in
52+
self?.update(options: &options, closure: $0)
53+
}
54+
55+
optionViewModels = [
56+
.init(title: "Min scale", kind: .singleSlider(current: options.minScale) { n in
57+
update { $0.minScale = n }
58+
}),
59+
.init(title: "Max scale", kind: .singleSlider(current: options.maxScale) { n in
60+
update { $0.maxScale = n }
61+
}),
62+
.init(title: "Scale ratio", kind: .singleSlider(current: options.scaleRatio) { n in
63+
update { $0.scaleRatio = n }
64+
}),
65+
.init(title: "Translation ratio", kind: .doubleSlider(current: options.translationRatio.pair) { n in
66+
update { $0.translationRatio = .by(pair: n) }
67+
}),
68+
.init(title: "Min translation ratio", kind: .doubleSlider(current: options.minTranslationRatio?.pair) { n in
69+
update { $0.minTranslationRatio = .by(pair: n) }
70+
}),
71+
.init(title: "Max translation ratio", kind: .doubleSlider(current: options.maxTranslationRatio?.pair) { n in
72+
update { $0.maxTranslationRatio = .by(pair: n) }
73+
}),
74+
.init(title: "Keep vertical spacing equal", kind: .toggleSwitch(current: options.keepVerticalSpacingEqual) { n in
75+
update { $0.keepVerticalSpacingEqual = n }
76+
}),
77+
.init(title: "Keep horizontal spacing equal", kind: .toggleSwitch(current: options.keepHorizontalSpacingEqual) { n in
78+
update { $0.keepHorizontalSpacingEqual = n }
79+
}),
80+
.init(title: "Scale curve", kind: .segmented(options: TransformCurve.all.map(\.name), current: options.scaleCurve.name) { n in
81+
update { $0.scaleCurve = .by(name: n)! }
82+
})
83+
]
84+
} else if var options = selectedLayout.stackOptions {
85+
updateCodePreview(options: options)
86+
let update: ((inout StackTransformViewOptions) -> Void) -> Void = { [weak self] in
87+
self?.update(options: &options, closure: $0)
88+
}
89+
90+
} else if var options = selectedLayout.snapshotOptions {
91+
updateCodePreview(options: options)
92+
let update: ((inout SnapshotTransformViewOptions) -> Void) -> Void = { [weak self] in
93+
self?.update(options: &options, closure: $0)
94+
}
95+
}
3596
}
3697
}

Samples/PagingLayoutSamples/Modules/LayoutDesigner/Options/LayoutDesignerOptionsTableView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ extension LayoutDesignerOptionsTableView: UITableViewDataSource {
5757
extension LayoutDesignerOptionsTableView: UITableViewDelegate {
5858

5959
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
60-
54
60+
64
6161
}
6262
}

Samples/PagingLayoutSamples/Modules/LayoutDesigner/Options/cell/LayoutDesignerOptionCell.swift

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,16 @@ class LayoutDesignerOptionCell: UITableViewCell, NibBased {
145145
slider1.isHidden = false
146146
input1.isHidden = false
147147
slider2.superview?.isHidden = false
148-
input1.set(value: current.0)
149-
slider1.value = Float(current.0)
148+
input1.set(value: current?.0 ?? 0)
149+
slider1.value = Float(current?.0 ?? 0)
150150
slider2.isHidden = false
151151
input2.isHidden = false
152-
input2.set(value: current.1)
153-
slider2.value = Float(current.1)
154-
onSlider1Change = { [weak self] in onChange($0, CGFloat(self?.slider2.value ?? 0)) }
155-
onSlider2Change = { [weak self] in onChange(CGFloat(self?.slider1.value ?? 0), $0) }
156-
onInput1Change = { [weak self] in onChange($0, CGFloat(self?.input2.floatValue ?? 0)) }
157-
onInput2Change = { [weak self] in onChange(CGFloat(self?.input1.floatValue ?? 0), $0) }
152+
input2.set(value: current?.1 ?? 0)
153+
slider2.value = Float(current?.1 ?? 0)
154+
onSlider1Change = { [weak self] in onChange(($0, CGFloat(self?.slider2.value ?? 0))) }
155+
onSlider2Change = { [weak self] in onChange((CGFloat(self?.slider1.value ?? 0), $0)) }
156+
onInput1Change = { [weak self] in onChange(($0, CGFloat(self?.input2.floatValue ?? 0))) }
157+
onInput2Change = { [weak self] in onChange((CGFloat(self?.input1.floatValue ?? 0), $0)) }
158158

159159
case let .toggleSwitch(current, onChange):
160160
switchView.isHidden = false
@@ -180,21 +180,14 @@ class LayoutDesignerOptionCell: UITableViewCell, NibBased {
180180
private extension UITextField {
181181

182182
var floatValue: Float {
183-
let formatter = NumberFormatter()
184-
formatter.numberStyle = .decimal
185-
formatter.allowsFloats = true
186-
formatter.maximumFractionDigits = 2
187-
formatter.locale = Locale(identifier: "en_US")
188-
let text = self.text?.replacingOccurrences(of: ",", with: ".") ?? "0"
189-
let number = formatter.number(from: text)
190-
return number?.floatValue ?? 0
183+
text?.floatValue ?? 0
191184
}
192185

193186
func set(value: Float) {
194187
set(value: CGFloat(value))
195188
}
196189

197190
func set(value: CGFloat) {
198-
text = String(format: "%.2f", value)
191+
text = value.format()
199192
}
200193
}

Samples/PagingLayoutSamples/Modules/LayoutDesigner/Options/cell/LayoutDesignerOptionCell.xib

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@
7171
<constraint firstItem="mrr-fb-BXW" firstAttribute="width" secondItem="nsZ-12-SEz" secondAttribute="width" priority="900" id="9yO-80-BMd"/>
7272
</constraints>
7373
</stackView>
74+
<view alpha="0.14999999999999999" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="pXN-ee-GI3">
75+
<rect key="frame" x="23" y="82" width="732" height="1"/>
76+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
77+
<constraints>
78+
<constraint firstAttribute="height" constant="1" id="mMG-5S-2qm"/>
79+
</constraints>
80+
</view>
7481
</subviews>
7582
<constraints>
83+
<constraint firstItem="pXN-ee-GI3" firstAttribute="leading" secondItem="PxB-JM-JuU" secondAttribute="leading" constant="23" id="1D0-mK-5QZ"/>
84+
<constraint firstAttribute="bottom" secondItem="pXN-ee-GI3" secondAttribute="bottom" id="3HJ-sb-NIN"/>
7685
<constraint firstItem="nsZ-12-SEz" firstAttribute="leading" secondItem="PxB-JM-JuU" secondAttribute="leading" constant="24" id="Afy-kk-jYN"/>
7786
<constraint firstAttribute="trailing" secondItem="nsZ-12-SEz" secondAttribute="trailing" constant="24" id="UeI-uw-LMX"/>
7887
<constraint firstItem="nsZ-12-SEz" firstAttribute="top" secondItem="PxB-JM-JuU" secondAttribute="top" id="XM1-o8-tUx"/>
88+
<constraint firstItem="pXN-ee-GI3" firstAttribute="width" secondItem="PxB-JM-JuU" secondAttribute="width" constant="-46" id="okV-BN-xAI"/>
7989
<constraint firstAttribute="bottom" secondItem="nsZ-12-SEz" secondAttribute="bottom" id="pTk-36-SVa"/>
8090
</constraints>
8191
</tableViewCellContentView>

Samples/PagingLayoutSamples/Modules/LayoutDesigner/Options/cell/LayoutDesignerOptionCellViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class LayoutDesignerOptionCellViewModel {
1515

1616
enum Kind {
1717
case singleSlider(current: CGFloat, onChange: (CGFloat) -> Void)
18-
case doubleSlider(current: (CGFloat, CGFloat), onChange: (CGFloat, CGFloat) -> Void)
18+
case doubleSlider(current: (CGFloat, CGFloat)?, onChange: ((CGFloat, CGFloat)) -> Void)
1919
case toggleSwitch(current: Bool, onChange: (Bool) -> Void)
2020
case segmented(options: [String], current: String?, onChange: (String) -> Void)
2121
}

0 commit comments

Comments
 (0)