From 2b4d8b95c961fb5dee5bd5575454c23ab920c246 Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Wed, 3 Apr 2019 14:46:34 -0400 Subject: [PATCH] checkbox --- MVMCoreUI.xcodeproj/project.pbxproj | 4 ++ MVMCoreUI/Atoms/Views/CheckBoxWithLabel.swift | 31 ++++++++++++ MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.h | 3 ++ MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m | 47 ++++++++++++++++++- MVMCoreUI/Atoms/Views/ViewConstrainingView.h | 3 ++ MVMCoreUI/Atoms/Views/ViewConstrainingView.m | 28 ++++++----- .../MVMCoreUIMoleculeMappingObject.m | 3 +- .../Protocols/FormValidationProtocol.swift | 2 +- 8 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 MVMCoreUI/Atoms/Views/CheckBoxWithLabel.swift diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index 5df32702..201e8d9a 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 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 */; }; 01056191224BBE8000E1557D /* FormValidationProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01056190224BBE7F00E1557D /* FormValidationProtocol.swift */; }; + 013DAB042254FCE00092ABB7 /* CheckBoxWithLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 013DAB032254FCE00092ABB7 /* CheckBoxWithLabel.swift */; }; 01DF55E021F8FAA800CC099B /* MFTextFieldListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DF55DF21F8FAA800CC099B /* MFTextFieldListView.swift */; }; 01DF567021FA5AB300CC099B /* TextFieldListFormViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DF566F21FA5AB300CC099B /* TextFieldListFormViewController.swift */; }; 01E569D3223FFFA500327251 /* ThreeLayerViewController.swift in Headers */ = {isa = PBXBuildFile; fileRef = D2A5146A2214905000345BFB /* ThreeLayerViewController.swift */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -167,6 +168,7 @@ 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 = ""; }; 01056190224BBE7F00E1557D /* FormValidationProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormValidationProtocol.swift; sourceTree = ""; }; + 013DAB032254FCE00092ABB7 /* CheckBoxWithLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckBoxWithLabel.swift; sourceTree = ""; }; 01DF55DF21F8FAA800CC099B /* MFTextFieldListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MFTextFieldListView.swift; sourceTree = ""; }; 01DF566F21FA5AB300CC099B /* TextFieldListFormViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextFieldListFormViewController.swift; sourceTree = ""; }; D206997521FB8A0B00CAE0DE /* MVMCoreUINavigationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreUINavigationController.h; sourceTree = ""; }; @@ -595,6 +597,7 @@ D22D1F19220341F50077CEC0 /* MVMCoreUICheckBox.m */, D22D1F44220496A30077CEC0 /* MVMCoreUISwitch.h */, D22D1F45220496A30077CEC0 /* MVMCoreUISwitch.m */, + 013DAB032254FCE00092ABB7 /* CheckBoxWithLabel.swift */, ); path = Views; sourceTree = ""; @@ -870,6 +873,7 @@ D29770F221F7C6D600B2F0D0 /* TopLabelsAndBottomButtonsTableViewController.m in Sources */, DBC4391922442197001AB423 /* DashLine.swift in Sources */, D29DF29621E7ADB8003B2FB9 /* StackableViewController.m in Sources */, + 013DAB042254FCE00092ABB7 /* CheckBoxWithLabel.swift in Sources */, D22D1F1F220343560077CEC0 /* MVMCoreUICheckMarkView.m in Sources */, D282AAB4223FDDAE00C46919 /* MFLoadImageView.swift in Sources */, D29DF11721E6805F003B2FB9 /* UIColor+MFConvenience.m in Sources */, diff --git a/MVMCoreUI/Atoms/Views/CheckBoxWithLabel.swift b/MVMCoreUI/Atoms/Views/CheckBoxWithLabel.swift new file mode 100644 index 00000000..533ee060 --- /dev/null +++ b/MVMCoreUI/Atoms/Views/CheckBoxWithLabel.swift @@ -0,0 +1,31 @@ +// +// ChecckBoxWithLabel.swift +// MVMCoreUI +// +// Created by Suresh, Kamlesh on 4/3/19. +// Copyright © 2019 Verizon Wireless. All rights reserved. +// + +import UIKit + +@objcMembers public class CheckBoxWithLabel: ViewConstrainingView { + let mvmCheckBox = MVMCoreUICheckBox(frame: .zero) + + public init() { + super.init(frame: .zero) + } + + public required init?(coder decoder: NSCoder) { + super.init(coder: decoder) + } + + public override func setupView() { + super.setupView() + addConstrainedView(mvmCheckBox) + } + + override open func setWithJSON(_ json: [AnyHashable: Any]?, delegate: NSObject?, additionalData: [AnyHashable: Any]?) { + super.setWithJSON(json, delegate: delegate, additionalData: additionalData) + mvmCheckBox.setWithJSON(json, delegate: delegate, additionalData: additionalData) + } +} diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.h b/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.h index 853293c7..32fb1e02 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.h +++ b/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.h @@ -59,4 +59,7 @@ - (void)setValueForAccessibilityText:(nullable NSString *)text; - (void)hideDescriptionLabelAndPinCheckboxToRight; +// Molecule Design +- (void)setWithJSON:(nullable NSDictionary *)json delegate:(nullable NSObject *)delegate additionalData:(nullable NSDictionary *)additionalData; + @end diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m b/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m index c67c628f..f00d9101 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m +++ b/MVMCoreUI/Atoms/Views/MVMCoreUICheckBox.m @@ -16,10 +16,13 @@ #import "UIColor+MFConvenience.h" #import "MVMCoreUIUtility.h" #import "MFStyler.h" +#import + 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; @@ -38,12 +41,32 @@ 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, weak) id delegate; + @end @implementation MVMCoreUICheckBox #pragma mark - convenient class methods +- (void)setWithJSON:(nullable NSDictionary *)json delegate:(nullable NSObject *)delegate additionalData:(nullable NSDictionary *)additionalData { + [FormValidator setupValidationWithMolecule:self delegate:(id)delegate]; + self.delegate = (id)delegate; + self.fieldKey = [json stringForKey:@"fieldKey"]; + self.isRequired = [json boolForKey:@"required"]; + + 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]; + NSString *label = [json string:@"label"]; + + [self setupWithCheckedColor:checkedColor unCheckColor:unCheckedColor text:label]; +} + + (instancetype)mfCheckBox { MVMCoreUICheckBox *checkBox = [[self alloc] initWithFrame:CGRectZero]; checkBox.translatesAutoresizingMaskIntoConstraints = NO; @@ -68,6 +91,23 @@ 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)initWithCheckedColor:(UIColor *)checkedColor unCheckColor:(UIColor *)unCheckedColor text:(NSString *)text { @@ -253,7 +293,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; @@ -271,6 +311,9 @@ static const CGFloat CheckBoxHeightWidth = 18.0; } completion:nil]; [self.checkMark updateCheckSelected:NO animated:animated]; } + + FormValidator *formValidator = [FormValidator getFormValidatorForDelegate:self.delegate]; + [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 bb4740a7..939c3b6f 100644 --- a/MVMCoreUI/Atoms/Views/ViewConstrainingView.h +++ b/MVMCoreUI/Atoms/Views/ViewConstrainingView.h @@ -47,4 +47,7 @@ // For setting up the view. - (void)setupView; +// Add a View Constraining View +- (void)addConstrainedView:(nonnull UIView *)view; + @end diff --git a/MVMCoreUI/Atoms/Views/ViewConstrainingView.m b/MVMCoreUI/Atoms/Views/ViewConstrainingView.m index a88b61c3..1ef339fb 100644 --- a/MVMCoreUI/Atoms/Views/ViewConstrainingView.m +++ b/MVMCoreUI/Atoms/Views/ViewConstrainingView.m @@ -28,28 +28,30 @@ ViewConstrainingView *constrainingView = [[ViewConstrainingView alloc] initWithFrame:CGRectZero]; constrainingView.translatesAutoresizingMaskIntoConstraints = NO; constrainingView.backgroundColor = [UIColor clearColor]; - + [constrainingView addConstrainedView:view]; + return constrainingView; +} + +- (void)addConstrainedView:(nonnull UIView *)view { view.translatesAutoresizingMaskIntoConstraints = NO; - [constrainingView addSubview:view]; - constrainingView.constrainedView = view; + [self addSubview:view]; + self.constrainedView = view; - NSLayoutConstraint *leftPin = [view.leftAnchor constraintEqualToAnchor:constrainingView.leftAnchor]; - constrainingView.leftPin = leftPin; + NSLayoutConstraint *leftPin = [view.leftAnchor constraintEqualToAnchor:self.leftAnchor]; + self.leftPin = leftPin; leftPin.active = YES; - NSLayoutConstraint *topPin = [view.topAnchor constraintEqualToAnchor:constrainingView.topAnchor]; - constrainingView.topPin = topPin; + NSLayoutConstraint *topPin = [view.topAnchor constraintEqualToAnchor:self.topAnchor]; + self.topPin = topPin; topPin.active = YES; - NSLayoutConstraint *bottomPin = [constrainingView.bottomAnchor constraintEqualToAnchor:view.bottomAnchor]; - constrainingView.bottomPin = bottomPin; + NSLayoutConstraint *bottomPin = [self.bottomAnchor constraintEqualToAnchor:view.bottomAnchor]; + self.bottomPin = bottomPin; bottomPin.active = YES; - NSLayoutConstraint *rightPin = [constrainingView.rightAnchor constraintEqualToAnchor:view.rightAnchor]; - constrainingView.rightPin = rightPin; + NSLayoutConstraint *rightPin = [self.rightAnchor constraintEqualToAnchor:view.rightAnchor]; + self.rightPin = rightPin; rightPin.active = YES; - - return constrainingView; } - (void)pinToSuperView { diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m index 0936f9df..4eb66001 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" : CheckBoxWithLabel.class } mutableCopy]; }); return mapping; diff --git a/MVMCoreUI/Protocols/FormValidationProtocol.swift b/MVMCoreUI/Protocols/FormValidationProtocol.swift index bfdbb0fd..5336af24 100644 --- a/MVMCoreUI/Protocols/FormValidationProtocol.swift +++ b/MVMCoreUI/Protocols/FormValidationProtocol.swift @@ -15,5 +15,5 @@ import Foundation @objc optional func enableField(_ enable: Bool) @objc optional func formFieldName() -> String? - @objc optional func formFieldValue() -> String? + @objc optional func formFieldValue() -> Any? }