From 132b810a664e899be27b60a74edeec699142815f Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Tue, 14 May 2019 21:52:32 -0400 Subject: [PATCH] refactoring --- MVMCoreUI.xcodeproj/project.pbxproj | 8 +- MVMCoreUI/FormUIHelpers/FormValidator.swift | 8 +- MVMCoreUI/Molecules/RadioButton.swift | 89 +++++++++++++------ MVMCoreUI/Molecules/RadioButtonModel.swift | 57 ++++++++++++ .../MVMCoreUIMoleculeMappingObject.m | 2 +- 5 files changed, 128 insertions(+), 36 deletions(-) create mode 100644 MVMCoreUI/Molecules/RadioButtonModel.swift diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index ddf5c036..e2b1e234 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -11,7 +11,7 @@ 0105618D224BBE7700E1557D /* FormValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0105618A224BBE7700E1557D /* FormValidator.swift */; }; 0105618E224BBE7700E1557D /* FormValidator+TextFields.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0105618B224BBE7700E1557D /* FormValidator+TextFields.swift */; }; 0105618F224BBE7700E1557D /* FormValidator+FormParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0105618C224BBE7700E1557D /* FormValidator+FormParams.swift */; }; - 01157B94225D376D00F15D92 /* RadioButtonList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01157B93225D376D00F15D92 /* RadioButtonList.swift */; }; + 0116A4E5228B19640094F3ED /* RadioButtonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0116A4E4228B19640094F3ED /* RadioButtonModel.swift */; }; 0198F79F225679880066C936 /* FormValidationProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0198F79E225679870066C936 /* FormValidationProtocol.swift */; }; 0198F7A62256A80B0066C936 /* MFRadioButton.h in Headers */ = {isa = PBXBuildFile; fileRef = 0198F7A02256A80A0066C936 /* MFRadioButton.h */; settings = {ATTRIBUTES = (Public, ); }; }; 0198F7A82256A80B0066C936 /* MFRadioButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 0198F7A22256A80A0066C936 /* MFRadioButton.m */; }; @@ -173,7 +173,7 @@ 0105618A224BBE7700E1557D /* FormValidator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormValidator.swift; sourceTree = ""; }; 0105618B224BBE7700E1557D /* FormValidator+TextFields.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FormValidator+TextFields.swift"; sourceTree = ""; }; 0105618C224BBE7700E1557D /* FormValidator+FormParams.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FormValidator+FormParams.swift"; sourceTree = ""; }; - 01157B93225D376D00F15D92 /* RadioButtonList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioButtonList.swift; sourceTree = ""; }; + 0116A4E4228B19640094F3ED /* RadioButtonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioButtonModel.swift; sourceTree = ""; }; 0198F79E225679870066C936 /* FormValidationProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormValidationProtocol.swift; sourceTree = ""; }; 0198F7A02256A80A0066C936 /* MFRadioButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MFRadioButton.h; sourceTree = ""; }; 0198F7A22256A80A0066C936 /* MFRadioButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MFRadioButton.m; sourceTree = ""; }; @@ -451,9 +451,9 @@ D2A5145C2211D22A00345BFB /* MVMCoreUIMoleculeViewProtocol.h */, D2A5145E2211DDC100345BFB /* MoleculeStackView.swift */, D274CA322236A78900B01B62 /* StandardFooterView.swift */, - 01157B93225D376D00F15D92 /* RadioButtonList.swift */, 01004F2F22721C3800991ECC /* RadioButton.swift */, D2E1FADC2268B25E00AEFD8C /* MoleculeTableViewCell.swift */, + 0116A4E4228B19640094F3ED /* RadioButtonModel.swift */, ); path = Molecules; sourceTree = ""; @@ -879,6 +879,7 @@ D29770F221F7C6D600B2F0D0 /* TopLabelsAndBottomButtonsTableViewController.m in Sources */, DBC4391922442197001AB423 /* DashLine.swift in Sources */, D29DF29621E7ADB8003B2FB9 /* StackableViewController.m in Sources */, + 0116A4E5228B19640094F3ED /* RadioButtonModel.swift in Sources */, D2E1FADB2260D3D200AEFD8C /* MVMCoreUIDelegateObject.swift in Sources */, D22D1F1F220343560077CEC0 /* MVMCoreUICheckMarkView.m in Sources */, 01004F3022721C3800991ECC /* RadioButton.swift in Sources */, @@ -912,7 +913,6 @@ 0105618F224BBE7700E1557D /* FormValidator+FormParams.swift in Sources */, D2E1FADD2268B25E00AEFD8C /* MoleculeTableViewCell.swift in Sources */, D29DF2AE21E7B3A4003B2FB9 /* MFTextView.m in Sources */, - 01157B94225D376D00F15D92 /* RadioButtonList.swift in Sources */, D29DF18121E69E50003B2FB9 /* MFView.m in Sources */, D29DF18321E69E54003B2FB9 /* SeparatorView.m in Sources */, D29DF17A21E69E1F003B2FB9 /* MFCustomButton.m in Sources */, diff --git a/MVMCoreUI/FormUIHelpers/FormValidator.swift b/MVMCoreUI/FormUIHelpers/FormValidator.swift index 07f4a583..af40b2de 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidator.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidator.swift @@ -12,10 +12,11 @@ import UIKit @objcMembers public class FormValidator: NSObject { var delegate: FormValidationProtocol? - var molecules: [UIView & FormValidationProtocol] = [] + var molecules: [FormValidationProtocol] = [] var extraValidationBlock: (() -> Bool)? + var radioButtonsModelByGroup: [String: RadioButtonModel] = [:] - public func insertMolecule(_ molecule: UIView & FormValidationProtocol) { + public func insertMolecule(_ molecule: FormValidationProtocol) { molecules.append(molecule) } @@ -30,7 +31,7 @@ import UIKit return delegate.formValidatorModel?() } - public static func setupValidation(molecule: UIView & FormValidationProtocol, delegate: FormValidationProtocol?) { + public static func setupValidation(molecule: FormValidationProtocol, delegate: FormValidationProtocol?) { if let validator = delegate?.formValidatorModel?() { validator.delegate = delegate validator.insertMolecule(molecule) @@ -52,3 +53,4 @@ import UIKit } } } + diff --git a/MVMCoreUI/Molecules/RadioButton.swift b/MVMCoreUI/Molecules/RadioButton.swift index f55ef302..c814de13 100644 --- a/MVMCoreUI/Molecules/RadioButton.swift +++ b/MVMCoreUI/Molecules/RadioButton.swift @@ -12,10 +12,16 @@ import UIKit @objcMembers open class RadioButton: ViewConstrainingView { let radioButton = MFRadioButton() - let label = Label() - var target: RadioButtonListProtocol? + var delegateObject:MVMCoreUIDelegateObject? var dummyButton: MFCustomButton? - + let label = Label() + + var groupName: String? + var fieldKey: String? + var formValue: Bool? + var isRequired: Bool = false + var radioButtonModel: RadioButtonModel? + // MARK: - Inits public init() { super.init(frame: .zero) @@ -29,11 +35,6 @@ import UIKit super.init(coder: aDecoder) } - public init(target: RadioButtonListProtocol) { - super.init(frame: .zero) - self.target = target - } - open override func setupView() { super.setupView() guard subviews.count == 0 else { @@ -43,18 +44,26 @@ import UIKit translatesAutoresizingMaskIntoConstraints = false radioButton.translatesAutoresizingMaskIntoConstraints = false addSubview(radioButton) - addSubview(label) radioButton.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true radioButton.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: PaddingOne).isActive = true bottomAnchor.constraint(greaterThanOrEqualTo: radioButton.bottomAnchor, constant: PaddingOne).isActive = true radioButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true - - label.leftAnchor.constraint(equalTo: radioButton.rightAnchor, constant: PaddingTwo).isActive = true - label.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true - label.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: PaddingOne).isActive = true - label.bottomAnchor.constraint(greaterThanOrEqualTo: bottomAnchor, constant: PaddingOne).isActive = true - label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true + + if let rightView = createRightView() { + addSubview(rightView) + rightView.leftAnchor.constraint(equalTo: radioButton.rightAnchor, constant: PaddingTwo).isActive = true + rightView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true + rightView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: PaddingOne).isActive = true + rightView.bottomAnchor.constraint(greaterThanOrEqualTo: bottomAnchor, constant: PaddingOne).isActive = true + rightView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true + } + addActionHandler() + } + + func createRightView() -> ViewConstrainingView? { + let rightView = ViewConstrainingView(constrainingView: label) + return rightView } func addActionHandler() { @@ -76,19 +85,12 @@ import UIKit } func tapAction() { - if let target = target { - target.selected?(self) + if let radioButtonModel = radioButtonModel { + radioButtonModel.selected(self) } else { radioButton.isSelected = !radioButton.isSelected } - } - - func getColor( _ json: [AnyHashable: Any], _ key: String) -> UIColor? { - if let colorHex = json.optionalStringForKey(key) { - return UIColor.mfGet(forHex: colorHex) - } else { - return nil - } + FormValidator.enableByValidationWith(delegate: self.delegateObject?.formValidationProtocol) } } @@ -102,11 +104,27 @@ extension RadioButton { guard let jsonDictionary = json else { return } + groupName = jsonDictionary.optionalStringForKey("groupName") + fieldKey = jsonDictionary.optionalStringForKey("fieldKey") + isRequired = jsonDictionary.boolForKey("required") + self.delegateObject = delegateObject as? MVMCoreUIDelegateObject + + radioButtonModel = RadioButtonModel.setupForRadioButtonGroup(radioButton: self, + formValidator: self.delegateObject?.formValidationProtocol?.formValidatorModel?()) + + /* If the radio button has a group, it will have RadioButtonModel. + In this case the RadioButtonModel should be the validator + */ + if let radioButtonModel = radioButtonModel { + FormValidator.setupValidation(molecule: radioButtonModel, delegate: self.delegateObject?.formValidationProtocol) + } else { + FormValidator.setupValidation(molecule: self, delegate: self.delegateObject?.formValidationProtocol) + } + label.setWithJSON(jsonDictionary.optionalDictionaryForKey(KeyLabel), delegateObject: delegateObject, additionalData: additionalData) - addActionHandler() } open override func needsToBeConstrained() -> Bool { @@ -118,15 +136,30 @@ extension RadioButton { } } + // MARK: - FormValidationProtocol extension RadioButton: FormValidationProtocol { + // Used to check the validity of the field, to enable/disable the primary button. + @objc public func isValidField() -> Bool { + if isRequired == false { + return true + } + return radioButtonModel?.isValidField() ?? false + } + // The Field name key value pair for sending to server @objc public func formFieldName() -> String? { + if let radioButtonModel = radioButtonModel { + return radioButtonModel.formFieldName() + } return json?.optionalStringForKey("fieldKey") } - // The Field value key value paid for sending to server + // The Feild value key value paid for sending to server @objc public func formFieldValue() -> Any? { - return json?.optionalStringForKey(KeyValue) + if let radioButtonModel = radioButtonModel { + return radioButtonModel.formFieldValue() + } + return radioButton.isSelected } } diff --git a/MVMCoreUI/Molecules/RadioButtonModel.swift b/MVMCoreUI/Molecules/RadioButtonModel.swift new file mode 100644 index 00000000..fbbb1a60 --- /dev/null +++ b/MVMCoreUI/Molecules/RadioButtonModel.swift @@ -0,0 +1,57 @@ +// +// RadioButtonModel.swift +// MVMCoreUI +// +// Created by Suresh, Kamlesh on 5/14/19. +// Copyright © 2019 Verizon Wireless. All rights reserved. +// + +import Foundation +import UIKit + +@objcMembers public class RadioButtonModel: NSObject { + + private var selectedRadioButton: RadioButton? + private var radioButtons: [RadioButton] = [] + + public static func setupForRadioButtonGroup(radioButton: RadioButton, formValidator: FormValidator?) -> RadioButtonModel? { + + guard let groupName = radioButton.groupName, + let formValidator = formValidator else { + return nil + } + + let radioButtonModel = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonModel() + formValidator.radioButtonsModelByGroup[groupName] = radioButtonModel + radioButtonModel.radioButtons.append(radioButton) + + return radioButtonModel + } + + public func selected(_ radioButton: RadioButton) { + selectedRadioButton?.radioButton.isSelected = false + selectedRadioButton = radioButton + selectedRadioButton?.radioButton.isSelected = true + } +} + +// MARK: - FormValidationProtocol +extension RadioButtonModel: FormValidationProtocol { + // Used to check the validity of the field, to enable/disable the primary button. + @objc public func isValidField() -> Bool { + if selectedRadioButton != nil { + return true + } + return false + } + + // The Field name key value pair for sending to server + @objc public func formFieldName() -> String? { + return selectedRadioButton?.fieldKey + } + + // The Feild value key value paid for sending to server + @objc public func formFieldValue() -> Any? { + return selectedRadioButton != nil ? true : false + } +} diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m index 0a37d62e..d6e7d9f2 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m @@ -33,7 +33,7 @@ @"caretView": CaretView.class, @"caretButton": CaretButton.class, @"textField" : MFTextField.class, - @"radioButton": RadioButtonList.class, + @"radioButton": RadioButton.class, @"checkbox" : MVMCoreUICheckBox.class } mutableCopy]; });