mvm_core_ui/MVMCoreUI/BaseClasses/TextViewModel.swift
2020-04-28 13:49:57 -04:00

108 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: TextEntryFieldModel {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public override class var identifier: String {
return "textView"
}
public var accessibilityText: 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 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 = "editable"
case hideBlinkingCaret
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------
required public init(from decoder: Decoder) throws {
try super.init(from: decoder)
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
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)
height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height)
}
public override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(moleculeName, forKey: .moleculeName)
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)
}
}