Skip to content

Commit

Permalink
Ensure SVGView updates when configuration updates occur
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjcampbell committed Oct 15, 2020
1 parent 7027b27 commit 0a8b2d4
Showing 1 changed file with 28 additions and 7 deletions.
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 0a8b2d4

Please sign in to comment.