Skip to content

Commit

Permalink
Fix telemetry opt-out through attribution dialog (better fix)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Yeshkunov committed Oct 13, 2021
1 parent d84adc3 commit 17324af
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Mapbox welcomes participation and contributions from everyone.
* Make `PointAnnotation.Image`'s fields public. ([#753](https://github.com/mapbox/mapbox-maps-ios/pull/753))
* Passing an unsupported locale into `Style.localizeLabels(into:forLayerIds:)` throws an error instead of crashing. ([#752](https://github.com/mapbox/mapbox-maps-ios/pull/752))
* Set `MapboxMap` flags during gestures and animations. ([#754](https://github.com/mapbox/mapbox-maps-ios/pull/754))
* Fix telemetry opt-out through attribution dialog (better fix). ([#758](https://github.com/mapbox/mapbox-maps-ios/pull/758))

## 10.0.0 - October 6, 2021

Expand Down
22 changes: 19 additions & 3 deletions Sources/MapboxMaps/Foundation/Events/EventsManager.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import UIKit
import MapboxMobileEvents

extension UserDefaults {
// dynamic var name have to be the same as corresponding key in UserDefaults
// to make KVO observing work properly
@objc dynamic var MGLMapboxMetricsEnabled: Bool {
get {
return bool(forKey: #keyPath(MGLMapboxMetricsEnabled))
}
set {
set(newValue, forKey: #keyPath(MGLMapboxMetricsEnabled))
}
}
}

internal class EventsManager: EventsListener {
private enum Constants {
static let MGLAPIClientUserAgentBase = "mapbox-maps-ios"
}

var telemetry: TelemetryProtocol!
private var metricsEnabledObservation: NSKeyValueObservation?

init(accessToken: String) {
let sdkVersion = "10.0.0"
Expand All @@ -16,10 +30,12 @@ internal class EventsManager: EventsListener {
userAgentBase: Constants.MGLAPIClientUserAgentBase,
hostSDKVersion: sdkVersion)
mmeEventsManager.skuId = "00"
}

init(with telemetry: TelemetryProtocol?) {
self.telemetry = telemetry
metricsEnabledObservation = UserDefaults.standard.observe(\.MGLMapboxMetricsEnabled, options: [.new, .initial]) { _, change in
guard let newValue = change.newValue else { return }
UserDefaults.mme_configuration().mme_isCollectionEnabled = newValue
MMEEventsManager.shared().pauseOrResumeMetricsCollectionIfRequired()
}
}

func push(event: EventType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class InfoButtonOrnament: UIView {
}

internal var isMetricsEnabled: Bool {
return UserDefaults.standard.bool(forKey: Ornaments.metricsEnabledKey)
return UserDefaults.standard.MGLMapboxMetricsEnabled
}

internal weak var delegate: InfoButtonOrnamentDelegate?
Expand Down
1 change: 0 additions & 1 deletion Sources/MapboxMaps/Ornaments/OrnamentsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public enum OrnamentVisibility: String, Equatable {

internal struct Ornaments {
static let localizableTableName = "OrnamentsLocalizable"
static let metricsEnabledKey = "MGLMapboxMetricsEnabled"
static let telemetryURL = "https://www.mapbox.com/telemetry/"
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/MapboxMaps/Style/AttributionDialogManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ internal class AttributionDialogManager {

internal var isMetricsEnabled: Bool {
get {
UserDefaults.standard.bool(forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled
}
set {
UserDefaults.standard.set(newValue, forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled = newValue
}
}

Expand Down
27 changes: 27 additions & 0 deletions Tests/MapboxMapsTests/Foundation/Events/EventsManagerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import XCTest
@testable import MapboxMaps

final class EventsManagerTests: XCTestCase {

let eventsManager = EventsManager(accessToken: "empty")

override class func setUp() {
UserDefaults.mme_configuration().set(true, forKey: "MMECollectionEnabledInSimulator")
}

func testUserDefaultsDynamicProperty() {
UserDefaults.standard.MGLMapboxMetricsEnabled = true
XCTAssertEqual(UserDefaults.standard.bool(forKey: "MGLMapboxMetricsEnabled"), true)

UserDefaults.standard.MGLMapboxMetricsEnabled = false
XCTAssertEqual(UserDefaults.standard.bool(forKey: "MGLMapboxMetricsEnabled"), false)
}

func testMGLMapboxMetricsEqualsMMECollectionEnabled() {
UserDefaults.standard.MGLMapboxMetricsEnabled = true
XCTAssertEqual(UserDefaults.mme_configuration().mme_isCollectionEnabled, true)

UserDefaults.standard.MGLMapboxMetricsEnabled = false
XCTAssertEqual(UserDefaults.mme_configuration().mme_isCollectionEnabled, false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InfoButtonOrnamentTests: XCTestCase {
infoButton.delegate = attributionDialogManager

parentViewController.view.addSubview(infoButton)
UserDefaults.standard.set(true, forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled = true
infoButton.infoTapped()

var infoAlert = try XCTUnwrap(parentViewController.currentAlert, "The info alert controller could not be found.")
Expand Down Expand Up @@ -70,7 +70,7 @@ class InfoButtonOrnamentTests: XCTestCase {
}

func testTelemetryOptIn() throws {
UserDefaults.standard.set(false, forKey: Ornaments.metricsEnabledKey)
UserDefaults.standard.MGLMapboxMetricsEnabled = false
let infoButton = InfoButtonOrnament()
infoButton.delegate = attributionDialogManager

Expand Down

0 comments on commit 17324af

Please sign in to comment.