impreovements

This commit is contained in:
Suresh, Kamlesh 2019-09-11 15:56:24 -04:00
parent fe066f5530
commit eac6bf3aa3
4 changed files with 39 additions and 8 deletions

View File

@ -26,6 +26,7 @@ typedef enum : NSUInteger {
static CGFloat const PrimaryButtonHeight = 42.0;
static CGFloat const PrimaryButtonSmallHeight = 30.0;
@interface PrimaryButton : MFCustomButton <MFTextFieldDelegate, UITextFieldDelegate, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewConstrainingProtocol>
@property (nonatomic, readonly, assign) PrimaryButtonType primaryButtonType; //use reset function to set

View File

@ -13,6 +13,7 @@
#import "MFStyler.h"
#import "UIColor+MFConvenience.h"
#import <MVMCoreUI/MVMCoreUI-Swift.h>
@import MVMCore.MVMCoreDispatchUtility;
@import MVMCore.MVMCoreGetterUtility;
@import MVMCore.NSDictionary_MFConvenience;
@ -667,6 +668,9 @@
}
- (void)setWithJSON:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData {
self.validationRequired = [json boolForKey:@"validationRequired"];
self.requiredFieldsList = [json array:@"requiredFields"];
[FormValidator setupValidationWithMolecule:self delegate:delegateObject.formValidationProtocol];
self.primaryButtonType = PrimaryButtonTypeCustom;
@ -697,8 +701,6 @@
if ((color = [json string:@"disabledBorderColor"])) {
self.disabledBorderColor = [UIColor mfGetColorForHex:color];
}
self.validationRequired = [json boolForKey:@"validationRequired"];
self.requiredFieldsList = [json array:@"requiredFields"];
NSString *size = [json string:@"size"];
if ([size isEqualToString:@"small"]) {
@ -777,6 +779,10 @@
#pragma mark - FormValidationEnableDisableProtocol
- (BOOL) isValidationRequired {
return self.validationRequired;
}
- (NSArray<NSString *> *)requiredFields {
return self.requiredFieldsList;
}

View File

@ -29,6 +29,9 @@ import Foundation
}
@objc public protocol FormValidationEnableDisableProtocol: FormValidationProtocol {
@objc func isValidationRequired() -> Bool
// Based on the isValidField(), the fields which needs to be enabled can call this method
@objc optional func enableField(_ enable: Bool)

View File

@ -13,6 +13,7 @@ import MVMCore
@objcMembers public class FormValidator: NSObject {
var delegate: FormValidationProtocol?
var extraValidationBlock: (() -> Bool)?
var fieldMolecules: [FormValidationFormFieldProtocol] = []
var enableDisableMolecules: [FormValidationEnableDisableProtocol] = []
var radioButtonsModelByGroup: [String: RadioButtonModel] = [:]
@ -21,8 +22,9 @@ import MVMCore
if let molecule = molecule as? FormValidationFormFieldProtocol {
fieldMolecules.append(molecule)
}
if let molecule = molecule as? FormValidationEnableDisableProtocol {
enableDisableMolecules.append(molecule)
if let moleculeT = molecule as? FormValidationEnableDisableProtocol,
moleculeT.isValidationRequired() {
enableDisableMolecules.append(moleculeT)
}
}
@ -45,6 +47,27 @@ import MVMCore
}
public func enableByValidation() {
if enableDisableMolecules.count > 1 {
enableByGroup()
} else {
enableByFieldValidation()
}
}
public func enableByFieldValidation() {
guard let enableMolecule = enableDisableMolecules.first else {
return;
}
var valid = true
for molecule in fieldMolecules {
valid = valid && molecule.isValidField()
}
let enableField = valid && (extraValidationBlock?() ?? true)
enableMolecule.enableField?(enableField)
}
func enableByGroup() {
var groupValue: [String: Bool] = [:]
for molecule in fieldMolecules {
let valid = molecule.isValidField()
@ -52,10 +75,7 @@ import MVMCore
groupValue[grouName] = valid && (groupValue[grouName] ?? true)
}
}
shouldEnable(groupValue)
}
public func shouldEnable(_ groupValue: [String: Bool]) {
for molecule in enableDisableMolecules {
var valid = false
for groupName in molecule.requiredFields?() ?? [] {
@ -64,6 +84,7 @@ import MVMCore
molecule.enableField?(valid)
}
}
}