Skip to content

Commit ba0c31e

Browse files
implement SUCard
1 parent a185afb commit ba0c31e

File tree

5 files changed

+87
-26
lines changed

5 files changed

+87
-26
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/CardPreview.swift

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ struct CardPreview: View {
88
var body: some View {
99
VStack {
1010
PreviewWrapper(title: "UIKit") {
11-
UKCard(model: self.model, content: cardContent)
11+
UKCard(model: self.model, content: self.ukCardContent)
1212
.preview
1313
}
14+
PreviewWrapper(title: "SwiftUI") {
15+
SUCard(model: self.model, content: self.suCardContent)
16+
}
1417
Form {
1518
Picker("Background Color", selection: self.$model.backgroundColor) {
1619
Text("Default").tag(Optional<UniversalColor>.none)
@@ -38,30 +41,42 @@ struct CardPreview: View {
3841
}
3942
}
4043
}
41-
}
4244

43-
#Preview {
44-
CardPreview()
45-
}
45+
// MARK: - Helpers
46+
47+
private func ukCardContent() -> UIView {
48+
let titleLabel = UILabel()
49+
titleLabel.text = "Card"
50+
titleLabel.font = UniversalFont.mdHeadline.uiFont
51+
titleLabel.textColor = UniversalColor.foreground.uiColor
52+
titleLabel.numberOfLines = 0
4653

47-
// MARK: - Helpers
54+
let subtitleLabel = UILabel()
55+
subtitleLabel.text = "Card is a container for text, images, and other content."
56+
subtitleLabel.font = UniversalFont.mdBody.uiFont
57+
subtitleLabel.textColor = UniversalColor.secondaryForeground.uiColor
58+
subtitleLabel.numberOfLines = 0
4859

49-
private func cardContent() -> UIView {
50-
let titleLabel = UILabel()
51-
titleLabel.text = "Card"
52-
titleLabel.font = UniversalFont.mdHeadline.uiFont
53-
titleLabel.textColor = UniversalColor.foreground.uiColor
54-
titleLabel.numberOfLines = 0
60+
let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
61+
stackView.axis = .vertical
62+
stackView.spacing = 8
5563

56-
let subtitleLabel = UILabel()
57-
subtitleLabel.text = "Card is a container for text, images, and other content."
58-
subtitleLabel.font = UniversalFont.mdBody.uiFont
59-
subtitleLabel.textColor = UniversalColor.secondaryForeground.uiColor
60-
subtitleLabel.numberOfLines = 0
64+
return stackView
65+
}
6166

62-
let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
63-
stackView.axis = .vertical
64-
stackView.spacing = 8
67+
private func suCardContent() -> some View {
68+
VStack(alignment: .leading, spacing: 8) {
69+
Text("Card")
70+
.foregroundStyle(UniversalColor.foreground.color)
71+
.font(UniversalFont.mdHeadline.font)
6572

66-
return stackView
73+
Text("Card is a container for text, images, and other content.")
74+
.foregroundStyle(UniversalColor.secondaryForeground.color)
75+
.font(UniversalFont.mdBody.font)
76+
}
77+
}
78+
}
79+
80+
#Preview {
81+
CardPreview()
6782
}

Sources/ComponentsKit/Components/Card/CardVM.swift renamed to Sources/ComponentsKit/Components/Card/Models/CardVM.swift

File renamed without changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import SwiftUI
2+
3+
public struct SUCard<Content: View>: View {
4+
let model: CardVM
5+
6+
@ViewBuilder private let content: () -> Content
7+
8+
public init(model: CardVM, content: @escaping () -> Content) {
9+
self.model = model
10+
self.content = content
11+
}
12+
13+
public var body: some View {
14+
self.content()
15+
.padding(self.model.contentPaddings.edgeInsets)
16+
.background(self.model.preferredBackgroundColor.color)
17+
.background(UniversalColor.background.color)
18+
.cornerRadius(self.model.cornerRadius.value)
19+
.overlay(
20+
RoundedRectangle(cornerRadius: self.model.cornerRadius.value)
21+
.stroke(UniversalColor.divider.color, lineWidth: 1.0)
22+
)
23+
.shadow(self.model.shadow)
24+
}
25+
}
26+
27+
extension View {
28+
func shadow(_ shadow: Shadow) -> some View {
29+
self.shadow(
30+
color: shadow.color.color,
31+
radius: shadow.radius,
32+
x: shadow.offset.width,
33+
y: shadow.offset.height
34+
)
35+
}
36+
}

Sources/ComponentsKit/Helpers/Paddings.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Foundation
1+
import SwiftUI
22

33
/// Defines padding values for each edge.
44
public struct Paddings: Hashable {
@@ -50,3 +50,16 @@ public struct Paddings: Hashable {
5050
self.trailing = padding
5151
}
5252
}
53+
54+
// MARK: - SwiftUI Helpers
55+
56+
extension Paddings {
57+
var edgeInsets: EdgeInsets {
58+
return EdgeInsets(
59+
top: self.top,
60+
leading: self.leading,
61+
bottom: self.bottom,
62+
trailing: self.trailing
63+
)
64+
}
65+
}

Sources/ComponentsKit/Modal/SwiftUI/ModalContent.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ struct ModalContent<VM: ModalVM, Header: View, Body: View, Footer: View>: View {
6363
.clipShape(RoundedRectangle(
6464
cornerRadius: self.model.cornerRadius.value
6565
))
66-
.padding(.top, self.model.outerPaddings.top)
67-
.padding(.leading, self.model.outerPaddings.leading)
68-
.padding(.bottom, self.model.outerPaddings.bottom)
69-
.padding(.trailing, self.model.outerPaddings.trailing)
66+
.padding(self.model.outerPaddings.edgeInsets)
7067
}
7168

7269
private var bodyTopPadding: CGFloat {

0 commit comments

Comments
 (0)