93 lines
3.0 KiB
Swift
93 lines
3.0 KiB
Swift
//
|
|
// TextViewModel.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Kevin Christiano on 5/7/20.
|
|
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
|
|
open class TextViewModel: MoleculeModelProtocol {//, FormFieldProtocol, FormRuleWatcherFieldProtocol {
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
public class var identifier: String {
|
|
return ""
|
|
}
|
|
|
|
public var baseValue: AnyHashable?
|
|
public var backgroundColor: Color?
|
|
// public var fieldKey: String?
|
|
public var fontStyle: Styler.Font = Styler.Font.RegularBodySmall
|
|
public var text: String?
|
|
public var placeholder: String?
|
|
public var textAlignment: NSTextAlignment = .left
|
|
// public var groupName: String
|
|
public var editable: Bool = true
|
|
public var showsPlaceholder: Bool = false
|
|
public var placeholderTextColor: Color = Color(uiColor: .mvmCoolGray3)
|
|
public var placeholderFontStyle: Styler.Font = Styler.Font.RegularMicro
|
|
|
|
public var isValid: Bool? {
|
|
didSet { updateUI?() }
|
|
}
|
|
|
|
/// Temporary binding mechanism for the view to update on enable changes.
|
|
public var updateUI: ActionBlock?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Validation Methods
|
|
//--------------------------------------------------
|
|
|
|
public func formFieldValue() -> AnyHashable? {
|
|
return text
|
|
}
|
|
|
|
public func setValidity(_ valid: Bool, rule: RulesProtocol) {
|
|
self.isValid = valid
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Keys
|
|
//--------------------------------------------------
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case moleculeName
|
|
case backgroundColor
|
|
case editable
|
|
// case groupName
|
|
case textAlignment
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Codec
|
|
//--------------------------------------------------
|
|
|
|
required public init(from decoder: Decoder) throws {
|
|
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
if let editable = try typeContainer.decodeIfPresent(Bool.self, forKey: .editable) {
|
|
self.editable = editable
|
|
}
|
|
|
|
// if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
|
|
// self.groupName = groupName
|
|
// }
|
|
|
|
if let textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) {
|
|
self.textAlignment = textAlignment
|
|
}
|
|
|
|
}
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
// try container.encode(groupName, forKey: .groupName)
|
|
try container.encode(editable, forKey: .editable)
|
|
try container.encode(textAlignment, forKey: .textAlignment)
|
|
}
|
|
}
|