diff --git a/MVMCoreUI/Atoms/TextFields/MFTextField.m b/MVMCoreUI/Atoms/TextFields/MFTextField.m index be25893e..33224f8a 100644 --- a/MVMCoreUI/Atoms/TextFields/MFTextField.m +++ b/MVMCoreUI/Atoms/TextFields/MFTextField.m @@ -324,7 +324,7 @@ } // key used to send text value to server - string = [map string:@"fieldKey"]; + string = [map string:KeyFieldKey]; if (string.length > 0) { self.fieldKey = string; } diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m b/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m index e68fe8f3..f7239def 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m +++ b/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m @@ -20,7 +20,8 @@ static const CGFloat FaultTolerance = 20.f; static const CGFloat CheckBoxHeightWidth = 18.0; -@interface MVMCoreUICheckBox () + +@interface MVMCoreUICheckBox () @property (nonatomic, readwrite) BOOL isSelected; @property (weak, nonatomic) UIView *checkedSquare; @@ -39,10 +40,45 @@ static const CGFloat CheckBoxHeightWidth = 18.0; @property (nullable, strong, nonatomic) NSLayoutConstraint *checkboxWidth; @property (nullable, strong, nonatomic) NSLayoutConstraint *checkboxHeight; +@property (nonatomic) BOOL isRequired; +@property (nullable, strong, nonatomic) NSString *fieldKey; +@property (nullable, strong, nonatomic) DelegateObject *delegate; + @end @implementation MVMCoreUICheckBox +#pragma mark - MVMCoreUIMoleculeViewProtocol + +- (BOOL)needsToBeConstrained { + return YES; +} + +- (UIStackViewAlignment)moleculeAlignment { + return UIStackViewAlignmentLeading; +} + +- (void)setWithJSON:(NSDictionary *)json delegateObject:(DelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData { + + [FormValidator setupValidationWithMolecule:self delegate:((MVMCoreUIDelegateObject *)delegateObject).formValidationProtocol]; + self.delegate = delegateObject; + self.fieldKey = [json stringForKey:KeyFieldKey]; + self.isRequired = [json boolForKey:KeyRequired]; + + NSString *checkedColorHex = [json string:@"checkedColor"]; + NSString *unCheckedColorHex = [json string:@"unCheckedColor"]; + + UIColor *checkedColor = checkedColorHex ? [UIColor mfGetColorForHex:checkedColorHex]: [UIColor blackColor]; + UIColor *unCheckedColor = unCheckedColorHex ? [UIColor mfGetColorForHex:unCheckedColorHex]: [UIColor clearColor]; + + [self setupWithCheckedColor:checkedColor + unCheckColor:unCheckedColor + label:[json dict:KeyLabel] + delegateObject:delegateObject + additionalData: additionalData]; +} + + #pragma mark - convenient class methods + (instancetype)mfCheckBox { @@ -69,8 +105,33 @@ static const CGFloat CheckBoxHeightWidth = 18.0; return checkBox; } +#pragma mark - FormValidationProtocol + +- (BOOL)isValidField { + if (self.isRequired) { + return self.isSelected; + } + return true; +} + +- (nullable NSString *)formFieldName { + return self.fieldKey; +} + +- (nullable id)formFieldValue { + return @(self.isSelected); +} + #pragma mark - inits +- (instancetype)init { + self = [super init]; + if (self) { + [self setupView]; + } + return self; +} + - (instancetype)initWithCheckedColor:(UIColor *)checkedColor unCheckColor:(UIColor *)unCheckedColor text:(NSString *)text { if (self = [super init]) { [self setupWithCheckedColor:checkedColor unCheckColor:unCheckedColor text:text]; @@ -207,17 +268,25 @@ static const CGFloat CheckBoxHeightWidth = 18.0; } } +- (void)setupWithCheckedColor:(UIColor *)checkedColor unCheckColor:(UIColor *)unCheckedColor { + if (checkedColor) { + self.checkedColor = checkedColor; + } + if (unCheckedColor) { + self.unCheckedColor = unCheckedColor; + } +} + - (void)setupWithCheckedColor:(UIColor *)checkedColor unCheckColor:(UIColor *)unCheckedColor text:(NSString *)text { - [self setupView]; - if (checkedColor) { - self.checkedColor = checkedColor; - } - if (unCheckedColor) { - self.unCheckedColor = unCheckedColor; - } + [self setupWithCheckedColor:checkedColor unCheckColor:unCheckedColor]; [self setDescriptionText:text]; } +- (void)setupWithCheckedColor:(UIColor *)checkedColor unCheckColor:(UIColor *)unCheckedColor label:(NSDictionary *)labelJson delegateObject:(DelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData { + [self setupWithCheckedColor:checkedColor unCheckColor:unCheckedColor]; + [self.descriptionLabel setWithJSON:labelJson delegateObject:delegateObject additionalData:additionalData]; +} + - (void)updateView:(CGFloat)size { [MVMCoreDispatchUtility performBlockOnMainThread:^{ [self.descriptionLabel updateView:size]; @@ -254,7 +323,7 @@ static const CGFloat CheckBoxHeightWidth = 18.0; [self setSelected:selected animated:animated runBlock:YES]; } -- (void)setSelected:(BOOL)selected animated:(BOOL)animated runBlock:(BOOL)runBlock{ +- (void)setSelected:(BOOL)selected animated:(BOOL)animated runBlock:(BOOL)runBlock { [self addAccessibilityLabel:selected]; self.isSelected = selected; @@ -272,6 +341,9 @@ static const CGFloat CheckBoxHeightWidth = 18.0; } completion:nil]; [self.checkMark updateCheckSelected:NO animated:animated]; } + + FormValidator *formValidator = ((MVMCoreUIDelegateObject *)self.delegate).formValidationProtocol.formValidatorModel; + [formValidator enableByValidation]; } - (void)setColor:(nullable UIColor *)color forState:(UIControlState)state { diff --git a/MVMCoreUI/Atoms/Views/ViewConstrainingView.h b/MVMCoreUI/Atoms/Views/ViewConstrainingView.h index 528a79e6..6b97f891 100644 --- a/MVMCoreUI/Atoms/Views/ViewConstrainingView.h +++ b/MVMCoreUI/Atoms/Views/ViewConstrainingView.h @@ -44,13 +44,13 @@ // Pins all edges to its super. 0 constant - (void)pinToSuperView; -// Add a view to be constrained in this view. -- (void)addConstrainedView:(nonnull UIView *)view; - // Resets all the constraints to default. - (void)resetConstraints; // For setting up the view. - (void)setupView; +// Add a view to be constrained in this view. +- (void)addConstrainedView:(nonnull UIView *)view; + @end diff --git a/MVMCoreUI/Atoms/Views/ViewConstrainingView.m b/MVMCoreUI/Atoms/Views/ViewConstrainingView.m index 861db8cd..c1e3e5ff 100644 --- a/MVMCoreUI/Atoms/Views/ViewConstrainingView.m +++ b/MVMCoreUI/Atoms/Views/ViewConstrainingView.m @@ -36,7 +36,7 @@ view.backgroundColor = [UIColor clearColor]; return view; } - + + (nonnull ViewConstrainingView *)viewConstrainingView:(UIView *)view { ViewConstrainingView *constrainingView = [[ViewConstrainingView alloc] initWithFrame:CGRectZero]; constrainingView.translatesAutoresizingMaskIntoConstraints = NO; @@ -52,7 +52,7 @@ self.bottomPin = dictionary[ConstraintBot]; self.rightPin = dictionary[ConstraintTrailing]; } - + - (void)setPinConstantsWithInsets:(UIEdgeInsets)insets { [self setTopPinConstant:insets.top left:insets.left bottom:insets.bottom right:insets.right]; } diff --git a/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift b/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift index afce4d28..b593a503 100644 --- a/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift +++ b/MVMCoreUI/FormUIHelpers/FormValidationProtocol.swift @@ -22,6 +22,6 @@ import Foundation // The Field name key value pair for sending to server @objc optional func formFieldName() -> String? - // The Feild value key value paid for sending to server - @objc optional func formFieldValue() -> String? + // The Field value key value pair for sending to server + @objc optional func formFieldValue() -> Any? } diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m index 66039753..e1101191 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m @@ -32,7 +32,8 @@ @"standardFooter": StandardFooterView.class, @"caretView": CaretView.class, @"caretButton": CaretButton.class, - @"textField" : MFTextField.class + @"textField" : MFTextField.class, + @"checkbox" : MVMCoreUICheckBox.class } mutableCopy]; }); return mapping; diff --git a/MVMCoreUI/Utility/MVMCoreUIConstants.h b/MVMCoreUI/Utility/MVMCoreUIConstants.h index a1d2759a..189c2c49 100644 --- a/MVMCoreUI/Utility/MVMCoreUIConstants.h +++ b/MVMCoreUI/Utility/MVMCoreUIConstants.h @@ -38,6 +38,9 @@ extern NSString * const KeyTextColor; extern NSString * const KeyIsHidden; extern NSString * const KeyIsOpaque; +extern NSString * const KeyFieldKey; +extern NSString * const KeyRequired; + #pragma mark - Values extern NSString * const StringY; diff --git a/MVMCoreUI/Utility/MVMCoreUIConstants.m b/MVMCoreUI/Utility/MVMCoreUIConstants.m index 76949ccf..9735675e 100644 --- a/MVMCoreUI/Utility/MVMCoreUIConstants.m +++ b/MVMCoreUI/Utility/MVMCoreUIConstants.m @@ -21,6 +21,8 @@ NSString * const KeyValue = @"value"; NSString * const KeyLabel = @"label"; NSString * const KeyDisable = @"disable"; NSString * const KeyFieldName = @"fieldName"; +NSString * const KeyFieldKey = @"fieldKey"; +NSString * const KeyRequired = @"required"; NSString * const KeyHideMainMenu = @"hideMainMenu"; NSString * const KeyProgressPercent = @"progressPercent"; @@ -37,6 +39,7 @@ NSString * const KeyTextColor = @"textColor"; NSString * const KeyIsHidden = @"isHidden"; NSString * const KeyIsOpaque = @"isOpaque"; + #pragma mark - Values NSString * const StringY = @"Y";