diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2015-05-20 11:49:59 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2015-05-20 11:49:59 -0700 |
commit | 90a50c0c4150080f725cc6b66909eef58a2a9b4a (patch) | |
tree | ed8a8fbb2ab0802fbc632fd94807032e07c0fdd8 /platform | |
parent | a081f1af5b7c173da5f5c1beaec8c274aeb353de (diff) | |
download | qtlocation-mapboxgl-90a50c0c4150080f725cc6b66909eef58a2a9b4a.tar.gz |
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.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/ios/MGLAccountManager.m | 2 | ||||
-rw-r--r-- | platform/ios/MGLAccountManager_Private.h | 10 | ||||
-rw-r--r-- | platform/ios/MGLMapView.mm | 22 |
3 files changed, 29 insertions, 5 deletions
diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m index 97d7e5e5ec..38ef1f4ca9 100644 --- a/platform/ios/MGLAccountManager.m +++ b/platform/ios/MGLAccountManager.m @@ -1,6 +1,6 @@ #import <Foundation/Foundation.h> -#import "MGLAccountManager.h" +#import "MGLAccountManager_Private.h" #import "MGLMapboxEvents_Private.h" #import "NSProcessInfo+MGLAdditions.h" diff --git a/platform/ios/MGLAccountManager_Private.h b/platform/ios/MGLAccountManager_Private.h new file mode 100644 index 0000000000..3cc05b09f0 --- /dev/null +++ b/platform/ios/MGLAccountManager_Private.h @@ -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 diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index cb89c58d46..997d5734aa 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -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"]; |