Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import UIKit

struct DividerPreview: View {
@State private var model = DividerVM()

var body: some View {
VStack {
PreviewWrapper(title: "UIKit") {
UKComponentPreview(model: self.model) {
UKDivider(model: self.model)
}
}
PreviewWrapper(title: "SwiftUI") {
SUDivider(model: self.model)
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/ComponentsKit/Divider/Models/DividerVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ extension DividerVM {
}
}
}

// MARK: - UIKit Helpers

extension DividerVM {
func shouldUpdateLayout(_ oldModel: Self) -> Bool {
return self.orientation != oldModel.orientation
|| self.size != oldModel.size
}
}
66 changes: 66 additions & 0 deletions Sources/ComponentsKit/Divider/UKDivider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import UIKit

/// A UIKit component that displays a separating line.
open class UKDivider: UIView, UKComponent {
// MARK: - Properties

/// A model that defines the appearance properties.
public var model: DividerVM {
didSet {
self.update(oldValue)
}
}

// MARK: - UIView Properties

open override var intrinsicContentSize: CGSize {
return self.sizeThatFits(UIView.layoutFittingExpandedSize)
}

// MARK: - Initializers

/// Initializer.
/// - Parameters:
/// - model: A model that defines the appearance properties.
public init(model: DividerVM = .init()) {
self.model = model
super.init(frame: .zero)
self.style()
}

public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Setup

private func style() {
self.backgroundColor = self.model.color.uiColor
self.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
self.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
}

// MARK: - Update

public func update(_ oldModel: DividerVM) {
guard self.model != oldModel else { return }

self.backgroundColor = self.model.color.uiColor

if self.model.shouldUpdateLayout(oldModel) {
self.invalidateIntrinsicContentSize()
}
}

// MARK: - UIView Methods

open override func sizeThatFits(_ size: CGSize) -> CGSize {
let lineSize = self.model.lineSize
switch self.model.orientation {
case .vertical:
return CGSize(width: lineSize, height: size.height)
case .horizontal:
return CGSize(width: size.width, height: lineSize)
}
}
}