created color helpers

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-04 15:11:02 -05:00
parent 5820401c50
commit 3acde8b4c8
5 changed files with 113 additions and 127 deletions

View File

@ -57,7 +57,7 @@
EAF7F0B3289B1ADC00B287F5 /* LabelAttributeAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B2289B1ADC00B287F5 /* LabelAttributeAction.swift */; }; EAF7F0B3289B1ADC00B287F5 /* LabelAttributeAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B2289B1ADC00B287F5 /* LabelAttributeAction.swift */; };
EAF7F0B5289C126F00B287F5 /* UILabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B4289C126F00B287F5 /* UILabel.swift */; }; EAF7F0B5289C126F00B287F5 /* UILabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B4289C126F00B287F5 /* UILabel.swift */; };
EAF7F0B7289C12A600B287F5 /* UITapGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B6289C12A600B287F5 /* UITapGestureRecognizer.swift */; }; EAF7F0B7289C12A600B287F5 /* UITapGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B6289C12A600B287F5 /* UITapGestureRecognizer.swift */; };
EAF7F0B9289C139800B287F5 /* ModelColorHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B8289C139800B287F5 /* ModelColorHelper.swift */; }; EAF7F0B9289C139800B287F5 /* ModelColorHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0B8289C139800B287F5 /* ModelColorHelpers.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -122,7 +122,7 @@
EAF7F0B2289B1ADC00B287F5 /* LabelAttributeAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelAttributeAction.swift; sourceTree = "<group>"; }; EAF7F0B2289B1ADC00B287F5 /* LabelAttributeAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelAttributeAction.swift; sourceTree = "<group>"; };
EAF7F0B4289C126F00B287F5 /* UILabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UILabel.swift; sourceTree = "<group>"; }; EAF7F0B4289C126F00B287F5 /* UILabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UILabel.swift; sourceTree = "<group>"; };
EAF7F0B6289C12A600B287F5 /* UITapGestureRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITapGestureRecognizer.swift; sourceTree = "<group>"; }; EAF7F0B6289C12A600B287F5 /* UITapGestureRecognizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITapGestureRecognizer.swift; sourceTree = "<group>"; };
EAF7F0B8289C139800B287F5 /* ModelColorHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelColorHelper.swift; sourceTree = "<group>"; }; EAF7F0B8289C139800B287F5 /* ModelColorHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelColorHelpers.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -258,8 +258,8 @@
EA3C3B4B2894823E000CA526 /* AnyProxy-PropertyWrapper.swift */, EA3C3B4B2894823E000CA526 /* AnyProxy-PropertyWrapper.swift */,
EAF7F09D289AAEC000B287F5 /* Constants.swift */, EAF7F09D289AAEC000B287F5 /* Constants.swift */,
EA3361B5288B2A410071C351 /* Control.swift */, EA3361B5288B2A410071C351 /* Control.swift */,
EAF7F0B8289C139800B287F5 /* ModelColorHelpers.swift */,
EAF7F09F289AB7EC00B287F5 /* View.swift */, EAF7F09F289AB7EC00B287F5 /* View.swift */,
EAF7F0B8289C139800B287F5 /* ModelColorHelper.swift */,
); );
path = Classes; path = Classes;
sourceTree = "<group>"; sourceTree = "<group>";
@ -464,7 +464,7 @@
EA33624728931B050071C351 /* Initable.swift in Sources */, EA33624728931B050071C351 /* Initable.swift in Sources */,
EAF7F0A4289B017C00B287F5 /* LabelAttributeModel.swift in Sources */, EAF7F0A4289B017C00B287F5 /* LabelAttributeModel.swift in Sources */,
EAF7F0B1289B177F00B287F5 /* LabelAttributeColor.swift in Sources */, EAF7F0B1289B177F00B287F5 /* LabelAttributeColor.swift in Sources */,
EAF7F0B9289C139800B287F5 /* ModelColorHelper.swift in Sources */, EAF7F0B9289C139800B287F5 /* ModelColorHelpers.swift in Sources */,
EA3361BD288B2C760071C351 /* TypeAlias.swift in Sources */, EA3361BD288B2C760071C351 /* TypeAlias.swift in Sources */,
EAF7F09A2899B17200B287F5 /* CATransaction.swift in Sources */, EAF7F09A2899B17200B287F5 /* CATransaction.swift in Sources */,
EAF7F0A2289AFB3900B287F5 /* Errorable.swift in Sources */, EAF7F0A2289AFB3900B287F5 /* Errorable.swift in Sources */,

View File

@ -1,59 +0,0 @@
//
// ModelColorHelper.swift
// VDS
//
// Created by Matt Bruce on 8/4/22.
//
import Foundation
import UIKit
public class ModelColor<ModelType:Disabling & Surfaceable> {
public var disabledSurfaceLight: UIColor = .clear
public var disabledSurfaceDark: UIColor = .clear
public var enabledSurfaceLight: UIColor = .clear
public var enabledSurfaceDark: UIColor = .clear
public func getColor(_ viewModel: ModelType) -> UIColor {
var color: UIColor
if viewModel.disabled {
if viewModel.surface == .light {
color = disabledSurfaceLight
} else {
color = disabledSurfaceDark
}
} else {
if viewModel.surface == .light {
color = enabledSurfaceLight
} else {
color = enabledSurfaceDark
}
}
return color
}
}
public class ModelColorHelper<ModelType:Disabling & Surfaceable> {
public var onColor = ModelColor<ModelType>()
public var offColor: ModelColor<ModelType>?
public var errorOnColor: ModelColor<ModelType>?
public var errorOffColor: ModelColor<ModelType>?
public init (){}
public func getColor(model: ModelType, isOn: Bool = true, isError: Bool = false) -> UIColor {
if isOn {
if isError {
return errorOnColor?.getColor(model) ?? onColor.getColor(model)
} else {
return onColor.getColor(model)
}
} else {
if isError {
return errorOffColor?.getColor(model) ?? (offColor?.getColor(model) ?? onColor.getColor(model))
} else {
return offColor?.getColor(model) ?? onColor.getColor(model)
}
}
}
}

View File

@ -0,0 +1,50 @@
//
// ModelColorHelper.swift
// VDS
//
// Created by Matt Bruce on 8/4/22.
//
import Foundation
import UIKit
public protocol Colorable {
associatedtype ModelType
func getColor(_ viewModel: ModelType) -> UIColor
}
public protocol BinaryColorable{
var userTrueColor: Bool { get }
}
open class BinaryColorHelper<ModelType:BinaryColorable> {
public var trueColor: UIColor = .clear
public var falseColor: UIColor = .clear
public func getColor(_ viewModel: ModelType) -> UIColor {
return viewModel.userTrueColor ? trueColor : falseColor
}
}
open class BinarySurfaceColorHelper<ModelType:Surfaceable & BinaryColorable>: Colorable{
public var light = BinaryColorHelper<ModelType>()
public var dark = BinaryColorHelper<ModelType>()
public func getColor(_ viewModel: ModelType) -> UIColor {
if viewModel.surface == .light {
return light.getColor(viewModel)
} else {
return dark.getColor(viewModel)
}
}
}
open class BinaryModelColorHelper<ModelType:Disabling & Surfaceable & BinaryColorable>: Colorable {
public var disabled = BinarySurfaceColorHelper<ModelType>()
public var enabled = BinarySurfaceColorHelper<ModelType>()
public func getColor(_ viewModel: ModelType) -> UIColor {
return viewModel.disabled ? disabled.getColor(viewModel) : enabled.getColor(viewModel)
}
}

View File

@ -271,73 +271,48 @@ open class CheckboxBase<ModelType: CheckboxModel>: Control<ModelType>, Changable
/// Manages the appearance of the checkbox. /// Manages the appearance of the checkbox.
private var shapeLayer: CAShapeLayer? private var shapeLayer: CAShapeLayer?
private func getCheckboxBackgroundColor(_ viewModel: ModelType) -> UIColor { private var checkboxBackgroundColor: CheckboxErrorColorHelper = {
var colors: (on: UIColor, off: UIColor) let helper = CheckboxErrorColorHelper()
if viewModel.disabled { helper.disabled.light.trueColor = VDSColor.interactiveDisabledOnlight
if viewModel.surface == .light { helper.disabled.dark.trueColor = VDSColor.interactiveDisabledOndark
colors = (on: VDSColor.interactiveDisabledOnlight, off: .clear) helper.error.light.trueColor = VDSColor.elementsPrimaryOnlight
} else { helper.error.light.falseColor = VDSColor.feedbackErrorBackgroundOnlight
colors = (on: VDSColor.interactiveDisabledOndark, off: .clear) helper.error.dark.trueColor = VDSColor.elementsPrimaryOndark
} helper.error.dark.falseColor = VDSColor.feedbackErrorBackgroundOndark
} else { helper.enabled.light.trueColor = VDSColor.elementsPrimaryOnlight
if viewModel.showError { helper.enabled.dark.trueColor = VDSColor.elementsPrimaryOndark
if viewModel.surface == .light { return helper
colors = (on: VDSColor.elementsPrimaryOnlight, off: VDSColor.feedbackErrorBackgroundOnlight) }()
} else {
colors = (on: VDSColor.elementsPrimaryOndark, off: VDSColor.feedbackErrorBackgroundOndark)
}
} else {
if viewModel.surface == .light {
colors = (on: VDSColor.elementsPrimaryOnlight, off: .clear)
} else {
colors = (on: VDSColor.elementsPrimaryOndark, off: .clear)
}
}
}
return viewModel.on ? colors.on : colors.off
}
private func getCheckboxBorderColor(_ viewModel: ModelType) -> UIColor { private var checkboxBorderColor: CheckboxErrorColorHelper = {
var colors: (on: UIColor, off: UIColor) let helper = CheckboxErrorColorHelper()
if viewModel.disabled { helper.disabled.light.trueColor = VDSColor.interactiveDisabledOnlight
if viewModel.surface == .light { helper.disabled.light.falseColor = VDSColor.interactiveDisabledOnlight
colors = (on: VDSColor.interactiveDisabledOnlight, off: VDSColor.interactiveDisabledOnlight) helper.disabled.dark.trueColor = VDSColor.interactiveDisabledOndark
} else { helper.disabled.dark.falseColor = VDSColor.interactiveDisabledOndark
colors = (on: VDSColor.interactiveDisabledOndark, off: VDSColor.interactiveDisabledOnlight) helper.error.light.trueColor = VDSColor.elementsPrimaryOnlight
} helper.error.light.falseColor = VDSColor.feedbackErrorOnlight
} else { helper.error.dark.trueColor = VDSColor.elementsPrimaryOndark
if viewModel.showError { helper.error.dark.falseColor = VDSColor.feedbackErrorOndark
if viewModel.surface == .light { helper.enabled.light.trueColor = VDSColor.elementsPrimaryOnlight
colors = (on: VDSColor.elementsPrimaryOnlight, off: VDSColor.feedbackErrorOnlight) helper.enabled.light.falseColor = VDSFormControlsColor.borderOnlight
} else { helper.enabled.dark.trueColor = VDSColor.elementsPrimaryOndark
colors = (on: VDSColor.elementsPrimaryOndark, off: VDSColor.feedbackErrorOndark) helper.enabled.dark.falseColor = VDSFormControlsColor.borderOndark
} return helper
} else { }()
if viewModel.surface == .light {
colors = (on: VDSColor.elementsPrimaryOnlight, off: VDSFormControlsColor.borderOnlight)
} else {
colors = (on: VDSColor.elementsPrimaryOndark, off: VDSFormControlsColor.borderOndark)
}
}
}
return viewModel.on ? colors.on : colors.off
}
private func getCheckboxCheckColor(_ viewModel: ModelType) -> UIColor {
var color: UIColor
if surface == .light {
color = VDSColor.elementsPrimaryOndark
} else {
color = VDSColor.elementsPrimaryOnlight
}
return viewModel.on ? color : .clear
}
private var checkboxCheckColor: BinarySurfaceColorHelper<ModelType> = {
let helper = BinarySurfaceColorHelper<ModelType>()
helper.light.trueColor = VDSColor.elementsPrimaryOndark
helper.dark.trueColor = VDSColor.elementsPrimaryOnlight
return helper
}()
private func updateCheckbox(_ viewModel: ModelType) { private func updateCheckbox(_ viewModel: ModelType) {
//get the colors //get the colors
let backgroundColor = getCheckboxBackgroundColor(viewModel) let backgroundColor = checkboxBackgroundColor.getColor(viewModel)
let borderColor = getCheckboxBorderColor(viewModel) let borderColor = checkboxBorderColor.getColor(viewModel)
let checkColor = getCheckboxCheckColor(viewModel) let checkColor = checkboxCheckColor.getColor(viewModel)
if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) { if let shapeLayer = shapeLayer, let sublayers = layer.sublayers, sublayers.contains(shapeLayer) {
shapeLayer.removeFromSuperlayer() shapeLayer.removeFromSuperlayer()
@ -437,4 +412,22 @@ open class CheckboxBase<ModelType: CheckboxModel>: Control<ModelType>, Changable
layoutIfNeeded() layoutIfNeeded()
} }
//--------------------------------------------------
// MARK: - Color Class Helpers
//--------------------------------------------------
private class CheckboxErrorColorHelper: BinaryModelColorHelper<ModelType> {
public let error = BinarySurfaceColorHelper<ModelType>()
override func getColor(_ viewModel: ModelType) -> UIColor {
//only show error is enabled and showError == true
let showErrorColor = !viewModel.disabled && viewModel.showError
if showErrorColor {
return error.getColor(viewModel)
} else {
return super.getColor(viewModel)
}
}
}
} }

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
import UIKit import UIKit
public protocol CheckboxModel: FormFieldable, Errorable, DataTrackable, Accessable, Surfaceable, Disabling, Initable { public protocol CheckboxModel: FormFieldable, Errorable, DataTrackable, Accessable, Surfaceable, Disabling, Initable, BinaryColorable {
var id: String? { get set } var id: String? { get set }
var on: Bool { get set } var on: Bool { get set }
var labelText: String? { get set } var labelText: String? { get set }
@ -18,6 +18,8 @@ public protocol CheckboxModel: FormFieldable, Errorable, DataTrackable, Accessab
} }
extension CheckboxModel { extension CheckboxModel {
public var userTrueColor: Bool { return on }
public var fontCategory: FontCategory { public var fontCategory: FontCategory {
get { return .body } get { return .body }
set { return } set { return }