146 lines
4.8 KiB
Swift
146 lines
4.8 KiB
Swift
//
|
|
// Helper.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 11/18/22.
|
|
//
|
|
|
|
import Foundation
|
|
import VDS
|
|
import UIKit
|
|
|
|
extension UIView {
|
|
public func makeWrapper(edgeSpacing: CGFloat = 0.0, isTrailing: Bool = true, isBottom: Bool = false) -> View {
|
|
UIView.makeWrapper(for: self, edgeSpacing: edgeSpacing, isTrailing: isTrailing, isBottom: isBottom)
|
|
}
|
|
|
|
public static func makeWrapper(for view: UIView, edgeSpacing: CGFloat = 0.0, isTrailing: Bool = true, isBottom: Bool = false) -> View {
|
|
makeWrapperWithConstraints(for: view, edgeSpacing: edgeSpacing, isTrailing: isTrailing, isBottom: isBottom).view
|
|
}
|
|
|
|
public static func makeWrapperWithConstraints(for view: UIView, edgeSpacing: CGFloat = 0.0, isTrailing: Bool = true, isBottom: Bool = false) -> (view: View, container: NSLayoutConstraint.Container) {
|
|
let container = NSLayoutConstraint.Container()
|
|
let wrapper = View().with {
|
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
wrapper.addSubview(view)
|
|
|
|
container.topConstraint = view.pinTop(anchor: wrapper.topAnchor, constant: edgeSpacing)
|
|
if isBottom {
|
|
container.bottomConstraint = view.pinBottomLessThanOrEqualTo(anchor: wrapper.bottomAnchor, constant: edgeSpacing)
|
|
} else {
|
|
container.bottomConstraint = view.pinBottom(anchor: wrapper.bottomAnchor, constant: edgeSpacing)
|
|
}
|
|
container.leadingConstraint = view.pinLeading(anchor: wrapper.leadingAnchor, constant: edgeSpacing)
|
|
if isTrailing {
|
|
container.trailingConstraint = view.pinTrailingLessThanOrEqualTo(anchor: wrapper.trailingAnchor, constant: edgeSpacing)
|
|
} else {
|
|
container.trailingConstraint = view.pinTrailing(anchor: wrapper.trailingAnchor, constant: edgeSpacing)
|
|
}
|
|
return (wrapper, container)
|
|
}
|
|
}
|
|
|
|
func labelPublisherCompletionHandler() -> (String, UILabel) -> () {
|
|
return { (text, label) in
|
|
let newText = "\(text) clicked - "
|
|
if let labelText = label.text {
|
|
let components = labelText.components(separatedBy: " - ")
|
|
let last: String = (components.last ?? "0").trimmingCharacters(in: .whitespaces)
|
|
let count = Int(last)!
|
|
label.text = "\(newText)\(count+1)"
|
|
} else {
|
|
label.text = "\(newText)1"
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Changeable {
|
|
func onChangeActionPublisher(_ text: String, label: UILabel) {
|
|
onChange = { _ in
|
|
let handler = labelPublisherCompletionHandler()
|
|
handler(text, label)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Clickable where Self: UIControl {
|
|
func onClickActionPublisher(_ text: String, label: UILabel) {
|
|
onClick = { _ in
|
|
let handler = labelPublisherCompletionHandler()
|
|
handler(text, label)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Clickable where Self: UIView {
|
|
func onClickActionPublisher(_ text: String, label: UILabel) {
|
|
onClick = { _ in
|
|
let handler = labelPublisherCompletionHandler()
|
|
handler(text, label)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ButtonBase {
|
|
func labelPublisher(_ label: UILabel){
|
|
onClick = { control in
|
|
let handler = labelPublisherCompletionHandler()
|
|
handler(control.text!, label)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension BaseViewController {
|
|
func makeButton(_ text: String, label: UILabel) -> Button {
|
|
return Button().with{
|
|
$0.text = text
|
|
$0.labelPublisher(label)
|
|
}
|
|
}
|
|
|
|
func makeTextLink(_ text: String, label: UILabel) -> TextLink {
|
|
return TextLink().with{
|
|
$0.text = text
|
|
$0.labelPublisher(label)
|
|
}
|
|
}
|
|
|
|
func makeTextLinkCaret(_ text: String, label: UILabel) -> TextLinkCaret {
|
|
return TextLinkCaret().with{
|
|
$0.text = text
|
|
$0.labelPublisher(label)
|
|
}
|
|
}
|
|
|
|
func makeTilelet(badge: String? = nil, eyebrow: String? = nil, title: String? = nil, subTitle: String? = nil) -> Tilelet {
|
|
var badgeModel: Tilelet.BadgeModel?
|
|
if let badge {
|
|
badgeModel = Tilelet.BadgeModel(text: badge)
|
|
}
|
|
|
|
var eyebrowModel: Tilelet.EyebrowModel?
|
|
if let eyebrow {
|
|
eyebrowModel = Tilelet.EyebrowModel(text: eyebrow)
|
|
}
|
|
|
|
var titleModel: Tilelet.TitleModel?
|
|
if let title {
|
|
titleModel = Tilelet.TitleModel(text: title)
|
|
}
|
|
|
|
var subTitleModel: Tilelet.SubTitleModel?
|
|
if let subTitle {
|
|
subTitleModel = Tilelet.SubTitleModel(text: subTitle)
|
|
}
|
|
|
|
return .init().with {
|
|
$0.surface = .light
|
|
$0.badgeModel = badgeModel
|
|
$0.eyebrowModel = eyebrowModel
|
|
$0.titleModel = titleModel
|
|
$0.subTitleModel = subTitleModel
|
|
}
|
|
}
|
|
}
|