refactoring

This commit is contained in:
Suresh, Kamlesh 2019-05-14 21:52:32 -04:00
parent ee09cfa278
commit 132b810a66
5 changed files with 128 additions and 36 deletions

View File

@ -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 = "<group>"; };
0105618B224BBE7700E1557D /* FormValidator+TextFields.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FormValidator+TextFields.swift"; sourceTree = "<group>"; };
0105618C224BBE7700E1557D /* FormValidator+FormParams.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "FormValidator+FormParams.swift"; sourceTree = "<group>"; };
01157B93225D376D00F15D92 /* RadioButtonList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioButtonList.swift; sourceTree = "<group>"; };
0116A4E4228B19640094F3ED /* RadioButtonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioButtonModel.swift; sourceTree = "<group>"; };
0198F79E225679870066C936 /* FormValidationProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormValidationProtocol.swift; sourceTree = "<group>"; };
0198F7A02256A80A0066C936 /* MFRadioButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MFRadioButton.h; sourceTree = "<group>"; };
0198F7A22256A80A0066C936 /* MFRadioButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MFRadioButton.m; sourceTree = "<group>"; };
@ -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 = "<group>";
@ -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 */,

View File

@ -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
}
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -33,7 +33,7 @@
@"caretView": CaretView.class,
@"caretButton": CaretButton.class,
@"textField" : MFTextField.class,
@"radioButton": RadioButtonList.class,
@"radioButton": RadioButton.class,
@"checkbox" : MVMCoreUICheckBox.class
} mutableCopy];
});