From 4bc22dbb8ecd552b7afbe920c5f243167e3155a2 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 26 Aug 2019 12:35:04 -0400 Subject: [PATCH] switches --- MVMCoreUI.xcodeproj/project.pbxproj | 10 ++- MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h | 3 +- MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m | 74 ++++++++++++++++++- .../SwitchLineItem.swift | 0 .../MVMCoreUIMoleculeMappingObject.m | 2 +- 5 files changed, 83 insertions(+), 6 deletions(-) rename MVMCoreUI/Molecules/{ => SwitchMolecules}/SwitchLineItem.swift (100%) diff --git a/MVMCoreUI.xcodeproj/project.pbxproj b/MVMCoreUI.xcodeproj/project.pbxproj index 22b92460..7a6d0181 100644 --- a/MVMCoreUI.xcodeproj/project.pbxproj +++ b/MVMCoreUI.xcodeproj/project.pbxproj @@ -391,6 +391,14 @@ path = FormUIHelpers; sourceTree = ""; }; + D224798823142BF2003FCCF9 /* SwitchMolecules */ = { + isa = PBXGroup; + children = ( + 016A1070228122180009D605 /* SwitchLineItem.swift */, + ); + path = SwitchMolecules; + sourceTree = ""; + }; D22D1F582204D2590077CEC0 /* LegacyControllers */ = { isa = PBXGroup; children = ( @@ -478,12 +486,12 @@ D29DF10E21E67A77003B2FB9 /* Molecules */ = { isa = PBXGroup; children = ( + D224798823142BF2003FCCF9 /* SwitchMolecules */, 0A12149F22C11A17007C7030 /* ActionDetailWithImage.swift */, 01DF55DF21F8FAA800CC099B /* MFTextFieldListView.swift */, D29770C721F7C4AE00B2F0D0 /* TopLabelsView.h */, D29770C621F7C4AE00B2F0D0 /* TopLabelsView.m */, D282AACA2243C61700C46919 /* ButtonView.swift */, - 016A1070228122180009D605 /* SwitchLineItem.swift */, 01CA51B4229716F60071A6EE /* Switch.swift */, D20A9A5D2243D3E300ADE781 /* TwoButtonView.swift */, D2A514662213885800345BFB /* StandardHeaderView.swift */, diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h index 5591f595..ba36419e 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h +++ b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.h @@ -8,10 +8,11 @@ #import +#import @import MVMCore.MVMCoreViewProtocol; typedef void(^ValueChangeBlock)(void); -@interface MVMCoreUISwitch : UIControl +@interface MVMCoreUISwitch : UIControl @property (assign, nonatomic, getter=isOn) BOOL on; @property (nullable, strong, nonatomic) UIColor *onTintColor; diff --git a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m index 9b9e0b96..95264f3c 100644 --- a/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m +++ b/MVMCoreUI/Atoms/Views/MVMCoreUISwitch.m @@ -14,6 +14,7 @@ #import #import #import +#import const CGFloat SwitchWidth = 42; const CGFloat SwitchHeight = 22; @@ -21,7 +22,7 @@ const CGFloat SwitchKnobWidth = 20; const CGFloat SwitchKnobHeight = 20; const CGFloat SwitchShakeIntensity = 2; -@interface MVMCoreUISwitch () +@interface MVMCoreUISwitch () @property (weak, nonatomic) UIView *baseView; @property (weak, nonatomic) UIView *knobView; @@ -37,6 +38,9 @@ const CGFloat SwitchShakeIntensity = 2; @property (nonatomic) BOOL canChangeValue; +@property (strong, nonatomic, nullable) NSDictionary *json; +@property (nullable, strong, nonatomic) DelegateObject *delegate; + @end @implementation MVMCoreUISwitch @@ -137,6 +141,50 @@ const CGFloat SwitchShakeIntensity = 2; } } +#pragma mark - MVMCoreUIMoleculeViewProtocol + +- (void)setWithJSON:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject additionalData:(NSDictionary *)additionalData { + self.json = json; + + [FormValidator setupValidationWithMolecule:self delegate:delegateObject.formValidationProtocol]; + + NSString *color = [json string:@"onTintColor"]; + if (color) { + self.onTintColor = [UIColor mfGetColorForHex:color]; + } + + color = [json string:@"offTintColor"]; + if (color) { + self.offTintColor = [UIColor mfGetColorForHex:color]; + } + + color = [json string:@"onKnobTintColor"]; + if (color) { + self.onKnobTintColor = [UIColor mfGetColorForHex:color]; + } + + color = [json string:@"offKnobTintColor"]; + if (color) { + self.offKnobTintColor = [UIColor mfGetColorForHex:color]; + } + + [self setState:[json boolForKey:@"state"] animated:false]; +} + ++ (CGFloat)estimatedHeightForRow:(NSDictionary *)json delegateObject:(MVMCoreUIDelegateObject *)delegateObject { + return [self getSwitchHeight]; +} + +#pragma mark - MVMCoreUIMoleculeViewProtocol + +- (BOOL)needsToBeConstrained { + return YES; +} + +- (UIStackViewAlignment)alignment { + return UIStackViewAlignmentTrailing; +} + #pragma mark - UIResponder overide - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { @@ -280,6 +328,11 @@ const CGFloat SwitchShakeIntensity = 2; if (self.valueChangedBlock) { self.valueChangedBlock(); } + + if (self.delegate && [self.delegate respondsToSelector:@selector(formValidationProtocol)] && [[self.delegate performSelector:@selector(formValidationProtocol)] respondsToSelector:@selector(formValidatorModel)]) { + FormValidator *formValidator = [[self.delegate performSelector:@selector(formValidationProtocol)] performSelector:@selector(formValidatorModel)]; + [formValidator enableByValidation]; + } } - (void)setState:(BOOL)state withoutBlockAnimated:(BOOL)animated { @@ -308,7 +361,7 @@ const CGFloat SwitchShakeIntensity = 2; } -- (BOOL)changeValue{ +- (BOOL)changeValue { self.on^=YES; self.shouldTouchToSwitch = NO; [self setState:self.on animated:YES]; @@ -349,7 +402,8 @@ const CGFloat SwitchShakeIntensity = 2; } #pragma mark - Accessibility -- (BOOL)isAccessibilityElement{ + +- (BOOL)isAccessibilityElement { return YES; } @@ -361,4 +415,18 @@ const CGFloat SwitchShakeIntensity = 2; return [MVMCoreUIUtility hardcodedStringWithKey:@"AccToggleHint"]; } +#pragma mark FormValidationProtocol + +- (BOOL)isValidField { + return self.isOn && [self.json boolForKey:@"required"]; +} + +- (NSString *)formFieldName { + return [self.json string:KeyFieldKey]; +} + +- (id)formFieldValue { + return @(self.isOn); +} + @end diff --git a/MVMCoreUI/Molecules/SwitchLineItem.swift b/MVMCoreUI/Molecules/SwitchMolecules/SwitchLineItem.swift similarity index 100% rename from MVMCoreUI/Molecules/SwitchLineItem.swift rename to MVMCoreUI/Molecules/SwitchMolecules/SwitchLineItem.swift diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m index 0dffc356..123940f4 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m @@ -45,7 +45,7 @@ @"checkbox": MVMCoreUICheckBox.class, @"listItem": MoleculeTableViewCell.class, @"switchLineItem": SwitchLineItem.class, - @"switch": Switch.class, + @"switch": MVMCoreUISwitch.class, @"leftRightLabelView": LeftRightLabelView.class, @"actionDetailWithImage": ActionDetailWithImage.class, @"image": MFLoadImageView.class,