Skip to content

Commit

Permalink
Refactor light and dark SVGs to make use of a color shceme
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjcampbell committed Aug 5, 2020
1 parent 47eb6d0 commit 11d895f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 64 deletions.
8 changes: 4 additions & 4 deletions Afterpay.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -36,7 +36,7 @@

/* Begin PBXFileReference section */
6615F99A24D14620005036F1 /* SVG.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVG.swift; sourceTree = "<group>"; };
661B233124DA8EE70010EBCD /* BadgeStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BadgeStyle.swift; sourceTree = "<group>"; };
661B233124DA8EE70010EBCD /* ColorScheme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorScheme.swift; sourceTree = "<group>"; };
662A3AEC24A999A500EFD826 /* CheckoutResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckoutResult.swift; sourceTree = "<group>"; };
6632E0AE248A0171007F0BD9 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
6635B95E24CAA9F000EBB3A6 /* ConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -98,8 +98,8 @@
661B233024DA87EA0010EBCD /* Views */ = {
isa = PBXGroup;
children = (
661B233124DA8EE70010EBCD /* BadgeStyle.swift */,
66EE378624D39FC50029BF42 /* BadgeView.swift */,
661B233124DA8EE70010EBCD /* ColorScheme.swift */,
66483F3A24D7A164000BE6B5 /* PriceBreakdownView.swift */,
);
path = Views;
Expand Down Expand Up @@ -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 */,
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/Components/ComponentsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
Expand Down
5 changes: 5 additions & 0 deletions Sources/Afterpay/SVG/SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,9 @@ struct SVG: Equatable {
)

}

struct SVGPair {
let lightSVG: SVG
let darkSVG: SVG
}
// swiftlint:enable line_length
16 changes: 5 additions & 11 deletions Sources/Afterpay/SVG/SVGView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
}

Expand Down
29 changes: 0 additions & 29 deletions Sources/Afterpay/Views/BadgeStyle.swift

This file was deleted.

18 changes: 5 additions & 13 deletions Sources/Afterpay/Views/BadgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,19 @@ 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)

sharedInit()
}

required init?(coder: NSCoder) {
self.lightStyle = .whiteOnBlack
self.darkStyle = .blackOnWhite
self.colorScheme = .static(.blackOnMint)

super.init(coder: coder)

Expand All @@ -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)

Expand Down
56 changes: 56 additions & 0 deletions Sources/Afterpay/Views/ColorScheme.swift
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 11 additions & 6 deletions Sources/Afterpay/Views/PriceBreakdownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 11d895f

Please sign in to comment.