vds_ios/VDS/Components/Label/VDSLabel.swift
Matt Bruce 831f4baf18 moved default models
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-07-30 09:20:01 -05:00

70 lines
1.8 KiB
Swift

//
// VDSLabel.swift
// VDS
//
// Created by Matt Bruce on 7/28/22.
//
import Foundation
import UIKit
import VDSColorTokens
import Combine
open class VDSLabel: UILabel, Modelable {
@Published public var model: VDSLabelModel = DefaultLabelModel()
private var cancellable: AnyCancellable?
@Proxy(\.model.fontSize)
public var fontSize: VDSFontSize
@Proxy(\.model.textPosition)
public var textPosition: VDSTextPosition
@Proxy(\.model.fontWeight)
public var fontWeight: VDSFontWeight
@Proxy(\.model.fontCategory)
public var fontCategory: VDSFontCategory
@Proxy(\.model.surface)
public var surface: Surface
//Initializers
public convenience init() {
self.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required public init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
private func setup() {
cancellable = $model.debounce(for: .seconds(ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in
self?.onStateChange(viewModel: viewModel)
}
}
//functions
private func onStateChange(viewModel: VDSLabelModel) {
textAlignment = viewModel.textPosition.textAlignment
textColor = viewModel.surface == .dark ? VDSColor.elementsPrimaryOndark : VDSColor.elementsPrimaryOnlight
guard let vdsFont = try? VDSFontStyle.font(for: viewModel.fontCategory, fontWeight: viewModel.fontWeight, fontSize: viewModel.fontSize) else {
font = VDSFontStyle.RegularBodyLarge.font
return
}
font = vdsFont
}
//Modelable
public func set(with model: VDSLabelModel) {
self.model = model
}
}