Button defaulting

This commit is contained in:
Pfeil, Scott Robert 2019-03-20 14:42:20 -04:00
parent a8145fd3c0
commit c411ec83ee
4 changed files with 128 additions and 111 deletions

View File

@ -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;

View File

@ -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 <MVMCoreActionDelegateProtocol>*)delegate : nil) additionalData:additionalData buttonDelegate:([delegate conformsToProtocol:@protocol(ButtonDelegateProtocol)] ? (id <ButtonDelegateProtocol>)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 {

View File

@ -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 <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)buttonDelegate;
- (nonnull instancetype)initWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap actionDelegate:(nullable NSObject <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate buttonDelegate:(nullable id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)buttonDelegate;
- (void)setupWithPrimaryButtonMap:(nullable NSDictionary *)primaryButtonMap secondaryButtonMap:(nullable NSDictionary *)secondaryButtonMap actionDelegate:(nullable NSObject <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)buttonDelegate;

View File

@ -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 <MVMCoreActionDelegateProtocol>*)delegate : nil) buttonDelegate:([delegate conformsToProtocol:@protocol(ButtonDelegateProtocol)] ? (id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate buttonDelegate:(nullable id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate additionalData:(nullable NSDictionary *)additionalData buttonDelegate:(nullable id <ButtonDelegateProtocol>)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 <MVMCoreActionDelegateProtocol>*)actionDelegate buttonDelegate:(nullable id <ButtonDelegateProtocol>)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) {