From c411ec83eed72480ec2106a9ec25384057fe6976 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Wed, 20 Mar 2019 14:42:20 -0400 Subject: [PATCH] Button defaulting --- MVMCoreUI/Atoms/Buttons/PrimaryButton.h | 4 + MVMCoreUI/Atoms/Buttons/PrimaryButton.m | 171 ++++++++++++++---------- MVMCoreUI/Molecules/PrimaryButtonView.h | 7 +- MVMCoreUI/Molecules/PrimaryButtonView.m | 57 ++++---- 4 files changed, 128 insertions(+), 111 deletions(-) diff --git a/MVMCoreUI/Atoms/Buttons/PrimaryButton.h b/MVMCoreUI/Atoms/Buttons/PrimaryButton.h index ba1c06d2..182a527b 100644 --- a/MVMCoreUI/Atoms/Buttons/PrimaryButton.h +++ b/MVMCoreUI/Atoms/Buttons/PrimaryButton.h @@ -75,6 +75,10 @@ static CGFloat const PrimaryButtonSmallHeight = 30.0; #pragma mark - Setting +// The new defaults. +- (void)setAsStandardCustom; +- (void)setAsSecondaryCustom; + // For setting after creation. - (void)setAsSmallButton:(BOOL)smallButton enabled:(BOOL)enabled bordered:(BOOL)bordered; diff --git a/MVMCoreUI/Atoms/Buttons/PrimaryButton.m b/MVMCoreUI/Atoms/Buttons/PrimaryButton.m index 5300d774..960e29e7 100644 --- a/MVMCoreUI/Atoms/Buttons/PrimaryButton.m +++ b/MVMCoreUI/Atoms/Buttons/PrimaryButton.m @@ -34,6 +34,56 @@ @implementation PrimaryButton +- (instancetype)initWithCoder:(NSCoder *)aDecoder { + if (self = [super initWithCoder:aDecoder]) { + [self pinHeight]; + [self setAsRed]; + [self setAsSmallButton:NO]; + } + return self; +} + +- (CGSize)intrinsicContentSize { + CGSize size = [super intrinsicContentSize]; + CGSize newSize = CGSizeMake(size.width + (self.smallButton ? [[self innerPaddingSmallButton] getValueBasedOnSize:self.sizeForSizing]*2 : [[self innerPadding] getValueBasedOnSize:self.sizeForSizing]*2), size.height); + if (self.tinyButton) { + newSize = CGSizeMake(size.width + [[self innerPaddingTinyButton] getValueBasedOnSize:self.sizeForSizing]*2 , size.height); + } + if (self.smallButton) { + CGFloat minimumWidth = [[self minimumWidthSmallButton] getValueBasedOnSize:self.sizeForSizing]; + if (newSize.width > minimumWidth) { + return newSize; + } else { + return CGSizeMake(minimumWidth, size.height); + } + } else if (self.tinyButton) { + CGFloat minimumWidth = [[self minimumWidthTinyButton] getValueBasedOnSize:self.sizeForSizing]; + if (newSize.width > minimumWidth) { + return newSize; + } else { + return CGSizeMake(minimumWidth, size.height); + } + } else { + CGFloat minimumWidth = [[self minimumWidth] getValueBasedOnSize:self.sizeForSizing]; + if (newSize.width > minimumWidth) { + return newSize; + } else { + return CGSizeMake(minimumWidth, size.height); + } + } +} + +- (void)pinHeight { + + // Adds the height constraint. + if (!self.height) { + NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[[self buttonHeight] getValueBasedOnSize:self.sizeForSizing]]; + self.height = height; + self.heightConstraint = height; + height.active = YES; + } +} + #pragma mark - Sizing - (MFSizeObject *)innerPadding { @@ -470,6 +520,15 @@ #pragma mark - Creation ++ (instancetype)getButton { + PrimaryButton *button = [PrimaryButton buttonWithType:UIButtonTypeCustom]; + button.translatesAutoresizingMaskIntoConstraints = NO; + button.sizeForSizing = [MVMCoreUISplitViewController getApplicationViewWidth]; + [button pinHeight]; + button.skipHighlighted = NO; + return button; +} + + (instancetype)primaryButton:(BOOL)enabled { PrimaryButton *button = [self getButton]; button.primaryButtonType = PrimaryButtonTypeBlack; @@ -561,86 +620,56 @@ return button; } -#pragma mark - For Subclassing +#pragma mark - Molecule protocol + +- (void)setAsStandardCustom { + // Default to standard look. + self.primaryButtonType = PrimaryButtonTypeCustom; + self.fillColor = [UIColor blackColor]; + self.textColor = [UIColor whiteColor]; + self.borderColor = nil; + _bordered = false; +} + +- (void)setAsSecondaryCustom { + // Default to standard look. + self.primaryButtonType = PrimaryButtonTypeCustom; + self.fillColor = nil; + self.textColor = nil; + self.borderColor = [UIColor blackColor]; + _bordered = true; +} + +- (void)setAsMolecule { + [self setAsStandardCustom]; +} - (void)setWithJSON:(NSDictionary *)json delegate:(NSObject *)delegate additionalData:(nullable NSDictionary *)additionalData { self.primaryButtonType = PrimaryButtonTypeCustom; NSString *color = [json string:@"fillColor"]; - self.fillColor = (color ? [UIColor mfGetColorForHex:color] : nil); - color = [json string:KeyTextColor]; - self.textColor = (color ? [UIColor mfGetColorForHex:color] : nil); - color = [json string:@"borderColor"]; - self.borderColor = (color ? [UIColor mfGetColorForHex:color] : nil); + if (color) { + self.fillColor = [UIColor mfGetColorForHex:color]; + } + if ((color = [json string:KeyTextColor])) { + self.textColor = [UIColor mfGetColorForHex:color]; + } + if ((color = [json string:@"borderColor"])) { + self.borderColor = [UIColor mfGetColorForHex:color]; + } _bordered = self.borderColor != nil; - color = [json string:@"disabledFillColor"]; - self.disabledFillColor = (color ? [UIColor mfGetColorForHex:color] : nil); - color = [json string:@"disabledTextColor"]; - self.disabledTextColor = (color ? [UIColor mfGetColorForHex:color] : nil); - color = [json string:@"disabledBorderColor"]; - self.disabledBorderColor = (color ? [UIColor mfGetColorForHex:color] : nil); + if ((color = [json string:@"disabledFillColor"])) { + self.disabledFillColor = [UIColor mfGetColorForHex:color]; + } + if ((color = [json string:@"disabledTextColor"])) { + self.disabledTextColor = [UIColor mfGetColorForHex:color]; + } + if ((color = [json string:@"disabledBorderColor"])) { + self.disabledBorderColor = [UIColor mfGetColorForHex:color]; + } [self setAsSmallButton:[json boolForKey:@"small"]]; [self setWithActionMap:json actionDelegate:([delegate conformsToProtocol:@protocol(MVMCoreActionDelegateProtocol)] ? (NSObject *)delegate : nil) additionalData:additionalData buttonDelegate:([delegate conformsToProtocol:@protocol(ButtonDelegateProtocol)] ? (id )delegate : nil)]; } -- (instancetype)initWithCoder:(NSCoder *)aDecoder { - if (self = [super initWithCoder:aDecoder]) { - [self pinHeight]; - [self setAsRed]; - [self setAsSmallButton:NO]; - } - return self; -} - -- (CGSize)intrinsicContentSize { - CGSize size = [super intrinsicContentSize]; - CGSize newSize = CGSizeMake(size.width + (self.smallButton ? [[self innerPaddingSmallButton] getValueBasedOnSize:self.sizeForSizing]*2 : [[self innerPadding] getValueBasedOnSize:self.sizeForSizing]*2), size.height); - if (self.tinyButton) { - newSize = CGSizeMake(size.width + [[self innerPaddingTinyButton] getValueBasedOnSize:self.sizeForSizing]*2 , size.height); - } - if (self.smallButton) { - CGFloat minimumWidth = [[self minimumWidthSmallButton] getValueBasedOnSize:self.sizeForSizing]; - if (newSize.width > minimumWidth) { - return newSize; - } else { - return CGSizeMake(minimumWidth, size.height); - } - } else if (self.tinyButton) { - CGFloat minimumWidth = [[self minimumWidthTinyButton] getValueBasedOnSize:self.sizeForSizing]; - if (newSize.width > minimumWidth) { - return newSize; - } else { - return CGSizeMake(minimumWidth, size.height); - } - } else { - CGFloat minimumWidth = [[self minimumWidth] getValueBasedOnSize:self.sizeForSizing]; - if (newSize.width > minimumWidth) { - return newSize; - } else { - return CGSizeMake(minimumWidth, size.height); - } - } -} - -+ (instancetype)getButton { - PrimaryButton *button = [PrimaryButton buttonWithType:UIButtonTypeCustom]; - button.translatesAutoresizingMaskIntoConstraints = NO; - button.sizeForSizing = [MVMCoreUISplitViewController getApplicationViewWidth]; - [button pinHeight]; - button.skipHighlighted = NO; - return button; -} - -- (void)pinHeight { - - // Adds the height constraint. - if (!self.height) { - NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[[self buttonHeight] getValueBasedOnSize:self.sizeForSizing]]; - self.height = height; - self.heightConstraint = height; - height.active = YES; - } -} - #pragma mark - Handling Validations - (void)setEnabledByValidity { diff --git a/MVMCoreUI/Molecules/PrimaryButtonView.h b/MVMCoreUI/Molecules/PrimaryButtonView.h index 7140a0d3..dbfd0c20 100644 --- a/MVMCoreUI/Molecules/PrimaryButtonView.h +++ b/MVMCoreUI/Molecules/PrimaryButtonView.h @@ -24,14 +24,9 @@ // Inits with two buttons. - (nonnull instancetype)initWithTwoButtons; -// Inits with whatever is in the passed in button map. (could be 0, 1, or 2 buttons) +// Legacy: Sets up with whatever is in the passed in button map. (could be 0, 1, or 2 buttons) - (nonnull instancetype)initButtonSmall:(BOOL)small buttonMap:(nullable NSDictionary *)buttonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate; - (nonnull instancetype)initWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate; - -// Sets up with whatever is in the passed in. (could be 0, 1, or 2 buttons) -- (void)setupWithFirstButtonJSON:(nullable NSDictionary *)firstButtonJSON secondButtonJSON:(nullable NSDictionary *)secondButtonJSON additionalData:(nullable NSDictionary *)additionalData actionDelegate:(nullable NSObject *)actionDelegate buttonDelegate:(nullable id )buttonDelegate; - -// Legacy: Sets up with whatever is in the passed in button map. (could be 0, 1, or 2 buttons) - (void)setupWithButtonMap:(nullable NSDictionary *)buttonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate; - (void)setupWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate; diff --git a/MVMCoreUI/Molecules/PrimaryButtonView.m b/MVMCoreUI/Molecules/PrimaryButtonView.m index 16ce03ed..a479efab 100644 --- a/MVMCoreUI/Molecules/PrimaryButtonView.m +++ b/MVMCoreUI/Molecules/PrimaryButtonView.m @@ -42,7 +42,13 @@ if (backgroundColorString) { self.backgroundColor = [UIColor mfGetColorForHex:backgroundColorString]; } - [self setupWithFirstButtonJSON:[json dict:@"firstButton"] secondButtonJSON:[json dict:@"secondButton"] additionalData:nil actionDelegate:([delegate conformsToProtocol:@protocol(MVMCoreActionDelegateProtocol)] ? (NSObject *)delegate : nil) buttonDelegate:([delegate conformsToProtocol:@protocol(ButtonDelegateProtocol)] ? (id )delegate : nil)]; + NSDictionary *primaryButtonMap = [json dict:@"primaryButton"]; + NSDictionary *secondaryButtonMap = [json dict:@"secondaryButton"]; + [self setupUIWithPrimaryButtonMap:primaryButtonMap secondaryButtonMap:secondaryButtonMap]; + [self.primaryButton setAsStandardCustom]; + [self.secondaryButton setAsSecondaryCustom]; + [self.primaryButton setWithJSON:primaryButtonMap delegate:delegate additionalData:additionalData]; + [self.secondaryButton setWithJSON:secondaryButtonMap delegate:delegate additionalData:additionalData]; } #pragma mark - Inits @@ -89,6 +95,8 @@ return self; } +#pragma mark - Legacy Setup + - (nonnull instancetype)initButtonSmall:(BOOL)small buttonMap:(nullable NSDictionary *)buttonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate { if (self = [self init]) { [self setupWithButtonMap:buttonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; @@ -105,24 +113,29 @@ return self; } -#pragma mark - Setup - - (void)setupWithButtonMap:(nullable NSDictionary *)buttonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate { - NSDictionary *secondaryButtonMap = [buttonMap dict:KeySecondaryButton]; NSDictionary *primaryButtonMap = [buttonMap dict:KeyPrimaryButton]; [self setupWithPrimaryButtonMap:primaryButtonMap secondaryButtonMap:secondaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; } -- (void)setupWithFirstButtonJSON:(nullable NSDictionary *)firstButtonJSON secondButtonJSON:(nullable NSDictionary *)secondButtonJSON additionalData:(nullable NSDictionary *)additionalData actionDelegate:(nullable NSObject *)actionDelegate buttonDelegate:(nullable id )buttonDelegate { - [self setupWithPrimaryButtonMap:secondButtonJSON secondaryButtonMap:firstButtonJSON additionalData:additionalData actionDelegate:actionDelegate buttonDelegate:buttonDelegate legacyJSON:NO]; -} - - (void)setupWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap actionDelegate:(nullable NSObject *)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id )buttonDelegate { - [self setupWithPrimaryButtonMap:primaryButtonMap secondaryButtonMap:secondaryButtonMap additionalData:additionalData actionDelegate:actionDelegate buttonDelegate:buttonDelegate legacyJSON:YES]; + [self setupUIWithPrimaryButtonMap:primaryButtonMap secondaryButtonMap:secondaryButtonMap]; + if (self.primaryButton && self.secondaryButton) { + [self.primaryButton setWithActionMap:primaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; + [self.secondaryButton setWithActionMap:secondaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; + } else if (self.primaryButton) { + [self.primaryButton setWithActionMap:primaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; + self.primaryButton.bordered = NO; + } else { + [self.primaryButton setWithActionMap:secondaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; + self.primaryButton.bordered = YES; + } } -- (void)setupWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap additionalData:(nullable NSDictionary *)additionalData actionDelegate:(nullable NSObject *)actionDelegate buttonDelegate:(nullable id )buttonDelegate legacyJSON:(BOOL)legacyJSON { +#pragma mark - Setup + +- (void)setupUIWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap { if (primaryButtonMap && secondaryButtonMap) { self.height.active = NO; @@ -132,13 +145,6 @@ self.twoButtonView = nil; [self setupWithTwoButtons]; } - if (legacyJSON) { - [self.primaryButton setWithActionMap:primaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; - [self.secondaryButton setWithActionMap:secondaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; - } else { - [self.primaryButton setWithJSON:primaryButtonMap delegate:actionDelegate additionalData:additionalData]; - [self.secondaryButton setWithJSON:secondaryButtonMap delegate:actionDelegate additionalData:additionalData]; - } } else if (primaryButtonMap || secondaryButtonMap) { self.height.active = NO; @@ -149,23 +155,6 @@ self.secondaryButton = nil; [self setupWithSingleButton]; } - [self alignCenter]; - - if (legacyJSON) { - if (primaryButtonMap) { - - // Only primary button - [self.primaryButton setWithActionMap:primaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; - self.primaryButton.bordered = NO; - } else { - - // Only secondary button - [self.primaryButton setWithActionMap:secondaryButtonMap actionDelegate:actionDelegate additionalData:additionalData buttonDelegate:buttonDelegate]; - self.primaryButton.bordered = YES; - } - } else { - [self.primaryButton setWithJSON:primaryButtonMap delegate:actionDelegate additionalData:additionalData]; - } } else { [self removeSubviews]; if (!self.height) {