mvm_core_ui/MVMCoreUI/BaseClasses/TextViewModel.swift
Kevin G Christiano 8141fa934f latest textview
2020-04-08 13:53:53 -04:00

109 lines
4.1 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 text: String = ""
public var accessibilityText: String?
public var textColor: Color = Color(uiColor: .mvmBlack)
public var fontStyle: LabelModel.FontStyle?
public var textAlignment: NSTextAlignment = .left
public var height: CGFloat?
public var placeholder: String = ""
public var placeholderFont: LabelModel.FontStyle = LabelModel.FontStyle.RegularMicro
public var isEditable: Bool = true
//--------------------------------------------------
// MARK: - Keys
//--------------------------------------------------
private enum CodingKeys: String, CodingKey {
case moleculeName
case text
case accessibilityText
case textColor
case backgroundColor
case fontStyle
case textAlignment
case attributes
case height
case placeholder
case placeholderFont
case isEditable
}
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
public init(text: String) {
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 placeholderFont = try typeContainer.decodeIfPresent(String.self, forKey: .placeholderFont),
let font = LabelModel.FontStyle(rawValue: placeholderFont) {
self.placeholderFont = font
}
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
}
accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
fontStyle = try typeContainer.decodeIfPresent(LabelModel.FontStyle.self, forKey: .fontStyle)
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.encode(text, forKey: .text)
try container.encode(placeholder, forKey: .placeholder)
try container.encode(placeholderFont, forKey: .placeholderFont)
try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
try container.encodeIfPresent(textColor, forKey: .textColor)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(fontStyle, forKey: .fontStyle)
try container.encode(textAlignment, forKey: .textAlignment)
try container.encodeIfPresent(height, forKey: .height)
try container.encode(isEditable, forKey: .isEditable)
}
}