Observe access token changes

The singleton `MGLAccountManager` wants to be the sole arbiter of the access token, but each instance of `mbgl::Map` (`mbgl::DefaultFileSource` in #1607) has its own copy of the access token. Now `MGLMapView` observes for changes to the `MGLAccountManager`’s access token and synchronizes `mbgl::Map` with it.
This commit is contained in:
Minh Nguyễn
2015-05-20 11:49:59 -07:00
parent a081f1af5b
commit 90a50c0c41
4 changed files with 30 additions and 5 deletions

View File

@@ -27,6 +27,7 @@
'../platform/ios/MGLFileCache.h',
'../platform/ios/MGLFileCache.mm',
'../include/mbgl/ios/MGLAccountManager.h',
'../platform/ios/MGLAccountManager_Private.h',
'../platform/ios/MGLAccountManager.m',
'../include/mbgl/ios/MGLAnnotation.h',
'../include/mbgl/ios/MGLUserLocation.h',

View File

@@ -1,6 +1,6 @@
#import <Foundation/Foundation.h>
#import "MGLAccountManager.h"
#import "MGLAccountManager_Private.h"
#import "MGLMapboxEvents_Private.h"
#import "NSProcessInfo+MGLAdditions.h"

View File

@@ -0,0 +1,10 @@
#import "MGLAccountManager.h"
@interface MGLAccountManager (Private)
/** Returns the shared instance of the `MGLAccountManager` class. */
+ (instancetype)sharedManager;
@property (atomic) NSString *accessToken;
@end

View File

@@ -20,7 +20,7 @@
#import "NSString+MGLAdditions.h"
#import "NSProcessInfo+MGLAdditions.h"
#import "NSException+MGLAdditions.h"
#import "MGLAccountManager.h"
#import "MGLAccountManager_Private.h"
#import "MGLAnnotation.h"
#import "MGLUserLocationAnnotationView.h"
#import "MGLUserLocation_Private.h"
@@ -266,9 +266,12 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
}
_mbglMap->resize(self.bounds.size.width, self.bounds.size.height, _glView.contentScaleFactor);
// mbgl::Map keeps its own copy of the access token.
NSString *accessToken = [MGLAccountManager accessToken];
_mbglMap->setAccessToken(accessToken ? (std::string)[accessToken UTF8String] : "");
// Observe for changes to the global access token (and find out the current one).
[[MGLAccountManager sharedManager] addObserver:self
forKeyPath:@"accessToken"
options:(NSKeyValueObservingOptionInitial |
NSKeyValueObservingOptionNew)
context:NULL];
// Notify map object when network reachability status changes.
[[NSNotificationCenter defaultCenter] addObserver:self
@@ -410,6 +413,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
[_regionChangeDelegateQueue cancelAllOperations];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"];
if (_mbglMap)
{
@@ -1292,6 +1296,16 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
#pragma mark - Properties -
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(__unused void *)context
{
// Synchronize mbgl::Map’s access token with the global one in MGLAccountManager.
if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager])
{
NSString *accessToken = change[NSKeyValueChangeNewKey];
_mbglMap->setAccessToken(accessToken ? (std::string)[accessToken UTF8String] : "");
}
}
+ (NSSet *)keyPathsForValuesAffectingZoomEnabled
{
return [NSSet setWithObject:@"allowsZooming"];