summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-03-30 16:27:28 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-04-03 12:07:53 -0700
commit6a1e7d5707762561e4947e90f94c1ede9e2940cc (patch)
tree068d7685c569332ac4d7cb09d7b383d6500b0bc7
parent999ebb371e98bb9ee5876cfad336426c5abae7a8 (diff)
downloadqtlocation-mapboxgl-6a1e7d5707762561e4947e90f94c1ede9e2940cc.tar.gz
Fixed race condition after setting access token
`Map` should be able to deal with having no access token or JSON even while it’s running. Most of `Map` accounts for this situation, but `reloadStyle()` incorrectly assumes that one or the other is set. This change corrects the assumption in `reloadStyle()`. Also expose the access token and style name to clients as ordinary KVO-compliant properties. Actually, they’re not so ordinary because they’re inspectable! Ref #1070, #1147
-rw-r--r--gyp/platform-ios.gypi3
-rw-r--r--include/mbgl/ios/MGLMapView.h6
-rw-r--r--include/mbgl/map/map.hpp1
-rw-r--r--ios/app/MBXViewController.mm7
-rw-r--r--platform/ios/MGLMapView.mm47
-rw-r--r--platform/ios/NSString+MGLAdditions.h8
-rw-r--r--platform/ios/NSString+MGLAdditions.m10
-rw-r--r--src/mbgl/map/map.cpp6
8 files changed, 66 insertions, 22 deletions
diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi
index 9965db57ac..fc6771d83a 100644
--- a/gyp/platform-ios.gypi
+++ b/gyp/platform-ios.gypi
@@ -30,6 +30,8 @@
'../include/mbgl/ios/MGLMetricsLocationManager.h',
'../platform/ios/MGLMetricsLocationManager.m',
'../include/mbgl/ios/MGLTypes.h',
+ '../platform/ios/NSString+MGLAdditions.h',
+ '../platform/ios/NSString+MGLAdditions.m',
'../platform/ios/vendor/SMCalloutView/SMCalloutView.h',
'../platform/ios/vendor/SMCalloutView/SMCalloutView.m',
],
@@ -50,6 +52,7 @@
'-framework MobileCoreServices',
'-framework QuartzCore',
'-framework SystemConfiguration',
+ '-ObjC',
],
},
diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h
index 17741b1711..354e32cc5a 100644
--- a/include/mbgl/ios/MGLMapView.h
+++ b/include/mbgl/ios/MGLMapView.h
@@ -54,7 +54,7 @@
/** Sets a Mapbox API access token for the map view.
* @param accessToken A Mapbox API token. */
-- (void)setAccessToken:(NSString *)accessToken;
+@property (nonatomic) IBInspectable NSString *accessToken;
#pragma mark - Managing Constraints
@@ -194,11 +194,11 @@
/** Sets the map style to a named, bundled style.
* @param styleName The map style name to use. */
-- (void)useBundledStyleNamed:(NSString *)styleName;
+@property (nonatomic) IBInspectable NSString *styleName;
/** Sets the map style URL to use.
* @param styleURL The map style URL to use. Can be either an HTTP/HTTPS URL or a Mapbox map ID style URL (`mapbox://<user.style>`). */
-- (void)setStyleURL:(NSURL *)styleURL;
+@property (nonatomic) NSURL *styleURL;
#pragma mark - Annotating the Map
diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp
index cdcfa27e42..764ff24ed8 100644
--- a/include/mbgl/map/map.hpp
+++ b/include/mbgl/map/map.hpp
@@ -96,6 +96,7 @@ public:
Duration getDefaultTransitionDuration();
void setStyleURL(const std::string& url);
void setStyleJSON(const std::string& json, const std::string& base = "");
+ std::string getStyleURL() const;
std::string getStyleJSON() const;
// Transition
diff --git a/ios/app/MBXViewController.mm b/ios/app/MBXViewController.mm
index c9973bcb7d..1461439697 100644
--- a/ios/app/MBXViewController.mm
+++ b/ios/app/MBXViewController.mm
@@ -19,7 +19,7 @@ static NSArray *const kStyleNames = @[
@"Hybrid",
];
-static NSString *const kStyleVersion = @"v7";
+static NSString *const kStyleVersion = @"7";
@interface MBXViewController () <UIActionSheetDelegate, MGLMapViewDelegate>
@@ -246,10 +246,7 @@ mbgl::Settings_NSUserDefaults *settings = nullptr;
styleName = [kStyleNames objectAtIndex:index];
}
- [self.mapView useBundledStyleNamed:
- [[[styleName lowercaseString]
- stringByAppendingString:@"-"]
- stringByAppendingString:kStyleVersion]];
+ self.mapView.styleName = [NSString stringWithFormat:@"%@-v%@", styleName.lowercaseString, kStyleVersion];
[titleButton setTitle:styleName forState:UIControlStateNormal];
}
diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm
index d486949175..b6519178d6 100644
--- a/platform/ios/MGLMapView.mm
+++ b/platform/ios/MGLMapView.mm
@@ -15,6 +15,7 @@
#include <mbgl/util/geo.hpp>
#import "MGLTypes.h"
+#import "NSString+MGLAdditions.h"
#import "MGLAnnotation.h"
#import "MGLUserLocationAnnotationView.h"
#import "MGLUserLocation_Private.h"
@@ -43,7 +44,7 @@ const std::string &defaultCacheDatabase() {
static dispatch_once_t loadGLExtensions;
NSString *const MGLDefaultStyleName = @"Emerald";
-NSString *const MGLStyleVersion = @"v7";
+NSString *const MGLStyleVersion = @"7";
NSString *const MGLDefaultStyleMarkerSymbolName = @"default_marker";
const NSTimeInterval MGLAnimationDuration = 0.3;
@@ -106,7 +107,7 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
if (self && [self commonInit])
{
- if (accessToken) [self setAccessToken:accessToken];
+ [self setAccessToken:accessToken];
if (styleJSON || accessToken)
{
@@ -126,8 +127,8 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
if (self && [self commonInit])
{
- if (accessToken) [self setAccessToken:accessToken];
- if (styleName) [self useBundledStyleNamed:styleName];
+ [self setAccessToken:accessToken];
+ if (styleName) [self setStyleName:styleName];
}
return self;
@@ -163,22 +164,22 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
return nil;
}
+- (NSString *)accessToken
+{
+ return @(mbglMap->getAccessToken().c_str()).mgl_stringOrNilIfEmpty;
+}
+
- (void)setAccessToken:(NSString *)accessToken
{
- if (accessToken)
- {
- mbglMap->setAccessToken((std::string)[accessToken UTF8String]);
- [MGLMapboxEvents setToken:accessToken];
- }
+ mbglMap->setAccessToken((std::string)[accessToken UTF8String]);
+ [MGLMapboxEvents setToken:accessToken.mgl_stringOrNilIfEmpty];
}
- (void)setStyleJSON:(NSString *)styleJSON
{
if ( ! styleJSON)
{
- [self useBundledStyleNamed:[[[MGLDefaultStyleName lowercaseString]
- stringByAppendingString:@"-"]
- stringByAppendingString:MGLStyleVersion]];
+ [self setStyleName:[NSString stringWithFormat:@"%@-v%@", MGLDefaultStyleName.lowercaseString, MGLStyleVersion]];
}
else
{
@@ -186,6 +187,12 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
}
}
+- (NSURL *)styleURL
+{
+ NSString *styleURLString = @(mbglMap->getStyleURL().c_str()).mgl_stringOrNilIfEmpty;
+ return styleURLString ? [NSURL URLWithString:styleURLString] : nil;
+}
+
- (void)setStyleURL:(NSURL *)styleURL
{
std::string styleURLString([[styleURL absoluteString] UTF8String]);
@@ -1322,7 +1329,21 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
return [NSArray arrayWithArray:_bundledStyleNames];
}
-- (void)useBundledStyleNamed:(NSString *)styleName
+- (NSString *)styleName
+{
+ NSURL *styleURL = self.styleURL;
+ NSString *styleName;
+ if ([styleURL.scheme isEqualToString:@"asset"])
+ {
+ styleName = styleURL.lastPathComponent.stringByDeletingPathExtension;
+ }
+ else if ([styleURL.scheme isEqualToString:@"mapbox"]) {
+ styleName = styleURL.host;
+ }
+ return styleName.mgl_stringOrNilIfEmpty;
+}
+
+- (void)setStyleName:(NSString *)styleName
{
NSString *hybridStylePrefix = @"hybrid-";
BOOL isHybrid = [styleName hasPrefix:hybridStylePrefix];
diff --git a/platform/ios/NSString+MGLAdditions.h b/platform/ios/NSString+MGLAdditions.h
new file mode 100644
index 0000000000..263cfc8cb7
--- /dev/null
+++ b/platform/ios/NSString+MGLAdditions.h
@@ -0,0 +1,8 @@
+#import <Foundation/Foundation.h>
+
+@interface NSString (MGLAdditions)
+
+/// Returns the receiver if non-empty or \c nil if empty.
+- (NSString *)mgl_stringOrNilIfEmpty;
+
+@end
diff --git a/platform/ios/NSString+MGLAdditions.m b/platform/ios/NSString+MGLAdditions.m
new file mode 100644
index 0000000000..d80c34cef0
--- /dev/null
+++ b/platform/ios/NSString+MGLAdditions.m
@@ -0,0 +1,10 @@
+#import "NSString+MGLAdditions.h"
+
+@implementation NSString (MGLAdditions)
+
+- (NSString *)mgl_stringOrNilIfEmpty
+{
+ return self.length ? self : nil;
+}
+
+@end
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 09e6b08a1e..ac2be3f060 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -396,6 +396,10 @@ void Map::setup() {
painter->setup();
}
+std::string Map::getStyleURL() const {
+ return data->getStyleInfo().url;
+}
+
void Map::setStyleURL(const std::string &url) {
assert(Environment::currentlyOn(ThreadType::Main));
@@ -756,7 +760,7 @@ void Map::reloadStyle() {
Log::Error(Event::Setup, "loading style failed: %s", res.message.c_str());
}
});
- } else {
+ } else if (!styleInfo.json.empty()) {
// We got JSON data directly.
loadStyleJSON(styleInfo.json, styleInfo.base);
}