diff --git a/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift b/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift index 2b2ef27b..54b441f7 100644 --- a/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift +++ b/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift @@ -29,7 +29,7 @@ import Foundation public var fieldKey: String? public var groupName: String? = FormValidator.defaultGroupName public var text: String? - public var baseValue: JSONValue? + public var baseValue: AnyHashable? public var isValid: Bool? { didSet { @@ -61,12 +61,8 @@ import Foundation case groupName } - public func formFieldValue() -> JSONValue? { - if let text = text { - return JSONValue(stringLiteral: text) - } else { - return nil - } + public func formFieldValue() -> AnyHashable? { + return text } //-------------------------------------------------- diff --git a/MVMCoreUI/Atoms/Views/CheckboxModel.swift b/MVMCoreUI/Atoms/Views/CheckboxModel.swift index 70224200..13b730a6 100644 --- a/MVMCoreUI/Atoms/Views/CheckboxModel.swift +++ b/MVMCoreUI/Atoms/Views/CheckboxModel.swift @@ -33,7 +33,7 @@ import Foundation public var fieldKey: String? public var fieldValue: JSONValue? public var groupName: String? = FormValidator.defaultGroupName - public var baseValue: JSONValue? + public var baseValue: AnyHashable? //-------------------------------------------------- // MARK: - Keys @@ -61,8 +61,8 @@ import Foundation init(isChecked: Bool = false) {} - public func formFieldValue() -> JSONValue? { - return JSONValue(booleanLiteral: isChecked) + public func formFieldValue() -> AnyHashable? { + return isChecked } //-------------------------------------------------- diff --git a/MVMCoreUI/Atoms/Views/RadioButton.swift b/MVMCoreUI/Atoms/Views/RadioButton.swift index 49625eb2..2c110b4e 100644 --- a/MVMCoreUI/Atoms/Views/RadioButton.swift +++ b/MVMCoreUI/Atoms/Views/RadioButton.swift @@ -83,11 +83,8 @@ import UIKit return radioModel?.fieldKey } - public func formFieldValue() -> JSONValue? { - if let fieldValue = radioModel?.fieldValue { - return JSONValue(stringLiteral: fieldValue) - } - return nil + public func formFieldValue() -> AnyHashable? { + return radioModel?.fieldValue } // MARK: - MVMViewProtocol diff --git a/MVMCoreUI/Atoms/Views/RadioButtonModel.swift b/MVMCoreUI/Atoms/Views/RadioButtonModel.swift index a8a5de28..0927866c 100644 --- a/MVMCoreUI/Atoms/Views/RadioButtonModel.swift +++ b/MVMCoreUI/Atoms/Views/RadioButtonModel.swift @@ -10,19 +10,53 @@ import Foundation import MVMCore public class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol { + public static var identifier: String = "radioButton" public var backgroundColor: Color? public var state: Bool? = false public var fieldKey: String? public var groupName: String? = FormValidator.defaultGroupName public var fieldValue: String? - public var baseValue: JSONValue? + public var baseValue: AnyHashable? - public func formFieldValue() -> JSONValue? { - if let fieldValue = fieldValue { - return JSONValue(stringLiteral: fieldValue) - } else { - return nil + public func formFieldValue() -> AnyHashable? { + return fieldValue + } + + private enum CodingKeys: String, CodingKey { + case moleculeName + case state + case backgroundColor + case fieldKey + case groupName + case fieldValue + } + + public init(_ state: Bool) { + self.state = state + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + if let state = try typeContainer.decodeIfPresent(Bool.self, forKey: .state) { + self.state = state + } + + backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + fieldKey = try typeContainer.decodeIfPresent(String.self, forKey: .fieldKey) + fieldValue = try typeContainer.decodeIfPresent(String.self, forKey: .fieldValue) + if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { + self.groupName = groupName } } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) + try container.encode(moleculeName, forKey: .moleculeName) + try container.encodeIfPresent(state, forKey: .state) + try container.encodeIfPresent(fieldKey, forKey: .fieldKey) + try container.encodeIfPresent(fieldValue, forKey: .fieldValue) + try container.encodeIfPresent(groupName, forKey: .groupName) + } } diff --git a/MVMCoreUI/Atoms/Views/ToggleModel.swift b/MVMCoreUI/Atoms/Views/ToggleModel.swift index afe9ed41..18022833 100644 --- a/MVMCoreUI/Atoms/Views/ToggleModel.swift +++ b/MVMCoreUI/Atoms/Views/ToggleModel.swift @@ -17,7 +17,7 @@ public class ToggleModel: MoleculeModelProtocol, FormFieldProtocol { public var alternateAction: ActionModelProtocol? public var fieldKey: String? public var groupName: String? = FormValidator.defaultGroupName - public var baseValue: JSONValue? + public var baseValue: AnyHashable? private enum CodingKeys: String, CodingKey { case moleculeName @@ -30,8 +30,8 @@ public class ToggleModel: MoleculeModelProtocol, FormFieldProtocol { case groupName } - public func formFieldValue() -> JSONValue? { - return JSONValue(booleanLiteral: state) + public func formFieldValue() -> AnyHashable? { + return state } public init(_ state: Bool) { diff --git a/MVMCoreUI/FormUIHelpers/New/FormFieldProtocol.swift b/MVMCoreUI/FormUIHelpers/New/FormFieldProtocol.swift index 0a099000..c59ccd08 100644 --- a/MVMCoreUI/FormUIHelpers/New/FormFieldProtocol.swift +++ b/MVMCoreUI/FormUIHelpers/New/FormFieldProtocol.swift @@ -11,12 +11,12 @@ import Foundation public protocol FormFieldProtocol: FormItemProtocol { var fieldKey: String? { get set } - var baseValue: JSONValue? { get set } - func formFieldValue() -> JSONValue? + var baseValue: AnyHashable? { get set } + func formFieldValue() -> AnyHashable? } extension FormFieldProtocol { - var baseValue: JSONValue? { + var baseValue: AnyHashable? { return nil } } diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/FormGroupRule.swift b/MVMCoreUI/FormUIHelpers/New/Rules/FormGroupRule.swift index 29872de7..d416ef3c 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/FormGroupRule.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/FormGroupRule.swift @@ -10,7 +10,6 @@ import Foundation open class FormGroupRule: Codable { -// public static var identifier: String = "formRule" var groupName: String var rules: [RulesProtocol] diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/RuleAllValueChangedModel.swift b/MVMCoreUI/FormUIHelpers/New/Rules/RuleAllValueChangedModel.swift index a33cb64c..7535c6dd 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/RuleAllValueChangedModel.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/RuleAllValueChangedModel.swift @@ -11,7 +11,7 @@ import Foundation public class RuleAllValueChangedModel: RulesProtocol { public static var identifier: String = "allValueChanged" - public var ruleType: String = RuleAllValueChangedModel.identifier + public var type: String = RuleAllValueChangedModel.identifier public var fields: [String] public func isValid(_ formField: FormFieldProtocol) -> Bool { diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/RuleAnyValueChangedModel.swift b/MVMCoreUI/FormUIHelpers/New/Rules/RuleAnyValueChangedModel.swift index 80b95514..f52dd92d 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/RuleAnyValueChangedModel.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/RuleAnyValueChangedModel.swift @@ -11,7 +11,7 @@ import Foundation public class RuleAnyValueChangedModel: RulesProtocol { public static var identifier: String = "anyValueChanged" - public var ruleType: String = RuleAnyValueChangedModel.identifier + public var type: String = RuleAnyValueChangedModel.identifier public var fields: [String] public func isValid(_ formField: FormFieldProtocol) -> Bool { diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/RuleEqualsModel.swift b/MVMCoreUI/FormUIHelpers/New/Rules/RuleEqualsModel.swift index 7d1532d8..fd7331f0 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/RuleEqualsModel.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/RuleEqualsModel.swift @@ -11,7 +11,7 @@ import Foundation public class RuleEqualsModel: RulesProtocol { public static var identifier: String = "equals" - public var ruleType: String = RuleEqualsModel.identifier + public var type: String = RuleEqualsModel.identifier public var fields: [String] public func isValid(_ formField: FormFieldProtocol) -> Bool { diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/RuleRegexModel.swift b/MVMCoreUI/FormUIHelpers/New/Rules/RuleRegexModel.swift index 9ef00426..bc66fd1a 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/RuleRegexModel.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/RuleRegexModel.swift @@ -10,7 +10,7 @@ import Foundation public class RuleRegexModel: RulesProtocol { public static var identifier: String = "regex" - public var ruleType: String = RuleRegexModel.identifier + public var type: String = RuleRegexModel.identifier public var fields: [String] public func isValid(_ formField: FormFieldProtocol) -> Bool { diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/RuleRequiredModel.swift b/MVMCoreUI/FormUIHelpers/New/Rules/RuleRequiredModel.swift index 252808a0..3900249a 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/RuleRequiredModel.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/RuleRequiredModel.swift @@ -12,7 +12,7 @@ import Foundation public class RuleRequiredModel: RulesProtocol { public static var identifier: String = "required" - public var ruleType: String = RuleRequiredModel.identifier + public var type: String = RuleRequiredModel.identifier public var fields: [String] public func isValid(_ formField: FormFieldProtocol) -> Bool { diff --git a/MVMCoreUI/FormUIHelpers/New/Rules/RulesProtocol.swift b/MVMCoreUI/FormUIHelpers/New/Rules/RulesProtocol.swift index 536bf468..24bedd01 100644 --- a/MVMCoreUI/FormUIHelpers/New/Rules/RulesProtocol.swift +++ b/MVMCoreUI/FormUIHelpers/New/Rules/RulesProtocol.swift @@ -14,7 +14,7 @@ public enum RulesCodingKey: String, CodingKey { } public protocol RulesProtocol: ModelProtocol { - var ruleType: String { get set } + var type: String { get set } var fields: [String] { get set } func isValid(_ formValidator: FormValidator) -> Bool func isValid(_ formField: FormFieldProtocol) -> Bool diff --git a/MVMCoreUI/Molecules/RadioButtonSelectionHelper.swift b/MVMCoreUI/Molecules/RadioButtonSelectionHelper.swift index 6674fe4d..263a85a4 100644 --- a/MVMCoreUI/Molecules/RadioButtonSelectionHelper.swift +++ b/MVMCoreUI/Molecules/RadioButtonSelectionHelper.swift @@ -16,7 +16,7 @@ import UIKit public var groupName: String? = FormValidator.defaultGroupName private var selectedRadioButton: RadioButton? private var fieldGroupName: String? - public var baseValue: JSONValue? + public var baseValue: AnyHashable? init(_ fieldKey: String?) { self.fieldKey = fieldKey @@ -47,7 +47,7 @@ extension RadioButtonSelectionHelper { return selectedRadioButton?.formFieldGroupName() ?? self.fieldGroupName } - public func formFieldValue() -> JSONValue? { + public func formFieldValue() -> AnyHashable? { return selectedRadioButton?.formFieldValue() } }