Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Introduce MGLConfigurationManager
Browse files Browse the repository at this point in the history
This commit renames MGLAccountManager to MGLConfigurationManager
since the name "account" was too specific. In addition, a new
setting was added to the configuration manager to allow a user
to specify a desired `activityType` to be used by location manager
instances in the library. This was added using existing conventions
so that the value can be specified by the API or in the Info.plist
in the client application. It's worth noting that, for backwards
compatibility reasons, MGLAccountManager was kept around
with deprecation warnings and just serves as a wrapper for
MGLConfigurationManager.

Finally, a few unrelated changes were made to clean up an unused
property in the MGLConfigurationManager_Private header and to
make constants for string values used in MGLConfigurationManager
(what was MGLAccountManager).
  • Loading branch information
boundsj committed Aug 22, 2015
1 parent 7e5e096 commit 15b2c60
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 103 deletions.
6 changes: 3 additions & 3 deletions gyp/platform-ios.gypi
Expand Up @@ -25,9 +25,9 @@
'../platform/ios/MGLMapView.mm',
'../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/MGLConfigurationManager.h',
'../platform/ios/MGLConfigurationManager_Private.h',
'../platform/ios/MGLConfigurationManager.m',
'../include/mbgl/ios/MGLAnnotation.h',
'../include/mbgl/ios/MGLUserLocation.h',
'../platform/ios/MGLUserLocation_Private.h',
Expand Down
14 changes: 7 additions & 7 deletions include/mbgl/ios/MGLAccountManager.h
Expand Up @@ -3,29 +3,29 @@
NS_ASSUME_NONNULL_BEGIN

/** The MGLAccountManager object provides a global way to set a Mapbox API access token, as well as other settings used framework-wide. */
__deprecated
@interface MGLAccountManager : NSObject

/** @name Authorizing Access */

/** Set the Mapbox API access token for the framework.
*
* You can set an access token on MGLAccountManager or on an individual map view. The same token is used throughout the framework.
* You can set an access token on MGLConfigurationManager or on an individual map view. The same token is used throughout the framework.
* @param accessToken The Mapbox API access token. */
+ (void)setAccessToken:(nullable NSString *)accessToken;
+ (void)setAccessToken:(nullable NSString *)accessToken __deprecated_msg("Use + setAccessToken in MGLConfigurationManager");

/** Retreive the Mapbox API access token for the framework.
*
* You can set an access token on MGLAccountManager or on an individual map view. The same token is used throughout the framework.
* You can set an access token on MGLConfigurationManager or on an individual map view. The same token is used throughout the framework.
* @return accessToken The Mapbox API access token. */
+ (nullable NSString *)accessToken;
+ (nullable NSString *)accessToken __deprecated_msg("Use + accessToken in MGLConfigurationManager");

/** @name Providing User Metrics Opt-Out */

+ (void)setMapboxMetricsEnabledSettingShownInApp:(BOOL)showsOptOut __attribute__((unavailable("Set MGLMapboxMetricsEnabledSettingShownInApp in Info.plist.")));

/** Whether in-app user metrics opt-out is configured. If set to the default value of `NO`, a user opt-out preference is expected in a `Settings.bundle` that shows in the application's section within the system Settings app. */
+ (BOOL)mapboxMetricsEnabledSettingShownInApp;
+ (BOOL)mapboxMetricsEnabledSettingShownInApp __deprecated_msg("Use + mapboxMetricsEnabledSettingShownInApp in MGLConfigurationManager");

@end

NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_END
43 changes: 43 additions & 0 deletions include/mbgl/ios/MGLConfigurationManager.h
@@ -0,0 +1,43 @@
#import "MGLTypes.h"
#import <CoreLocation/CoreLocation.h>

NS_ASSUME_NONNULL_BEGIN

/** The MGLConfigurationManager object provides a global way to set a Mapbox API access token, as well as other settings used framework-wide. */
@interface MGLConfigurationManager : NSObject

/** @name Authorizing Access */

/** Set the Mapbox API access token for the framework.
*
* You can set an access token on MGLConfigurationManager or on an individual map view. The same token is used throughout the framework.
* @param accessToken The Mapbox API access token. */
+ (void)setAccessToken:(nullable NSString *)accessToken;

/** Retreive the Mapbox API access token for the framework.
*
* You can set an access token on MGLConfigurationManager or on an individual map view. The same token is used throughout the framework.
* @return accessToken The Mapbox API access token. */
+ (nullable NSString *)accessToken;

/** @name Providing User Metrics Opt-Out */
+ (void)setMapboxMetricsEnabledSettingShownInApp:(BOOL)showsOptOut __attribute__((unavailable("Set MGLMapboxMetricsEnabledSettingShownInApp in Info.plist.")));

/** Whether in-app user metrics opt-out is configured. If set to the default value of `NO`, a user opt-out preference is expected in a `Settings.bundle` that shows in the application's section within the system Settings app. */
+ (BOOL)mapboxMetricsEnabledSettingShownInApp;

/** Set the type of activity associated with location updates for the application.
*
* You can configure the activity type associated with location updates that make the most sense for youre application.
* @param activityType The desired CLActivityType */
+ (void)setActivityType:(CLActivityType)activityType;

/** Retreive the type of activity associated with location updates for the application.
*
* You can configure the activity type associated with location updates that make the most sense for your application.
* @return activityType The configured CLActivityType*/
+ (CLActivityType)activityType;

@end

NS_ASSUME_NONNULL_END
6 changes: 3 additions & 3 deletions include/mbgl/ios/MGLMapView.h
Expand Up @@ -30,20 +30,20 @@ IB_DESIGNABLE
* @param frame The frame for the view, measured in points.
* @return An initialized map view. */
- (instancetype)initWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken __attribute__((unavailable("Use -initWithFrame:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLAccountManager setAccessToken:].")));
- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken __attribute__((unavailable("Use -initWithFrame:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLConfigurationManager setAccessToken:].")));

/** Initializes and returns a newly allocated map view with the specified frame and style URL.
* @param frame The frame for the view, measured in points.
* @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>`). Specify `nil` for the default style.
* @return An initialized map view. */
- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL;
- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(nullable NSURL *)styleURL __attribute__((unavailable("Use -initWithFrame:styleURL:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLAccountManager setAccessToken:].")));
- (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(nullable NSURL *)styleURL __attribute__((unavailable("Use -initWithFrame:styleURL:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLConfigurationManager setAccessToken:].")));

#pragma mark - Authorizing Access

/** @name Authorizing Access */

@property (nonatomic, nullable) NSString *accessToken __attribute__((unavailable("Use +[MGLAccountManager accessToken] and +[MGLAccountManager setAccessToken:].")));
@property (nonatomic, nullable) NSString *accessToken __attribute__((unavailable("Use +[MGLConfigurationManager accessToken] and +[MGLConfigurationManager setAccessToken:].")));

#pragma mark - Managing Constraints

Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/ios/Mapbox.h
@@ -1,4 +1,4 @@
#import "MGLAccountManager.h"
#import "MGLConfigurationManager.h"
#import "MGLAnnotation.h"
#import "MGLAnnotationImage.h"
#import "MGLGeometry.h"
Expand Down
6 changes: 3 additions & 3 deletions ios/app/MBXAppDelegate.m
Expand Up @@ -6,8 +6,8 @@ @implementation MBXAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set access token, unless MGLAccountManager already read it in from Info.plist.
if ( ! [MGLAccountManager accessToken]) {
// Set access token, unless MGLConfigurationManager already read it in from Info.plist.
if ( ! [MGLConfigurationManager accessToken]) {
NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
if (accessToken) {
// Store to preferences so that we can launch the app later on without having to specify
Expand All @@ -20,7 +20,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}
if ( ! accessToken) NSLog(@"No access token set. Mapbox vector tiles won't work.");

[MGLAccountManager setAccessToken:accessToken];
[MGLConfigurationManager setAccessToken:accessToken];
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Expand Down
71 changes: 8 additions & 63 deletions platform/ios/MGLAccountManager.m
@@ -1,73 +1,18 @@
#import "MGLAccountManager_Private.h"
#import "MGLMapboxEvents.h"
#import "NSProcessInfo+MGLAdditions.h"

@interface MGLAccountManager()

@property (atomic) BOOL mapboxMetricsEnabledSettingShownInApp;
@property (atomic) NSString *accessToken;

@end

#import "MGLAccountManager.h"
#import "MGLConfigurationManager.h"

@implementation MGLAccountManager

+ (void)load {
// Read the initial configuration from Info.plist. The shown-in-app setting
// preempts the Settings bundle check in -[MGLMapboxEvents init] triggered
// by setting the access token.
NSBundle *bundle = [NSBundle mainBundle];
NSNumber *shownInAppNumber = [bundle objectForInfoDictionaryKey:@"MGLMapboxMetricsEnabledSettingShownInApp"];
if (shownInAppNumber) {
[MGLAccountManager sharedManager].mapboxMetricsEnabledSettingShownInApp = [shownInAppNumber boolValue];
}
NSString *accessToken = [bundle objectForInfoDictionaryKey:@"MGLMapboxAccessToken"];
if (accessToken.length) {
self.accessToken = accessToken;
}
}

// Can be called from any thread.
//
+ (instancetype) sharedManager {
if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
return nil;
}
static dispatch_once_t onceToken;
static MGLAccountManager *_sharedManager;
void (^setupBlock)() = ^{
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
});
};
if ( ! [[NSThread currentThread] isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
setupBlock();
});
}
else {
setupBlock();
}
return _sharedManager;
+ (void)setAccessToken:(nullable NSString *)accessToken {
[MGLConfigurationManager setAccessToken:accessToken];
}

+ (BOOL) mapboxMetricsEnabledSettingShownInApp {
return [MGLAccountManager sharedManager].mapboxMetricsEnabledSettingShownInApp;
+ (nullable NSString *)accessToken {
return [MGLConfigurationManager accessToken];
}

+ (void) setAccessToken:(NSString *) accessToken {
if ( ! [accessToken length]) return;

[MGLAccountManager sharedManager].accessToken = accessToken;

// Update MGLMapboxEvents
// NOTE: This is (likely) the initial setup of MGLMapboxEvents
[MGLMapboxEvents sharedManager];
+ (BOOL)mapboxMetricsEnabledSettingShownInApp {
return [MGLConfigurationManager mapboxMetricsEnabledSettingShownInApp];
}

+ (NSString *) accessToken {
return [MGLAccountManager sharedManager].accessToken;
}


@end
10 changes: 0 additions & 10 deletions platform/ios/MGLAccountManager_Private.h

This file was deleted.

117 changes: 117 additions & 0 deletions platform/ios/MGLConfigurationManager.m
@@ -0,0 +1,117 @@
#import "MGLConfigurationManager_Private.h"
#import "MGLMapboxEvents.h"
#import "NSProcessInfo+MGLAdditions.h"

static NSString * const kMGLMapboxMetricsEnabledSettingShownInApp = @"MGLMapboxMetricsEnabledSettingShownInApp";
static NSString * const kMGLMapboxAccessToken = @"MGLMapboxAccessToken";
static NSString * const kMGLMapboxActivityType = @"MGLMapboxActivityType";

// Used for parsing options specified in info.plist
static NSString * const kMGLMapboxActivityTypeOther = @"Other";
static NSString * const kMGLMapboxActivityTypeAutomotiveNavigation = @"AutomotiveNavigation";
static NSString * const kMGLMapboxActivityTypeFitness = @"Fitness";
static NSString * const kMGLMapboxActivityTypeOtherNavigation = @"OtherNavigation";

@interface MGLConfigurationManager ()

@property (atomic) BOOL mapboxMetricsEnabledSettingShownInApp;
@property (atomic) NSString *accessToken;
@property (atomic) CLActivityType activityType;

@end


@implementation MGLConfigurationManager

+ (void)load {
// Read the initial configuration from Info.plist. The shown-in-app setting
// preempts the Settings bundle check in -[MGLMapboxEvents init] triggered
// by setting the access token.
NSBundle *bundle = [NSBundle mainBundle];

NSNumber *shownInAppNumber = [bundle objectForInfoDictionaryKey:kMGLMapboxMetricsEnabledSettingShownInApp];
if (shownInAppNumber) {
[MGLConfigurationManager sharedManager].mapboxMetricsEnabledSettingShownInApp = [shownInAppNumber boolValue];
}

NSString *accessToken = [bundle objectForInfoDictionaryKey:kMGLMapboxAccessToken];
if (accessToken.length) {
self.accessToken = accessToken;
}

NSString *activityType = [bundle objectForInfoDictionaryKey:kMGLMapboxActivityType];
if (activityType.length) {
self.activityType = [self activityTypeForBundleStringValue:activityType];
}
}

// Can be called from any thread.
//
+ (instancetype) sharedManager {
if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) {
return nil;
}
static dispatch_once_t onceToken;
static MGLConfigurationManager *_sharedManager;
void (^setupBlock)() = ^{
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
});
};
if ( ! [[NSThread currentThread] isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
setupBlock();
});
}
else {
setupBlock();
}
return _sharedManager;
}

+ (BOOL) mapboxMetricsEnabledSettingShownInApp {
return [MGLConfigurationManager sharedManager].mapboxMetricsEnabledSettingShownInApp;
}

+ (void) setAccessToken:(NSString *) accessToken {
if ( ! [accessToken length]) return;

[MGLConfigurationManager sharedManager].accessToken = accessToken;

// Update MGLMapboxEvents
// NOTE: This is (likely) the initial setup of MGLMapboxEvents
[MGLMapboxEvents sharedManager];
}

+ (NSString *) accessToken {
return [MGLConfigurationManager sharedManager].accessToken;
}

+ (void) setActivityType:(CLActivityType)activityType {
[MGLConfigurationManager sharedManager].activityType = activityType;

// Ensure MGLMapboxEvents configures location manager with new activity type
[MGLMapboxEvents validate];
}

+ (CLActivityType) activityType {
return [MGLConfigurationManager sharedManager].activityType;
}

#pragma mark - Utility

+ (CLActivityType)activityTypeForBundleStringValue:(NSString *)stringValue {
if ([stringValue isEqualToString:kMGLMapboxActivityTypeAutomotiveNavigation]) {
return CLActivityTypeAutomotiveNavigation;
}
if ([stringValue isEqual:kMGLMapboxActivityTypeFitness]) {
return CLActivityTypeFitness;
}
if ([stringValue isEqual:kMGLMapboxActivityTypeOtherNavigation]) {
return CLActivityTypeOtherNavigation;
}
return CLActivityTypeOther;
}


@end
8 changes: 8 additions & 0 deletions platform/ios/MGLConfigurationManager_Private.h
@@ -0,0 +1,8 @@
#import "MGLConfigurationManager.h"

@interface MGLConfigurationManager (Private)

/** Returns the shared instance of the `MGLConfigurationManager` class. */
+ (instancetype)sharedManager;

@end

0 comments on commit 15b2c60

Please sign in to comment.