Skip to content

Commit 871bad7

Browse files
authored
Merge pull request #25 from componentskit/Divider-UIKit
Divider UIKit
2 parents 9821894 + a4a993d commit 871bad7

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import UIKit
44

55
struct DividerPreview: View {
66
@State private var model = DividerVM()
7-
7+
88
var body: some View {
99
VStack {
10+
PreviewWrapper(title: "UIKit") {
11+
UKComponentPreview(model: self.model) {
12+
UKDivider(model: self.model)
13+
}
14+
}
1015
PreviewWrapper(title: "SwiftUI") {
1116
SUDivider(model: self.model)
1217
}

Sources/ComponentsKit/Divider/Models/DividerVM.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ extension DividerVM {
3535
}
3636
}
3737
}
38+
39+
// MARK: - UIKit Helpers
40+
41+
extension DividerVM {
42+
func shouldUpdateLayout(_ oldModel: Self) -> Bool {
43+
return self.orientation != oldModel.orientation
44+
|| self.size != oldModel.size
45+
}
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import UIKit
2+
3+
/// A UIKit component that displays a separating line.
4+
open class UKDivider: UIView, UKComponent {
5+
// MARK: - Properties
6+
7+
/// A model that defines the appearance properties.
8+
public var model: DividerVM {
9+
didSet {
10+
self.update(oldValue)
11+
}
12+
}
13+
14+
// MARK: - UIView Properties
15+
16+
open override var intrinsicContentSize: CGSize {
17+
return self.sizeThatFits(UIView.layoutFittingExpandedSize)
18+
}
19+
20+
// MARK: - Initializers
21+
22+
/// Initializer.
23+
/// - Parameters:
24+
/// - model: A model that defines the appearance properties.
25+
public init(model: DividerVM = .init()) {
26+
self.model = model
27+
super.init(frame: .zero)
28+
self.style()
29+
}
30+
31+
public required init?(coder: NSCoder) {
32+
fatalError("init(coder:) has not been implemented")
33+
}
34+
35+
// MARK: - Setup
36+
37+
private func style() {
38+
self.backgroundColor = self.model.color.uiColor
39+
self.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
40+
self.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
41+
}
42+
43+
// MARK: - Update
44+
45+
public func update(_ oldModel: DividerVM) {
46+
guard self.model != oldModel else { return }
47+
48+
self.backgroundColor = self.model.color.uiColor
49+
50+
if self.model.shouldUpdateLayout(oldModel) {
51+
self.invalidateIntrinsicContentSize()
52+
}
53+
}
54+
55+
// MARK: - UIView Methods
56+
57+
open override func sizeThatFits(_ size: CGSize) -> CGSize {
58+
let lineSize = self.model.lineSize
59+
switch self.model.orientation {
60+
case .vertical:
61+
return CGSize(width: lineSize, height: size.height)
62+
case .horizontal:
63+
return CGSize(width: size.width, height: lineSize)
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)