Merge branch 'feature/radio_fixes' into 'develop'

Feature/radio fixes

See merge request BPHV_MIPS/mvm_core_ui!363
This commit is contained in:
Suresh, Kamlesh Jain 2020-04-10 17:32:34 -04:00
commit b981e124e4
9 changed files with 116 additions and 40 deletions

View File

@ -20,6 +20,12 @@ import UIKit
} }
} }
public override var isSelected: Bool {
didSet {
radioModel?.state = isSelected
}
}
public var enabledColor: UIColor = .mvmBlack public var enabledColor: UIColor = .mvmBlack
public var disabledColor: UIColor = .mvmCoolGray3 public var disabledColor: UIColor = .mvmCoolGray3
public var delegateObject: MVMCoreUIDelegateObject? public var delegateObject: MVMCoreUIDelegateObject?

View File

@ -55,7 +55,7 @@ open class RadioButtonModel: MoleculeModelProtocol, FormFieldProtocol {
//-------------------------------------------------- //--------------------------------------------------
public func formFieldValue() -> AnyHashable? { public func formFieldValue() -> AnyHashable? {
return state return fieldValue
} }
//-------------------------------------------------- //--------------------------------------------------

View File

@ -17,15 +17,30 @@ import Foundation
public var fieldKey: String? public var fieldKey: String?
public var groupName: String = FormValidator.defaultGroupName public var groupName: String = FormValidator.defaultGroupName
private var selectedRadioButton: RadioButton? private var selectedRadioButton: RadioButton?
private var fieldGroupName: String? private var selectedRadioButtonModel: RadioButtonModel?
public var baseValue: AnyHashable? public var baseValue: AnyHashable?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializer // MARK: - Initializer
//-------------------------------------------------- //--------------------------------------------------
init(_ fieldKey: String?) { public func set(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton) {
self.fieldKey = fieldKey self.fieldKey = radioButtonModel.fieldKey
self.groupName = radioButtonModel.groupName
if radioButtonModel.state {
if self.baseValue == nil,
let selected = radioButtonModel.baseValue as? Bool, selected {
self.baseValue = radioButtonModel.fieldValue
}
selectedRadioButtonModel = radioButtonModel
// Below code is needed for cell resuse scenario.
radioButton.isSelected = true
selectedRadioButton = radioButton
} else {
radioButton.isSelected = false
}
} }
//-------------------------------------------------- //--------------------------------------------------
@ -35,35 +50,33 @@ import Foundation
public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) { public static func setupForRadioButtonGroup(_ radioButtonModel: RadioButtonModel, _ radioButton: RadioButton, delegateObject: MVMCoreUIDelegateObject?) {
guard let groupName = radioButtonModel.fieldKey, guard let groupName = radioButtonModel.fieldKey,
let formValidator = delegateObject?.formHolderDelegate?.formValidator let formValidator = delegateObject?.formHolderDelegate?.formValidator else {
else { return } return
let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper(radioButtonModel.fieldKey)
radioButtonSelectionHelper.fieldGroupName = radioButtonModel.fieldKey
formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper
if radioButtonModel.state {
radioButtonSelectionHelper.selectedRadioButton = radioButton
} }
let radioButtonSelectionHelper = formValidator.radioButtonsModelByGroup[groupName] ?? RadioButtonSelectionHelper()
radioButtonSelectionHelper.set(radioButtonModel, radioButton)
formValidator.radioButtonsModelByGroup[groupName] = radioButtonSelectionHelper
FormValidator.setupValidation(for: radioButtonSelectionHelper, delegate: delegateObject?.formHolderDelegate) FormValidator.setupValidation(for: radioButtonSelectionHelper, delegate: delegateObject?.formHolderDelegate)
} }
public func selected(_ radioButton: RadioButton) { public func selected(_ radioButton: RadioButton) {
// Checks because the view could be reused
if selectedRadioButton?.radioModel === selectedRadioButtonModel {
selectedRadioButton?.isSelected = false
} else {
selectedRadioButtonModel?.state = false
}
selectedRadioButton?.isSelected = false
selectedRadioButton = radioButton selectedRadioButton = radioButton
selectedRadioButton?.isSelected = true selectedRadioButton?.isSelected = true
selectedRadioButtonModel = selectedRadioButton?.radioModel
} }
} }
// MARK: - FormValidationFormFieldProtocol // MARK: - FormValidationFormFieldProtocol
extension RadioButtonSelectionHelper { extension RadioButtonSelectionHelper {
public func formFieldGroupName() -> String? {
return selectedRadioButton?.formFieldGroupName() ?? fieldGroupName
}
public func formFieldValue() -> AnyHashable? { public func formFieldValue() -> AnyHashable? {
return selectedRadioButton?.formFieldValue() return selectedRadioButtonModel?.fieldValue
} }
} }

View File

@ -74,12 +74,13 @@ import MVMCore
/// Validates all rule groups. Returns if valid /// Validates all rule groups. Returns if valid
public func validate() -> Bool { public func validate() -> Bool {
var valid = true var valid = true
guard let formRules = formRules else { guard let formRules = formRules else { return valid }
return valid
}
for group in formRules { for group in formRules {
valid = valid && validateGroup(group) let groupValid = validateGroup(group)
valid = valid && groupValid
} }
return valid return valid
} }
@ -137,13 +138,16 @@ import MVMCore
// TODO: Temporary hacks, rewrite architecture to support this. // TODO: Temporary hacks, rewrite architecture to support this.
public extension FormValidator { public extension FormValidator {
func getGroupName(forPageType pageType: String?) -> String? { func getGroupName(forPageType pageType: String?) -> String? {
for actionItem in groupWatchers { for actionItem in groupWatchers {
if let buttonModel = actionItem as? ButtonModel, if let buttonModel = actionItem as? ButtonModel,
pageType == (buttonModel.action as? ActionOpenPageModel)?.pageType { pageType == (buttonModel.action as? ActionOpenPageModel)?.pageType {
return buttonModel.groupName return buttonModel.groupName
} }
} }
return nil return nil
} }
} }

View File

@ -11,19 +11,35 @@ import Foundation
open class FormGroupRule: Codable { open class FormGroupRule: Codable {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
var groupName: String var groupName: String
var rules: [RulesProtocol] var rules: [RulesProtocol]
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
init(_ groupName: String, _ rules: [RulesProtocol]) { init(_ groupName: String, _ rules: [RulesProtocol]) {
self.groupName = groupName self.groupName = groupName
self.rules = rules self.rules = rules
} }
//--------------------------------------------------
// MARK: - Keys
//--------------------------------------------------
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case groupName case groupName
case rules case rules
} }
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
self.groupName = try typeContainer.decode(String.self, forKey: .groupName) self.groupName = try typeContainer.decode(String.self, forKey: .groupName)

View File

@ -9,11 +9,18 @@
import Foundation import Foundation
public class RuleAllValueChangedModel: RulesProtocol { public class RuleAllValueChangedModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "allValueChanged" public static var identifier: String = "allValueChanged"
public var type: String = RuleAllValueChangedModel.identifier public var type: String = RuleAllValueChangedModel.identifier
public var fields: [String] public var fields: [String]
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool { public func isValid(_ formField: FormFieldProtocol) -> Bool {
return formField.baseValue != formField.formFieldValue() return formField.baseValue != formField.formFieldValue()
} }

View File

@ -10,31 +10,41 @@ import Foundation
public class RuleEqualsModel: RulesProtocol { public class RuleEqualsModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "equals" public static var identifier: String = "equals"
public var type: String = RuleEqualsModel.identifier public var type: String = RuleEqualsModel.identifier
public var fields: [String] public var fields: [String]
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool { public func isValid(_ formField: FormFieldProtocol) -> Bool {
return false return false
} }
public func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool { public func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
var valid = true var valid = true
var compareValue: AnyHashable? var compareValue: AnyHashable?
for formKey in fields { for formKey in fields {
guard let formField = fieldMolecules[formKey] else { guard let formField = fieldMolecules[formKey] else { continue }
continue
}
if compareValue == nil { if compareValue == nil {
compareValue = formField.formFieldValue() compareValue = formField.formFieldValue()
continue continue
} }
if compareValue != formField.formFieldValue(){
if compareValue != formField.formFieldValue() {
valid = false valid = false
break break
} }
} }
return valid return valid
} }
} }

View File

@ -8,16 +8,27 @@
import Foundation import Foundation
public class RuleRegexModel: RulesProtocol { public class RuleRegexModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "regex" public static var identifier: String = "regex"
public var type: String = RuleRegexModel.identifier public var type: String = RuleRegexModel.identifier
public var fields: [String] public var fields: [String]
public var regex: String public var regex: String
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool { public func isValid(_ formField: FormFieldProtocol) -> Bool {
if let stringToValidate = formField.formFieldValue() as? String { if let stringToValidate = formField.formFieldValue() as? String {
return MVMCoreUIUtility.validate(stringToValidate, withRegularExpression: regex) return MVMCoreUIUtility.validate(stringToValidate, withRegularExpression: regex)
} }
return false return false
} }
} }

View File

@ -10,21 +10,30 @@ import Foundation
public class RuleRequiredModel: RulesProtocol { public class RuleRequiredModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "allRequired" public static var identifier: String = "allRequired"
public var type: String = RuleRequiredModel.identifier public var type: String = RuleRequiredModel.identifier
public var fields: [String] public var fields: [String]
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool { public func isValid(_ formField: FormFieldProtocol) -> Bool {
guard let value = formField.formFieldValue() else { guard let value = formField.formFieldValue() else { return false }
return false
}
var valid = true var valid = true
if let valueString = value as? String { if let valueString = value as? String {
valid = valueString.count > 0 valid = valueString.count > 0
} else if let valueBool = value as? Bool { } else if let valueBool = value as? Bool {
valid = valueBool valid = valueBool
} }
return valid return valid
} }
} }