From 58d695f6d31769437007e918fceb41a49b377001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Sat, 9 May 2015 01:28:24 -0700 Subject: [PATCH] Respond to opt-out setting change immediately As soon as the user returns to the host app after changing the opt-out preference in Settings, `MGLMapboxEvents` responds by pausing or resuming. Also moved the IB designables agent check to occur only once, at launch, since the process name is not expected to change mid-session. --- platform/ios/MGLMapboxEvents.m | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 310283c5c..e1c84efe6 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -170,7 +170,9 @@ const NSTimeInterval MGLFlushInterval = 60; @end -@implementation MGLMapboxEvents +@implementation MGLMapboxEvents { + id _userDefaultsObserver; +} + (void)initialize { if (self == [MGLMapboxEvents class]) { @@ -197,8 +199,7 @@ const NSTimeInterval MGLFlushInterval = 60; } + (BOOL)isEnabled { - return ( ! NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent && - [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsEnabled"] && + return ([[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsEnabled"] && [[NSUserDefaults standardUserDefaults] integerForKey:@"MGLMapboxAccountType"] == 0); } @@ -231,7 +232,6 @@ const NSTimeInterval MGLFlushInterval = 60; _geoTrustCert = [NSData dataWithContentsOfFile:cerPath]; } - cerPath = nil; cerPath = [resourceBundle pathForResource:@"api_mapbox_com-digicert" ofType:@"der"]; if (cerPath != nil) { _digicertCert = [NSData dataWithContentsOfFile:cerPath]; @@ -252,6 +252,21 @@ const NSTimeInterval MGLFlushInterval = 60; // Enable Battery Monitoring [UIDevice currentDevice].batteryMonitoringEnabled = YES; + + __weak MGLMapboxEvents *weakSelf = self; + _userDefaultsObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSUserDefaultsDidChangeNotification + object:nil + queue:[NSOperationQueue mainQueue] + usingBlock: + ^(NSNotification *notification) { + MGLMapboxEvents *strongSelf = weakSelf; + BOOL enabledInSettings = [[strongSelf class] isEnabled]; + if (strongSelf.paused && enabledInSettings) { + [strongSelf resumeMetricsCollection]; + } else if (!strongSelf.paused && !enabledInSettings) { + [strongSelf pauseMetricsCollection]; + } + }]; } return self; } @@ -263,6 +278,10 @@ const NSTimeInterval MGLFlushInterval = 60; static dispatch_once_t onceToken; static MGLMapboxEvents *_sharedManager; dispatch_once(&onceToken, ^{ + if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { + return; + } + void (^setupBlock)() = ^{ _sharedManager = [[self alloc] init]; }; @@ -279,6 +298,7 @@ const NSTimeInterval MGLFlushInterval = 60; } - (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:_userDefaultsObserver]; [self pauseMetricsCollection]; }