Skip to content

Commit 45998ef

Browse files
Merge pull request #31 from componentskit/refactor
Improve existing code
2 parents beae296 + 2fa9496 commit 45998ef

File tree

19 files changed

+121
-113
lines changed

19 files changed

+121
-113
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/Helpers/UKComponentPreview.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ struct UKComponentPreview<View, Model>: UIViewRepresentable where View: UKCompon
3838
let model: Model
3939
let view: View
4040

41-
init(model: Model, view: @escaping () -> View) {
42-
self.view = view()
43-
self.model = model
41+
init(view: View) {
42+
self.view = view
43+
self.model = view.model
4444
}
4545

4646
func makeUIView(context: Context) -> Container {
@@ -51,3 +51,9 @@ struct UKComponentPreview<View, Model>: UIViewRepresentable where View: UKCompon
5151
container.component.model = self.model
5252
}
5353
}
54+
55+
extension UKComponent {
56+
var preview: some View {
57+
UKComponentPreview(view: self)
58+
}
59+
}

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/ButtonPreview.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ struct ButtonPreview: View {
1010
var body: some View {
1111
VStack {
1212
PreviewWrapper(title: "UIKit") {
13-
UKComponentPreview(model: self.model) {
14-
UKButton(model: self.model)
15-
}
13+
UKButton(model: self.model)
14+
.preview
1615
}
1716
PreviewWrapper(title: "SwiftUI") {
1817
SUButton(model: self.model)
@@ -28,12 +27,12 @@ struct ButtonPreview: View {
2827
Toggle("Full Width", isOn: self.$model.isFullWidth)
2928
SizePicker(selection: self.$model.size)
3029
Picker("Style", selection: self.$model.style) {
31-
Text("Filled").tag(ButtonStyle.filled)
32-
Text("Plain").tag(ButtonStyle.plain)
33-
Text("Bordered with small border").tag(ButtonStyle.bordered(.small))
34-
Text("Bordered with medium border").tag(ButtonStyle.bordered(.medium))
35-
Text("Bordered with large border").tag(ButtonStyle.bordered(.large))
36-
Text("Bordered with custom border: 6px").tag(ButtonStyle.bordered(.custom(6)))
30+
Text("Filled").tag(ButtonVM.Style.filled)
31+
Text("Plain").tag(ButtonVM.Style.plain)
32+
Text("Bordered with small border").tag(ButtonVM.Style.bordered(.small))
33+
Text("Bordered with medium border").tag(ButtonVM.Style.bordered(.medium))
34+
Text("Bordered with large border").tag(ButtonVM.Style.bordered(.large))
35+
Text("Bordered with custom border: 6px").tag(ButtonVM.Style.bordered(.custom(6)))
3736
}
3837
}
3938
}

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/CheckboxPreview.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ struct CheckboxPreview: View {
1212
var body: some View {
1313
VStack {
1414
PreviewWrapper(title: "UIKit") {
15-
UKComponentPreview(model: self.model) {
16-
UKCheckbox(
17-
initialValue: false,
18-
model: self.model
19-
)
20-
}
15+
UKCheckbox(
16+
initialValue: false,
17+
model: self.model
18+
)
19+
.preview
2120
}
2221
PreviewWrapper(title: "SwiftUI") {
2322
SUCheckbox(

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/DividerPreview.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ struct DividerPreview: View {
88
var body: some View {
99
VStack {
1010
PreviewWrapper(title: "UIKit") {
11-
UKComponentPreview(model: self.model) {
12-
UKDivider(model: self.model)
13-
}
11+
UKDivider(model: self.model)
12+
.preview
1413
}
1514
PreviewWrapper(title: "SwiftUI") {
1615
SUDivider(model: self.model)
1716
}
1817
Form {
1918
Picker("Orientation", selection: self.$model.orientation) {
20-
Text("Horizontal").tag(DividerVM.DividerOrientation.horizontal)
21-
Text("Vertical").tag(DividerVM.DividerOrientation.vertical)
19+
Text("Horizontal").tag(DividerVM.Orientation.horizontal)
20+
Text("Vertical").tag(DividerVM.Orientation.vertical)
2221
}
2322
SizePicker(selection: self.$model.size)
2423
Picker("Color", selection: self.$model.color) {

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/InputFieldPreview.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ import SwiftUI
44
import UIKit
55

66
struct InputFieldPreview: View {
7-
@State private var model = InputFieldVM {
8-
$0.title = "Title"
9-
}
7+
@State private var model = Self.initialModel
108

119
@State private var text: String = ""
1210
@FocusState private var isFocused: Bool
1311

14-
@ObservedObject private var inputField = PreviewInputField()
12+
@ObservedObject private var inputField = PreviewInputField(model: Self.initialModel)
1513

1614
var body: some View {
1715
VStack {
1816
PreviewWrapper(title: "UIKit") {
19-
UKComponentPreview(model: self.model) {
20-
self.inputField
21-
}
17+
self.inputField
18+
.preview
19+
.onAppear {
20+
self.inputField.model = Self.initialModel
21+
}
22+
.onChange(of: self.model) { newValue in
23+
self.inputField.model = newValue
24+
}
2225
}
2326
PreviewWrapper(title: "SwiftUI") {
2427
SUInputField(
@@ -74,6 +77,12 @@ struct InputFieldPreview: View {
7477
}
7578
}
7679
}
80+
81+
private static var initialModel: InputFieldVM {
82+
return .init {
83+
$0.title = "Title"
84+
}
85+
}
7786
}
7887

7988
private final class PreviewInputField: UKInputField, ObservableObject, UITextFieldDelegate {

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/LoadingPreview.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ struct LoadingPreview: View {
88
var body: some View {
99
VStack {
1010
PreviewWrapper(title: "UIKit") {
11-
UKComponentPreview(model: self.model) {
12-
UKLoading(model: self.model)
13-
}
11+
UKLoading(model: self.model)
12+
.preview
1413
}
1514
PreviewWrapper(title: "SwiftUI") {
1615
SULoading(model: self.model)
@@ -23,7 +22,7 @@ struct LoadingPreview: View {
2322
}
2423
SizePicker(selection: self.$model.size)
2524
Picker("Style", selection: self.$model.style) {
26-
Text("Spinner").tag(LoadingStyle.spinner)
25+
Text("Spinner").tag(LoadingVM.Style.spinner)
2726
}
2827
}
2928
}

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/RadioGroupPreview.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ struct RadioGroupPreview: View {
2323
var body: some View {
2424
VStack {
2525
PreviewWrapper(title: "UIKit") {
26-
UKComponentPreview(model: self.model) {
27-
UKRadioGroup(model: self.model)
28-
}
26+
UKRadioGroup(model: self.model)
27+
.preview
2928
}
3029
PreviewWrapper(title: "SwiftUI") {
3130
SURadioGroup(selectedId: $selectedId, model: self.model)

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/SegmentedControlPreview.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ struct SegmentedControlPreview: View {
2828
var body: some View {
2929
VStack {
3030
PreviewWrapper(title: "UIKit") {
31-
UKComponentPreview(model: self.model) {
32-
UKSegmentedControl(
33-
selectedId: .iPad,
34-
model: self.model
35-
)
36-
}
31+
UKSegmentedControl(
32+
selectedId: .iPad,
33+
model: self.model
34+
)
35+
.preview
3736
}
3837
PreviewWrapper(title: "SwiftUI") {
3938
SUSegmentedControl(

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/TextInputPreview.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import SwiftUI
44
import UIKit
55

66
struct TextInputPreviewPreview: View {
7-
@State private var model = TextInputVM {
8-
$0.placeholder = "Placeholder"
9-
$0.minRows = 1
10-
$0.maxRows = nil
11-
}
7+
@State private var model = Self.initialModel
128

139
@State private var text: String = ""
1410
@FocusState private var isFocused: Bool
1511

16-
@ObservedObject private var textInput = PreviewTextInput()
12+
@ObservedObject private var textInput = PreviewTextInput(model: Self.initialModel)
1713

1814
var body: some View {
1915
VStack {
2016
PreviewWrapper(title: "UIKit") {
21-
UKComponentPreview(model: self.model) {
22-
self.textInput
23-
}
17+
self.textInput
18+
.preview
19+
.onAppear {
20+
self.textInput.model = Self.initialModel
21+
}
22+
.onChange(of: self.model) { newValue in
23+
self.textInput.model = newValue
24+
}
2425
}
2526
PreviewWrapper(title: "SwiftUI") {
2627
SUTextInput(
@@ -75,6 +76,14 @@ struct TextInputPreviewPreview: View {
7576
}
7677
}
7778
}
79+
80+
private static var initialModel: TextInputVM {
81+
return .init {
82+
$0.placeholder = "Placeholder"
83+
$0.minRows = 1
84+
$0.maxRows = nil
85+
}
86+
}
7887
}
7988

8089
private final class PreviewTextInput: UKTextInput, ObservableObject {
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import Foundation
22

3-
/// The buttons appearance style.
4-
public enum ButtonStyle: Hashable {
5-
/// A button with a filled background.
6-
case filled
7-
/// A button with a transparent background.
8-
case plain
9-
/// A button with a transparent background and a border.
10-
case bordered(BorderWidth)
3+
extension ButtonVM {
4+
/// The buttons appearance style.
5+
public enum Style: Hashable {
6+
/// A button with a filled background.
7+
case filled
8+
/// A button with a transparent background.
9+
case plain
10+
/// A button with a transparent background and a border.
11+
case bordered(BorderWidth)
12+
}
1113
}

0 commit comments

Comments
 (0)