Created
January 30, 2020 08:31
-
-
Save larryonoff/0c0b2b1d2bbad073524f1309c541ba9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Amplitude_iOS | |
import Foundation | |
import ReactiveSwift | |
import UIKit | |
public final class AmplitudeUserPropertiesUpdater { | |
private enum Constants { | |
static let keyPrefix = "remoteSettings" | |
static let applicationStateKey = "applicationState" | |
} | |
private let amplitude: Amplitude | |
private let notificationCenter: NotificationCenter | |
private let remoteSettings: RemoteConfig | |
private let disposable = SerialDisposable() | |
public static let shared = AmplitudeUserPropertiesUpdater() | |
private init( | |
amplitude: Amplitude = .instance(), | |
notificationCenter: NotificationCenter = .default, | |
remoteSettings: RemoteConfig = .shared | |
) { | |
self.amplitude = amplitude | |
self.notificationCenter = notificationCenter | |
self.remoteSettings = remoteSettings | |
} | |
public func start() { | |
disposable.inner = SignalProducer | |
.merge( | |
notificationsForApplicationState(), | |
notificationsForRemoteSettings() | |
) | |
.startWithValues { [weak self] in self?.update() } | |
} | |
private func notificationsForApplicationState() -> SignalProducer<Void, Never> { | |
SignalProducer | |
.merge( | |
notificationCenter.reactive.notifications( | |
forName: UIApplication.didEnterBackgroundNotification | |
), | |
notificationCenter.reactive.notifications( | |
forName: UIApplication.willEnterForegroundNotification | |
), | |
notificationCenter.reactive.notifications( | |
forName: UIApplication.didBecomeActiveNotification | |
), | |
notificationCenter.reactive.notifications( | |
forName: UIApplication.willResignActiveNotification | |
) | |
) | |
.map { _ in } | |
} | |
private func notificationsForRemoteSettings() -> SignalProducer<Void, Never> { | |
remoteSettings | |
.observer() | |
.skipError() | |
.map { _ in } | |
.producer | |
} | |
private func update() { | |
amplitude.clearUserProperties() | |
var newUserProperties: [String: String] = [:] | |
if let allKeyValues = remoteSettings.allKeyValues { | |
let keysWithValues = allKeyValues | |
.map { ("\(Constants.keyPrefix):\($0.key)", $0.value) } | |
newUserProperties.merge( | |
keysWithValues, | |
uniquingKeysWith: { _, new in new } | |
) | |
} | |
let app = UIApplication.shared | |
newUserProperties[Constants.applicationStateKey] = | |
app.applicationState.description | |
amplitude.setUserProperties(newUserProperties) | |
} | |
} | |
final class AmplitudeAppDelegate: NSObject { | |
private let updater: AmplitudeUserPropertiesUpdater = .shared | |
} | |
// MARK: - UIApplicationDelegate | |
extension AmplitudeAppDelegate: UIApplicationDelegate { | |
func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
) -> Bool { | |
updater.start() | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment