diff --git a/Sources/Afterpay/Afterpay.swift b/Sources/Afterpay/Afterpay.swift index 2b834f31..ff8b8906 100644 --- a/Sources/Afterpay/Afterpay.swift +++ b/Sources/Afterpay/Afterpay.swift @@ -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? @@ -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) } diff --git a/Sources/Afterpay/Views/SVGView.swift b/Sources/Afterpay/Views/SVGView.swift index aed0f576..68e86b37 100644 --- a/Sources/Afterpay/Views/SVGView.swift +++ b/Sources/Afterpay/Views/SVGView.swift @@ -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 @@ -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! @@ -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 @@ -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() } }