Reforming inversion

This commit is contained in:
Kevin G Christiano 2020-05-05 13:44:18 -04:00
parent 4ffd07dd3d
commit 257420c3e4
3 changed files with 46 additions and 98 deletions

View File

@ -11,6 +11,17 @@ import UIKit
public typealias FacadeElements = (fill: UIColor?, text: UIColor?, border: UIColor?) public typealias FacadeElements = (fill: UIColor?, text: UIColor?, border: UIColor?)
public enum ButtonStyle: String, Codable {
case primary
case secondary
}
public enum ButtonSize: String, Codable {
case standard
case tiny
}
public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWatcherFieldProtocol { public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWatcherFieldProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
@ -21,8 +32,8 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
public var title: String public var title: String
public var action: ActionModelProtocol public var action: ActionModelProtocol
public var enabled: Bool = true public var enabled: Bool = true
public var style: Styler.Button? public var style: ButtonStyle?
public var size: Styler.Button.Size? = .standard public var size: ButtonSize? = .standard
public var groupName: String = "" public var groupName: String = ""
public var isInverted: Bool = false public var isInverted: Bool = false
@ -35,9 +46,6 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
text: disabled_textColor(), text: disabled_textColor(),
border: disabled_borderColor()) border: disabled_borderColor())
public var backgroundColor_standard: Color?
public var backgroundColor_inverted: Color?
public var enabledFillColor: Color? public var enabledFillColor: Color?
public var enabledTextColor: Color? public var enabledTextColor: Color?
public var enabledBorderColor: Color? public var enabledBorderColor: Color?
@ -61,7 +69,7 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
// MARK: - Default Button Facades // MARK: - Default Button Facades
//-------------------------------------------------- //--------------------------------------------------
/* /*
private var primaryFacade = Facade(enabledFillColor: .mvmBlack, private var primaryFacade = Facade(enabledFillColor: ,
enabledTextColor: .mvmWhite, enabledTextColor: .mvmWhite,
disabledFillColor: .mvmCoolGray6, disabledFillColor: .mvmCoolGray6,
disabledTextColor: .mvmWhite, disabledTextColor: .mvmWhite,
@ -122,10 +130,6 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
// MARK: - Methods // MARK: - Methods
//-------------------------------------------------- //--------------------------------------------------
public func backgroundColor() -> UIColor? {
return (isInverted ? backgroundColor_inverted : backgroundColor_standard)?.uiColor
}
public func getBorderWidth() -> CGFloat? { public func getBorderWidth() -> CGFloat? {
return isInverted ? borderWidth_inverted : borderWidth return isInverted ? borderWidth_inverted : borderWidth
} }
@ -170,21 +174,6 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
case size case size
case facade case facade
case groupName case groupName
case backgroundColor_standard
case backgroundColor_inverted
case enabledFillColor
case enabledTextColor
case enabledBorderColor
case enabledFillColor_inverted
case enabledTextColor_inverted
case enabledBorderColor_inverted
case disabledFillColor
case disabledTextColor
case disabledBorderColor
case disabledFillColor_inverted
case disabledTextColor_inverted
case disabledBorderColor_inverted
} }
//-------------------------------------------------- //--------------------------------------------------
@ -198,11 +187,11 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
title = try typeContainer.decode(String.self, forKey: .title) title = try typeContainer.decode(String.self, forKey: .title)
action = try typeContainer.decodeModel(codingKey: .action) action = try typeContainer.decodeModel(codingKey: .action)
if let style = try typeContainer.decodeIfPresent(Styler.Button.self, forKey: .style) { if let style = try typeContainer.decodeIfPresent(ButtonStyle.self, forKey: .style) {
self.style = style self.style = style
} }
if let size = try typeContainer.decodeIfPresent(Styler.Button.Size.self, forKey: .size) { if let size = try typeContainer.decodeIfPresent(ButtonSize.self, forKey: .size) {
self.size = size self.size = size
} }
@ -211,27 +200,12 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
} }
if let isInverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) { if let isInverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
facade.isInverted = isInverted self.isInverted = isInverted
} }
if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) { if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
self.groupName = groupName self.groupName = groupName
} }
backgroundColor_standard = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor_standard)
backgroundColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor_inverted)
enabledFillColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledFillColor)
enabledTextColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledTextColor)
enabledBorderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledBorderColor)
enabledFillColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledFillColor_inverted)
enabledTextColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledTextColor_inverted)
enabledBorderColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .enabledBorderColor_inverted)
disabledFillColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledFillColor)
disabledTextColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledTextColor)
disabledBorderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledBorderColor)
disabledFillColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledFillColor_inverted)
disabledTextColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledTextColor_inverted)
disabledBorderColor_inverted = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledBorderColor_inverted)
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
@ -240,25 +214,9 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupW
try container.encode(title, forKey: .title) try container.encode(title, forKey: .title)
try container.encode(enabled, forKey: .enabled) try container.encode(enabled, forKey: .enabled)
try container.encodeModel(action, forKey: .action) try container.encodeModel(action, forKey: .action)
try container.encodeIfPresent(facade, forKey: .facade)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(style, forKey: .style) try container.encodeIfPresent(style, forKey: .style)
try container.encodeIfPresent(size, forKey: .size) try container.encodeIfPresent(size, forKey: .size)
try container.encodeIfPresent(groupName, forKey: .groupName) try container.encodeIfPresent(groupName, forKey: .groupName)
try container.encodeIfPresent(backgroundColor_standard, forKey: .backgroundColor_standard)
try container.encodeIfPresent(backgroundColor_inverted, forKey: .backgroundColor_inverted)
try container.encodeIfPresent(enabledFillColor, forKey: .enabledFillColor)
try container.encodeIfPresent(enabledTextColor, forKey: .enabledTextColor)
try container.encodeIfPresent(enabledBorderColor, forKey: .enabledBorderColor)
try container.encodeIfPresent(enabledFillColor_inverted, forKey: .enabledFillColor_inverted)
try container.encodeIfPresent(enabledTextColor_inverted, forKey: .enabledTextColor_inverted)
try container.encodeIfPresent(enabledBorderColor_inverted, forKey: .enabledBorderColor_inverted)
try container.encodeIfPresent(disabledFillColor, forKey: .disabledFillColor)
try container.encodeIfPresent(disabledTextColor, forKey: .disabledTextColor)
try container.encodeIfPresent(disabledBorderColor, forKey: .disabledBorderColor)
try container.encodeIfPresent(disabledFillColor_inverted, forKey: .disabledFillColor_inverted)
try container.encodeIfPresent(disabledTextColor_inverted, forKey: .disabledTextColor_inverted)
try container.encodeIfPresent(disabledBorderColor_inverted, forKey: .disabledBorderColor_inverted)
} }
} }

View File

@ -26,6 +26,11 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
didSet { style() } didSet { style() }
} }
private enum ButtonHeight: CGFloat {
case tiny = 20
case standard = 42
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Computed Properties // MARK: - Computed Properties
//-------------------------------------------------- //--------------------------------------------------
@ -55,20 +60,20 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
/// The primary styling for a button. Should be used for main buttons /// The primary styling for a button. Should be used for main buttons
public func stylePrimary() { public func stylePrimary() {
enabledTitleColor = buttonModel?.facade.enabled.text ?? .mvmWhite enabledTitleColor = buttonModel?.enabledElements.text ?? .mvmWhite
disabledTitleColor = buttonModel?.facade.disabled.text ?? .mvmWhite disabledTitleColor = buttonModel?.disabledElements.text ?? .mvmWhite
layer.borderWidth = buttonModel?.facade.getBorderWidth() ?? 0 layer.borderWidth = buttonModel?.getBorderWidth() ?? 0
backgroundColor = isEnabled ? buttonModel?.facade.enabled.fill ?? .mvmBlack : buttonModel?.facade.disabled.fill ?? .mvmCoolGray6 backgroundColor = isEnabled ? buttonModel?.enabledElements.fill ?? .mvmBlack : buttonModel?.disabledElements.fill ?? .mvmCoolGray6
} }
/// The secondary styling for a button. Should be used for secondary buttons /// The secondary styling for a button. Should be used for secondary buttons
public func styleSecondary() { public func styleSecondary() {
enabledTitleColor = buttonModel?.facade.enabled.text ?? .mvmBlack enabledTitleColor = buttonModel?.enabledElements.text ?? .mvmBlack
disabledTitleColor = buttonModel?.facade.disabled.text ?? .mvmCoolGray6 disabledTitleColor = buttonModel?.disabledElements.text ?? .mvmCoolGray6
backgroundColor = .clear backgroundColor = .clear
layer.borderWidth = buttonModel?.facade.getBorderWidth() ?? 1 layer.borderWidth = buttonModel?.getBorderWidth() ?? 1
borderColor = isEnabled ? buttonModel?.facade.enabled.border ?? .mvmBlack : buttonModel?.facade.disabled.border ?? .mvmCoolGray6 borderColor = isEnabled ? buttonModel?.enabledElements.border ?? .mvmBlack : buttonModel?.disabledElements.border ?? .mvmCoolGray6
} }
/// Styles the button based on the model style /// Styles the button based on the model style
@ -82,30 +87,30 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
stylePrimary() stylePrimary()
} }
if let titleColor = buttonModel?.facade.enabled.text { if let titleColor = buttonModel?.enabledElements.text {
enabledTitleColor = titleColor enabledTitleColor = titleColor
} }
if let disabledTitleColor = buttonModel?.facade.disabled.text { if let disabledTitleColor = buttonModel?.disabledElements.text {
self.disabledTitleColor = disabledTitleColor self.disabledTitleColor = disabledTitleColor
} }
if isEnabled { if isEnabled {
if let fillColor = buttonModel?.facade.enabled.fill { if let fillColor = buttonModel?.enabledElements.fill {
backgroundColor = fillColor backgroundColor = fillColor
} }
if let borderColor = buttonModel?.facade.enabled.border { if let borderColor = buttonModel?.enabledElements.border {
layer.borderWidth = buttonModel?.facade.getBorderWidth() ?? 1 layer.borderWidth = buttonModel?.getBorderWidth() ?? 1
self.borderColor = borderColor self.borderColor = borderColor
} }
} else { } else {
if let fillColor = buttonModel?.facade.disabled.fill { if let fillColor = buttonModel?.disabledElements.fill {
backgroundColor = fillColor backgroundColor = fillColor
} }
if let borderColor = buttonModel?.facade.disabled.border { if let borderColor = buttonModel?.disabledElements.border {
layer.borderWidth = buttonModel?.facade.getBorderWidth() ?? 1 layer.borderWidth = buttonModel?.getBorderWidth() ?? 1
self.borderColor = borderColor self.borderColor = borderColor
} }
} }
@ -119,18 +124,18 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
PillButton.getHeight(for: buttonModel?.size, size: size) PillButton.getHeight(for: buttonModel?.size, size: size)
} }
public static func getHeight(for buttonSize: Styler.Button.Size?, size: CGFloat) -> CGFloat { public static func getHeight(for buttonSize: ButtonSize?, size: CGFloat) -> CGFloat {
switch buttonSize { switch buttonSize {
case .tiny: case .tiny:
return MFSizeObject(standardSize: Styler.Button.Height.tiny.rawValue, return MFSizeObject(standardSize: ButtonHeight.tiny.rawValue,
standardiPadPortraitSize: 34, standardiPadPortraitSize: 34,
iPadProLandscapeSize: 38)?.getValueBased(onSize: size) ?? Styler.Button.Height.tiny.rawValue iPadProLandscapeSize: 38)?.getValueBased(onSize: size) ?? ButtonHeight.tiny.rawValue
default: default:
return MFSizeObject(standardSize: Styler.Button.Height.standard.rawValue, return MFSizeObject(standardSize: ButtonHeight.standard.rawValue,
standardiPadPortraitSize: 46, standardiPadPortraitSize: 46,
iPadProLandscapeSize: 50)?.getValueBased(onSize: size) ?? Styler.Button.Height.standard.rawValue iPadProLandscapeSize: 50)?.getValueBased(onSize: size) ?? ButtonHeight.standard.rawValue
} }
} }
@ -184,10 +189,10 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
switch buttonModel?.size { switch buttonModel?.size {
case .tiny: case .tiny:
titleLabel?.font = MFFonts.mfFont75Bd(11 * (intrinsicContentSize.height / Styler.Button.Height.tiny.rawValue)) titleLabel?.font = MFFonts.mfFont75Bd(11 * (intrinsicContentSize.height / ButtonHeight.tiny.rawValue))
default: default:
titleLabel?.font = MFFonts.mfFont75Bd(13 * (intrinsicContentSize.height / Styler.Button.Height.standard.rawValue)) titleLabel?.font = MFFonts.mfFont75Bd(13 * (intrinsicContentSize.height / ButtonHeight.standard.rawValue))
} }
layer.cornerRadius = getInnerPadding() layer.cornerRadius = getInnerPadding()

View File

@ -172,21 +172,6 @@ open class Styler {
} }
} }
public enum Button: String, Codable {
case primary
case secondary
public enum Size: String, Codable {
case standard
case tiny
}
public enum Height: CGFloat {
case tiny = 20
case standard = 42
}
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Functions // MARK: - Functions
//-------------------------------------------------- //--------------------------------------------------