diff --git a/Afterpay.xcodeproj/project.pbxproj b/Afterpay.xcodeproj/project.pbxproj index 26afb1d3..2883feee 100644 --- a/Afterpay.xcodeproj/project.pbxproj +++ b/Afterpay.xcodeproj/project.pbxproj @@ -8,7 +8,7 @@ /* Begin PBXBuildFile section */ 6615F99B24D14620005036F1 /* SVG.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6615F99A24D14620005036F1 /* SVG.swift */; }; - 661B233224DA8EE70010EBCD /* BadgeStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 661B233124DA8EE70010EBCD /* BadgeStyle.swift */; }; + 661B233224DA8EE70010EBCD /* ColorScheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 661B233124DA8EE70010EBCD /* ColorScheme.swift */; }; 662A3AED24A999A500EFD826 /* CheckoutResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 662A3AEC24A999A500EFD826 /* CheckoutResult.swift */; }; 6635B95F24CAA9F000EBB3A6 /* ConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6635B95E24CAA9F000EBB3A6 /* ConfigurationTests.swift */; }; 6640C15224D8E1A700F7F4CC /* Macaw in Frameworks */ = {isa = PBXBuildFile; productRef = 6640C15124D8E1A700F7F4CC /* Macaw */; }; @@ -36,7 +36,7 @@ /* Begin PBXFileReference section */ 6615F99A24D14620005036F1 /* SVG.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVG.swift; sourceTree = ""; }; - 661B233124DA8EE70010EBCD /* BadgeStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BadgeStyle.swift; sourceTree = ""; }; + 661B233124DA8EE70010EBCD /* ColorScheme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorScheme.swift; sourceTree = ""; }; 662A3AEC24A999A500EFD826 /* CheckoutResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckoutResult.swift; sourceTree = ""; }; 6632E0AE248A0171007F0BD9 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = ""; }; 6635B95E24CAA9F000EBB3A6 /* ConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationTests.swift; sourceTree = ""; }; @@ -98,8 +98,8 @@ 661B233024DA87EA0010EBCD /* Views */ = { isa = PBXGroup; children = ( - 661B233124DA8EE70010EBCD /* BadgeStyle.swift */, 66EE378624D39FC50029BF42 /* BadgeView.swift */, + 661B233124DA8EE70010EBCD /* ColorScheme.swift */, 66483F3A24D7A164000BE6B5 /* PriceBreakdownView.swift */, ); path = Views; @@ -376,7 +376,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 661B233224DA8EE70010EBCD /* BadgeStyle.swift in Sources */, + 661B233224DA8EE70010EBCD /* ColorScheme.swift in Sources */, 66639B5424D2619200C68558 /* SVGView.swift in Sources */, 6615F99B24D14620005036F1 /* SVG.swift in Sources */, 66483F3B24D7A164000BE6B5 /* PriceBreakdownView.swift in Sources */, diff --git a/Example/Example/Components/ComponentsViewController.swift b/Example/Example/Components/ComponentsViewController.swift index 2f25009e..c6bbe42f 100644 --- a/Example/Example/Components/ComponentsViewController.swift +++ b/Example/Example/Components/ComponentsViewController.swift @@ -78,7 +78,7 @@ final class ComponentsViewController: UIViewController { titleLabel.font = .preferredFont(forTextStyle: .title1) stack.addArrangedSubview(titleLabel) - let badge = BadgeView(style: .blackOnMint) + let badge = BadgeView() badge.widthAnchor.constraint(equalToConstant: 64).isActive = true let badgeStack = UIStackView(arrangedSubviews: [badge, UIView()]) diff --git a/Sources/Afterpay/SVG/SVG.swift b/Sources/Afterpay/SVG/SVG.swift index b9d53565..e38f48d5 100644 --- a/Sources/Afterpay/SVG/SVG.swift +++ b/Sources/Afterpay/SVG/SVG.swift @@ -131,4 +131,9 @@ struct SVG: Equatable { ) } + +struct SVGPair { + let lightSVG: SVG + let darkSVG: SVG +} // swiftlint:enable line_length diff --git a/Sources/Afterpay/SVG/SVGView.swift b/Sources/Afterpay/SVG/SVGView.swift index 926b58fe..03c801c7 100644 --- a/Sources/Afterpay/SVG/SVGView.swift +++ b/Sources/Afterpay/SVG/SVGView.swift @@ -18,16 +18,10 @@ final class SVGView: Macaw.SVGView { var svg: SVG { svg(for: traitCollection) } - private let lightSVG: SVG - private let darkSVG: SVG + private let svgPair: SVGPair - convenience init(svg: SVG) { - self.init(lightSVG: svg, darkSVG: svg) - } - - init(lightSVG: SVG, darkSVG: SVG) { - self.lightSVG = lightSVG - self.darkSVG = darkSVG + init(svgPair: SVGPair) { + self.svgPair = svgPair super.init(node: Group(), frame: .zero) @@ -54,11 +48,11 @@ final class SVGView: Macaw.SVGView { private func svg(for traitCollection: UITraitCollection) -> SVG { switch traitCollection.userInterfaceStyle { case .dark: - return darkSVG + return svgPair.darkSVG case .light, .unspecified: fallthrough @unknown default: - return lightSVG + return svgPair.lightSVG } } diff --git a/Sources/Afterpay/Views/BadgeStyle.swift b/Sources/Afterpay/Views/BadgeStyle.swift deleted file mode 100644 index 3ca7d203..00000000 --- a/Sources/Afterpay/Views/BadgeStyle.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// BadgeStyle.swift -// Afterpay -// -// Created by Adam Campbell on 5/8/20. -// Copyright © 2020 Afterpay. All rights reserved. -// - -import Foundation - -public enum BadgeStyle { - case blackOnMint - case mintOnBlack - case whiteOnBlack - case blackOnWhite - - var svg: SVG { - switch self { - case .blackOnMint: - return .badgeBlackOnMint - case .mintOnBlack: - return .badgeMintOnBlack - case .whiteOnBlack: - return .badgeWhiteOnBlack - case .blackOnWhite: - return .badgeBlackOnWhite - } - } -} diff --git a/Sources/Afterpay/Views/BadgeView.swift b/Sources/Afterpay/Views/BadgeView.swift index 236583b3..622cdd0a 100644 --- a/Sources/Afterpay/Views/BadgeView.swift +++ b/Sources/Afterpay/Views/BadgeView.swift @@ -11,18 +11,11 @@ import UIKit public final class BadgeView: UIView { - private let lightStyle: BadgeStyle - private let darkStyle: BadgeStyle - + private let colorScheme: ColorScheme private var svgView: SVGView! - public convenience init(style: BadgeStyle) { - self.init(lightStyle: style, darkStyle: style) - } - - public init(lightStyle: BadgeStyle, darkStyle: BadgeStyle) { - self.lightStyle = lightStyle - self.darkStyle = darkStyle + public init(colorScheme: ColorScheme = .static(.blackOnMint)) { + self.colorScheme = colorScheme super.init(frame: .zero) @@ -30,8 +23,7 @@ public final class BadgeView: UIView { } required init?(coder: NSCoder) { - self.lightStyle = .whiteOnBlack - self.darkStyle = .blackOnWhite + self.colorScheme = .static(.blackOnMint) super.init(coder: coder) @@ -45,7 +37,7 @@ public final class BadgeView: UIView { accessibilityLabel = "after pay" // SVG Layout - svgView = SVGView(lightSVG: lightStyle.svg, darkSVG: darkStyle.svg) + svgView = SVGView(svgPair: colorScheme.badgeSVGPair) addSubview(svgView) diff --git a/Sources/Afterpay/Views/ColorScheme.swift b/Sources/Afterpay/Views/ColorScheme.swift new file mode 100644 index 00000000..780ea233 --- /dev/null +++ b/Sources/Afterpay/Views/ColorScheme.swift @@ -0,0 +1,56 @@ +// +// BadgeStyle.swift +// Afterpay +// +// Created by Adam Campbell on 5/8/20. +// Copyright © 2020 Afterpay. All rights reserved. +// + +import Foundation + +public enum ColorScheme { + case `static`(ColorPalette) + case dynamic(lightPalette: ColorPalette, darkPalette: ColorPalette) + + var lightPalette: ColorPalette { + switch self { + case .static(let palette): + return palette + case .dynamic(let lightPalette, _): + return lightPalette + } + } + + var darkPalette: ColorPalette { + switch self { + case .static(let palette): + return palette + case .dynamic(_, let darkPalette): + return darkPalette + } + } + + var badgeSVGPair: SVGPair { + let svg: (ColorPalette) -> SVG = { palette in + switch palette { + case .blackOnMint: + return .badgeBlackOnMint + case .mintOnBlack: + return .badgeMintOnBlack + case .whiteOnBlack: + return .badgeWhiteOnBlack + case .blackOnWhite: + return .badgeBlackOnWhite + } + } + + return SVGPair(lightSVG: svg(lightPalette), darkSVG: svg(darkPalette)) + } +} + +public enum ColorPalette { + case blackOnMint + case mintOnBlack + case whiteOnBlack + case blackOnWhite +} diff --git a/Sources/Afterpay/Views/PriceBreakdownView.swift b/Sources/Afterpay/Views/PriceBreakdownView.swift index bf7f5adc..9d3099d2 100644 --- a/Sources/Afterpay/Views/PriceBreakdownView.swift +++ b/Sources/Afterpay/Views/PriceBreakdownView.swift @@ -12,14 +12,19 @@ import UIKit public final class PriceBreakdownView: UIView { private let label = UILabel() + private let colorScheme: ColorScheme + + public init(colorScheme: ColorScheme = .static(.blackOnMint)) { + self.colorScheme = colorScheme - public init() { super.init(frame: .zero) sharedInit() } required init?(coder: NSCoder) { + self.colorScheme = .static(.blackOnMint) + super.init(coder: coder) sharedInit() @@ -48,8 +53,8 @@ public final class PriceBreakdownView: UIView { } private func updateAttributedText() { - let svgView = SVGView(svg: .badgeBlackOnMint) - let svg = svgView.svg + let badgeSVGView = SVGView(svgPair: colorScheme.badgeSVGPair) + let svg = badgeSVGView.svg let font: UIFont = .preferredFont(forTextStyle: .body) let fontHeight = font.ascender - font.descender @@ -58,11 +63,11 @@ public final class PriceBreakdownView: UIView { let width = widthFittingFont > svg.minimumWidth ? widthFittingFont : svg.minimumWidth let size = CGSize(width: width, height: width * svg.aspectRatio) - svgView.frame = CGRect(origin: .zero, size: size) + badgeSVGView.frame = CGRect(origin: .zero, size: size) - let renderer = UIGraphicsImageRenderer(size: svgView.bounds.size) + let renderer = UIGraphicsImageRenderer(size: badgeSVGView.bounds.size) let image = renderer.image { rendererContext in - svgView.layer.render(in: rendererContext.cgContext) + badgeSVGView.layer.render(in: rendererContext.cgContext) } let attributedString = NSMutableAttributedString(