Skip to content

Commit

Permalink
Merge pull request #119 from afterpay/auto-updating-badge-view
Browse files Browse the repository at this point in the history
Ensure SVGView updates when a change in locale causes a change in the SVG
  • Loading branch information
adamjcampbell authored Oct 15, 2020
2 parents f3a37bf + 0a8b2d4 commit 1f97eea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
7 changes: 6 additions & 1 deletion Sources/Afterpay/Afterpay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public func setAuthenticationChallengeHandler(_ handler: @escaping Authenticatio
let notificationCenter = NotificationCenter()

extension NSNotification.Name {

/// Fires when the Afterpay configuration is updated with the previous configuration
static let configurationUpdated = NSNotification.Name("ConfigurationUpdated")

}

private var configuration: Configuration?
Expand All @@ -85,7 +88,9 @@ func getConfiguration() -> Configuration? {
/// Sets the Configuration object to use for rendering UI Components in the Afterpay SDK.
/// - Parameter configuration: The configuration or nil to clear.
public func setConfiguration(_ configuration: Configuration?) {
let previousConfiguration = Afterpay.configuration

Afterpay.configuration = configuration

notificationCenter.post(name: .configurationUpdated, object: configuration)
notificationCenter.post(name: .configurationUpdated, object: previousConfiguration)
}
35 changes: 28 additions & 7 deletions Sources/Afterpay/Views/SVGView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ import UIKit

final class SVGView: Macaw.SVGView {

var svg: SVG { svg(for: traitCollection) }
var svg: SVG {
svgConfiguration.svg(localizedFor: locale, withTraits: traitCollection)
}

var svgConfiguration: SVGConfiguration {
didSet { svgDidChange() }
}

private var locale: Locale {
getConfiguration()?.locale ?? Locales.unitedStates
}

init(svgConfiguration: SVGConfiguration) {
self.svgConfiguration = svgConfiguration

Expand All @@ -31,6 +37,10 @@ final class SVGView: Macaw.SVGView {
backgroundColor = .clear
translatesAutoresizingMaskIntoConstraints = false
setupConstraints()

let selector = #selector(configurationDidChange)
let name: NSNotification.Name = .configurationUpdated
notificationCenter.addObserver(self, selector: selector, name: name, object: nil)
}

private var aspectRatioConstraint: NSLayoutConstraint!
Expand All @@ -47,11 +57,6 @@ final class SVGView: Macaw.SVGView {
NSLayoutConstraint.activate([aspectRatioConstraint, minimumWidthConstraint])
}

private func svg(for traitCollection: UITraitCollection) -> SVG {
let locale = getConfiguration()?.locale ?? Locales.unitedStates
return svgConfiguration.svg(localizedFor: locale, withTraits: traitCollection)
}

private func svgDidChange() {
node = svg.node

Expand All @@ -64,10 +69,26 @@ final class SVGView: Macaw.SVGView {
}
}

@objc private func configurationDidChange(_ notification: NSNotification) {
let previousLocale = (notification.object as? Configuration)?.locale ?? Locales.unitedStates

let svgForLocale = { [traitCollection, svgConfiguration] locale in
svgConfiguration.svg(localizedFor: locale, withTraits: traitCollection)
}

if svgForLocale(previousLocale) != svgForLocale(locale) {
svgDidChange()
}
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

if previousTraitCollection.map(svg) != svg(for: traitCollection) {
let svgForTraits = { [locale, svgConfiguration] traitCollection in
svgConfiguration.svg(localizedFor: locale, withTraits: traitCollection)
}

if previousTraitCollection.map(svgForTraits) != svgForTraits(traitCollection) {
svgDidChange()
}
}
Expand Down

0 comments on commit 1f97eea

Please sign in to comment.