Skip to content

Commit d10a78f

Browse files
authored
Merge pull request #20 from ServerDriven/develop
0.7.0
2 parents 69f9f90 + 5d47016 commit d10a78f

File tree

10 files changed

+141
-121
lines changed

10 files changed

+141
-121
lines changed

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,16 @@ struct ScreenDataContentView: View {
7979
public var baseID: String
8080

8181
var body: some View {
82-
guard let screen = store.screen else {
83-
return AnyView(
84-
ProgressView()
85-
.onAppear {
86-
store.fetch(screenID: baseID)
87-
}
88-
)
89-
}
90-
91-
return AnyView(
82+
if let screen = store.screen {
9283
NavigationView {
9384
SDScreen(screen: screen)
9485
}
95-
)
86+
} else {
87+
ProgressView()
88+
.onAppear {
89+
store.fetch(screenID: baseID)
90+
}
91+
}
9692
}
9793
}
9894
```

Sources/ScreenDataUI/Helpers/SomeView+SDUI.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,31 @@ import SwiftUI
99
import ScreenData
1010

1111
public extension SomeView {
12+
13+
@ViewBuilder
1214
var ui: some View {
13-
switch type {
14-
case .button:
15-
return AnyView(SDButton(button: someButton!))
16-
case .label:
17-
return AnyView(SDLabel(label: someLabel!))
18-
case .text:
19-
return AnyView(SDText(text: someText!))
20-
case .container:
21-
return AnyView(SDContainerView(container: someContainer!))
22-
case .image:
23-
return AnyView(SDImage(image: someImage!))
24-
case .spacer:
25-
guard let size = someSpacer?.size else {
26-
return AnyView(Spacer())
15+
if type == .button {
16+
SDButton(button: someButton!)
17+
} else if type == .label {
18+
SDLabel(label: someLabel!)
19+
} else if type == .text {
20+
SDText(text: someText!)
21+
} else if type == .container {
22+
SDContainerView(container: someContainer!)
23+
} else if type == .image {
24+
SDImage(image: someImage!)
25+
} else if type == .spacer {
26+
if let size = someSpacer?.size {
27+
Spacer().frame(width: CGFloat(size),
28+
height: CGFloat(size),
29+
alignment: .center)
30+
} else {
31+
Spacer()
2732
}
28-
29-
return AnyView(Spacer().frame(width: CGFloat(size),
30-
height: CGFloat(size),
31-
alignment: .center))
32-
case .custom:
33-
return AnyView(SDCustomView(custom: someCustomView!))
33+
} else if type == .custom {
34+
SDCustomView(custom: someCustomView!)
35+
} else {
36+
EmptyView()
3437
}
3538
}
3639
}

Sources/ScreenDataUI/Modifiers/SDStyleModifier.swift

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,49 @@ public struct SDStyleModifier: ViewModifier {
2727
return CGFloat(height)
2828
}
2929

30+
@ViewBuilder
3031
public func body(content: Content) -> some View {
3132
if style.isHidden {
32-
return AnyView(
33-
content
34-
.hidden()
35-
.frame(width: 0, height: 0, alignment: .center)
36-
)
33+
content
34+
.hidden()
35+
.frame(width: 0, height: 0, alignment: .center)
36+
} else if style.padding != 0 {
37+
content
38+
.padding(CGFloat(style.padding))
39+
.background(
40+
style.backgroundColor.map {
41+
SDColor(color: $0)
42+
} ?? SDColor(color: SomeColor(red: 0, green: 0, blue: 0, alpha: 0))
43+
)
44+
.cornerRadius(CGFloat(style.cornerRadius))
45+
.foregroundColor(
46+
style.foregroundColor.map {
47+
SDColor(color: $0).body as? Color
48+
} ?? Color.primary
49+
)
50+
.frame(
51+
width: width,
52+
height: height,
53+
alignment: .center
54+
)
3755
} else {
38-
return AnyView(
39-
content
40-
.padding(CGFloat(style.padding))
41-
.background(
42-
style.backgroundColor.map {
43-
AnyView(SDColor(color: $0))
44-
} ?? AnyView(Color.clear)
45-
)
46-
.cornerRadius(CGFloat(style.cornerRadius))
47-
.foregroundColor(style.foregroundColor.map {
56+
content
57+
.background(
58+
style.backgroundColor.map {
59+
SDColor(color: $0)
60+
} ?? SDColor(color: SomeColor(red: 0, green: 0, blue: 0, alpha: 0))
61+
)
62+
.cornerRadius(CGFloat(style.cornerRadius))
63+
.foregroundColor(
64+
style.foregroundColor.map {
4865
SDColor(color: $0).body as? Color
49-
} ?? Color.primary)
50-
.frame(
51-
width: width,
52-
height: height,
53-
alignment: .center
54-
)
55-
)
66+
} ?? Color.primary
67+
)
68+
.frame(
69+
width: width,
70+
height: height,
71+
alignment: .center
72+
)
5673
}
57-
5874
}
5975
}

Sources/ScreenDataUI/Navigation/SDScreenProvider.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public struct SDScreenProvider: ScreenProviding {
1717

1818
public func screen(forID id: String) -> AnyPublisher<SomeScreen, Error> {
1919
guard let provider = SDScreenProvider.default else {
20-
return ScreenDataNavigation.MockScreenProvider(mockScreen:
21-
SomeScreen(title: "Mock Screen",
22-
backgroundColor: SomeColor(red: 1, green: 1, blue: 1),
23-
someView: SomeText(title: "Set SDScreenProvider.default").someView)
20+
return ScreenDataNavigation.MockScreenProvider(
21+
mockScreen: SomeScreen(
22+
title: "Mock Screen",
23+
backgroundColor: SomeColor(red: 1, green: 1, blue: 1),
24+
someView: SomeText(title: "Set SDScreenProvider.default").someView
25+
)
2426
)
2527
.screen(forID: id)
2628
.eraseToAnyPublisher()

Sources/ScreenDataUI/Navigation/SDScreenStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct SDScreenStore: ScreenStoring {
1818

1919
public func store(screens: [SomeScreen]) -> AnyPublisher<Void, Error> {
2020
guard let store = SDScreenStore.default else {
21-
log(level: .info("SAVED Screens: \(screens.map(\.id))"))
21+
log(level: .info("Saving SomeScreens: \(screens.map(\.id))"))
2222
return ScreenDataNavigation.UserDefaultScreenStorer(baseKey: "SDScreenStore")
2323
.store(screens: screens)
2424
}

Sources/ScreenDataUI/Views/SDButton.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,27 @@ public struct SDButton: View {
3131
}
3232

3333
public var body: some View {
34-
guard button.destination != nil else {
35-
return AnyView(
36-
Button(
37-
action: buttonAction,
38-
label: {
39-
Text(button.title)
40-
.font(SDFont.font(for: .body))
41-
.background(with: button.style)
42-
}
43-
)
44-
)
45-
}
46-
47-
return AnyView(
34+
if let destination = button.destination {
4835
SDDestinationLink(
4936
provider: SDScreenProvider(),
50-
destination: button.destination,
37+
destination: destination,
5138
action: buttonAction,
5239
content: {
5340
Text(button.title)
5441
.font(SDFont.font(for: .body))
5542
.background(with: button.style)
5643
}
5744
)
58-
)
45+
} else {
46+
Button(
47+
action: buttonAction,
48+
label: {
49+
Text(button.title)
50+
.font(SDFont.font(for: .body))
51+
.background(with: button.style)
52+
}
53+
)
54+
}
5955
}
6056

6157
private func buttonAction() {

Sources/ScreenDataUI/Views/SDCustomView.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ public struct SDCustomView: View {
2727
}
2828

2929
public var body: some View {
30-
guard let id = custom.id,
31-
let customView = SDCustomView.customViews[id] else {
32-
return AnyView(
33-
Text("404")
34-
.font(.title)
35-
.foregroundColor(.red)
36-
)
30+
if let id = custom.id,
31+
let customView = SDCustomView.customViews[id] {
32+
customView.view(forSomeCustomView: custom)
33+
} else {
34+
Text("404")
35+
.font(.title)
36+
.foregroundColor(.red)
3737
}
38-
39-
return customView.view(forSomeCustomView: custom)
4038
}
4139
}

Sources/ScreenDataUI/Views/SDDestinationLink.swift

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,57 +63,54 @@ public struct SDDestinationLink<Content>: View where Content: View {
6363

6464
public var body: some View {
6565
if destination == nil {
66-
return AnyView(content())
67-
}
68-
69-
guard destination?.type != .url else {
70-
return AnyView(
71-
Button(action: {
66+
content()
67+
} else if destination?.type == .url {
68+
Button(
69+
action: {
7270
action?()
7371
if let destinationURL = destination?.toID,
7472
let url = URL(string: destinationURL) {
7573
openURL(url)
7674
}
77-
}, label: {
75+
},
76+
label: {
7877
content()
79-
})
78+
}
8079
)
81-
}
82-
83-
guard let destinationView = store.destinationView else {
84-
return AnyView(loadingView)
85-
}
86-
87-
return AnyView(
80+
} else if let destinationView = store.destinationView {
8881
NavigationLink(
8982
destination: destinationView,
9083
isActive: $isPresentingDestination,
9184
label: {
92-
Button(action: {
93-
action?()
94-
isPresentingDestination = true
95-
}, label: {
96-
content()
97-
})
85+
Button(
86+
action: {
87+
action?()
88+
isPresentingDestination = true
89+
},
90+
label: {
91+
content()
92+
}
93+
)
9894
}
9995
)
10096
.onAppear {
10197
store.load(destination: destination, provider: provider)
10298
}
103-
)
99+
} else {
100+
loadingView
101+
}
104102
}
105103

104+
@ViewBuilder
106105
public var loadingView: some View {
107-
guard let destination = destination else {
108-
return AnyView(content())
109-
}
110-
111-
return AnyView(
106+
if let destination = destination {
112107
ProgressView()
113108
.onAppear {
114109
store.load(destination: destination,
115110
provider: provider)
116111
}
117-
)
112+
} else {
113+
content()
114+
}
118115
}
119116
}

Sources/ScreenDataUI/Views/SDImage.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,21 @@ public struct SDImage: View {
6767
public var image: SomeImage
6868
private var progressTint: Color
6969

70-
private var width: CGFloat? {
70+
private var width: CGFloat {
7171
guard let width = image.style?.width else {
72-
return nil
72+
return .infinity
73+
}
74+
75+
if CGFloat(width) >= UIScreen.main.bounds.width {
76+
return .infinity
7377
}
7478

7579
return CGFloat(width)
7680
}
7781

78-
private var height: CGFloat? {
82+
private var height: CGFloat {
7983
guard let height = image.style?.height else {
80-
return nil
84+
return .infinity
8185
}
8286

8387
return CGFloat(height)
@@ -92,7 +96,11 @@ public struct SDImage: View {
9296
Image(uiImage: loadedImage)
9397
.resizable()
9498
.aspectRatio(contentMode: image.aspectScale == ImageAspectScale.fit ? .fit : .fill)
95-
.frame(width: width ?? UIScreen.main.bounds.width, height: height, alignment: .center)
99+
.frame(
100+
minWidth: 0, maxWidth: width,
101+
minHeight: 0, maxHeight: height,
102+
alignment: .center
103+
)
96104
} else {
97105
ProgressView()
98106
.progressViewStyle(CircularProgressViewStyle(tint: progressTint))

Sources/ScreenDataUI/Views/SDScreen.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ public struct SDScreen: View, Equatable {
4646
footerView.ui
4747
}
4848
}
49-
.background(Color(red: Double(screen.backgroundColor.red),
50-
green: Double(screen.backgroundColor.green),
51-
blue: Double(screen.backgroundColor.blue),
52-
opacity: Double(screen.backgroundColor.alpha)))
49+
.background(
50+
Color(
51+
red: Double(screen.backgroundColor.red),
52+
green: Double(screen.backgroundColor.green),
53+
blue: Double(screen.backgroundColor.blue),
54+
opacity: Double(screen.backgroundColor.alpha)
55+
)
56+
)
5357
.navigationBarTitle(screen.title)
5458
}
5559
}

0 commit comments

Comments
 (0)