diff --git a/Sources/Amplitude/Configuration.swift b/Sources/Amplitude/Configuration.swift index 71aa286..65c568c 100644 --- a/Sources/Amplitude/Configuration.swift +++ b/Sources/Amplitude/Configuration.swift @@ -33,9 +33,13 @@ public class Configuration { public var identifyBatchIntervalMillis: Int public internal(set) var migrateLegacyData: Bool @available(*, deprecated, renamed: "autocapture", message: "Please use `autocapture` instead.") - /// The SDK no longer tracks changes to the defaultTracking options after initialization. - public var defaultTracking: DefaultTrackingOptions = DefaultTrackingOptions() { - didSet { autocapture = defaultTracking.autocaptureOptions } + public lazy var defaultTracking: DefaultTrackingOptions = { + DefaultTrackingOptions(delegate: self) + }() { + didSet { + defaultTracking.delegate = self + autocapture = defaultTracking.autocaptureOptions + } } public internal(set) var autocapture: AutocaptureOptions public var offline: Bool? @@ -178,3 +182,10 @@ public class Configuration { return Configuration.getNormalizeInstanceName(self.instanceName) } } + +extension Configuration: DefaultTrackingOptionsDelegate { + @available(*, deprecated) + func didChangeOptions(options: DefaultTrackingOptions) { + autocapture = options.autocaptureOptions + } +} diff --git a/Sources/Amplitude/DefaultTrackingOptions.swift b/Sources/Amplitude/DefaultTrackingOptions.swift index d128b71..bf5a014 100644 --- a/Sources/Amplitude/DefaultTrackingOptions.swift +++ b/Sources/Amplitude/DefaultTrackingOptions.swift @@ -1,5 +1,10 @@ import Foundation +protocol DefaultTrackingOptionsDelegate: AnyObject { + @available(*, deprecated) + func didChangeOptions(options: DefaultTrackingOptions) +} + @available(*, deprecated, renamed: "AutocaptureOptions", message: "Please use `AutocaptureOptions` instead") public class DefaultTrackingOptions { public static var ALL: DefaultTrackingOptions { @@ -10,54 +15,45 @@ public class DefaultTrackingOptions { } public var sessions: Bool { - get { - autocaptureOptions.contains(.sessions) - } - set { - if newValue { - autocaptureOptions.insert(.sessions) - } else { - autocaptureOptions.remove(.sessions) - } + didSet { + delegate?.didChangeOptions(options: self) } } public var appLifecycles: Bool { - get { - autocaptureOptions.contains(.appLifecycles) - } - set { - if newValue { - autocaptureOptions.insert(.appLifecycles) - } else { - autocaptureOptions.remove(.appLifecycles) - } + didSet { + delegate?.didChangeOptions(options: self) } } public var screenViews: Bool { - get { - autocaptureOptions.contains(.screenViews) - } - set { - if newValue { - autocaptureOptions.insert(.screenViews) - } else { - autocaptureOptions.remove(.screenViews) - } + didSet { + delegate?.didChangeOptions(options: self) } } - var autocaptureOptions: AutocaptureOptions + weak var delegate: DefaultTrackingOptionsDelegate? + + var autocaptureOptions: AutocaptureOptions { + return [ + sessions ? .sessions : [], + appLifecycles ? .appLifecycles : [], + screenViews ? .screenViews : [] + ].reduce(into: []) { $0.formUnion($1) } + } public init( sessions: Bool = true, appLifecycles: Bool = false, screenViews: Bool = false ) { - self.autocaptureOptions = [] self.sessions = sessions self.appLifecycles = appLifecycles self.screenViews = screenViews } + + convenience init(delegate: DefaultTrackingOptionsDelegate) { + self.init() + self.delegate = delegate + } } diff --git a/Tests/AmplitudeTests/AutocaptureOptionsTests.swift b/Tests/AmplitudeTests/AutocaptureOptionsTests.swift index 740837f..5a308c6 100644 --- a/Tests/AmplitudeTests/AutocaptureOptionsTests.swift +++ b/Tests/AmplitudeTests/AutocaptureOptionsTests.swift @@ -18,4 +18,49 @@ final class AutocaptureOptionsTests: XCTestCase { XCTAssertFalse(options.contains(.sessions)) XCTAssertTrue(options.contains(.elementInteractions)) } + + func testDefaultTrackingOptionChangesReflectInAutocapture() { + let configuration = Configuration( + apiKey: "test-api-key" + ) + + XCTAssertTrue(configuration.autocapture.contains(.sessions)) + + (configuration as DeprecationWarningDiscardable).setDefaultTrackingOptions(sessions: false, appLifecycles: true, screenViews: true) + + XCTAssertFalse(configuration.autocapture.contains(.sessions)) + XCTAssertTrue(configuration.autocapture.contains(.appLifecycles)) + XCTAssertTrue(configuration.autocapture.contains(.screenViews)) + } + + func testDefaultTrackingInstanceChangeReflectInAutocapture() { + let configuration = Configuration( + apiKey: "test-api-key" + ) + + (configuration as DeprecationWarningDiscardable).setDefaultTracking(sessions: false, appLifecycles: true, screenViews: true) + + XCTAssertFalse(configuration.autocapture.contains(.sessions)) + XCTAssertTrue(configuration.autocapture.contains(.appLifecycles)) + XCTAssertTrue(configuration.autocapture.contains(.screenViews)) + } +} + +private protocol DeprecationWarningDiscardable { + func setDefaultTracking(sessions: Bool, appLifecycles: Bool, screenViews: Bool) + func setDefaultTrackingOptions(sessions: Bool, appLifecycles: Bool, screenViews: Bool) +} + +extension Configuration: DeprecationWarningDiscardable { + @available(*, deprecated) + func setDefaultTracking(sessions: Bool, appLifecycles: Bool, screenViews: Bool) { + defaultTracking = DefaultTrackingOptions(sessions: sessions, appLifecycles: appLifecycles, screenViews: screenViews) + } + + @available(*, deprecated) + func setDefaultTrackingOptions(sessions: Bool, appLifecycles: Bool, screenViews: Bool) { + defaultTracking.sessions = sessions + defaultTracking.appLifecycles = appLifecycles + defaultTracking.screenViews = screenViews + } }