mvm_core_ui/MVMCoreUI/BaseClasses/TextViewModel.swift
Pfeil, Scott Robert 8a8bc84f99 three layer fill
2020-04-28 13:04:13 -04:00

125 lines
4.8 KiB
Swift

//
// TextViewModel.swift
// MVMCoreUI
//
// Created by Kevin Christiano on 4/2/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
open class TextViewModel: MoleculeModelProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "textView"
public var backgroundColor: Color?
public var accessibilityText: String?
public var text: String = ""
public var textColor: Color = Color(uiColor: .mvmBlack)
public var fontStyle: Styler.Font = Styler.Font.RegularBodySmall
public var textAlignment: NSTextAlignment = .left
public var height: CGFloat?
public var placeholder: String = ""
public var placeholderTextColor: Color = Color(uiColor: .mvmCoolGray3)
public var placeholderFontStyle: Styler.Font = Styler.Font.RegularMicro
public var showsPlaceholder: Bool = true
public var isEditable: Bool = true
public var borderColor: Color?
public var borderWidth: CGFloat = 0
//--------------------------------------------------
// MARK: - Keys
//--------------------------------------------------
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor
case text
case accessibilityText
case textColor
case fontStyle
case textAlignment
case attributes
case height
case borderColor
case borderWidth
case placeholder
case placeholderFontStyle
case isEditable
}
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
public init(height: CGFloat, text: String = "") {
self.height = height
self.text = text
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
if let text = try typeContainer.decodeIfPresent(String.self, forKey: .text) {
self.text = text
}
if let placeholder = try typeContainer.decodeIfPresent(String.self, forKey: .placeholder) {
self.placeholder = placeholder
}
if let placeholderFontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .placeholderFontStyle) {
self.placeholderFontStyle = placeholderFontStyle
}
if let textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) {
self.textAlignment = textAlignment
}
if let textColor = try typeContainer.decodeIfPresent(Color.self, forKey: .textColor) {
self.textColor = textColor
}
if let isEditable = try typeContainer.decodeIfPresent(Bool.self, forKey: .isEditable) {
self.isEditable = isEditable
}
if let borderWidth = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .borderWidth) {
self.borderWidth = borderWidth
}
if let fontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .fontStyle) {
self.fontStyle = fontStyle
}
borderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor)
accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
try container.encodeIfPresent(textColor, forKey: .textColor)
try container.encodeIfPresent(fontStyle, forKey: .fontStyle)
try container.encodeIfPresent(borderColor, forKey: .borderColor)
try container.encodeIfPresent(height, forKey: .height)
try container.encode(text, forKey: .text)
try container.encode(placeholder, forKey: .placeholder)
try container.encode(placeholderFontStyle, forKey: .placeholderFontStyle)
try container.encode(textAlignment, forKey: .textAlignment)
try container.encode(isEditable, forKey: .isEditable)
try container.encode(borderWidth, forKey: .borderWidth)
}
}