From dcc0fbc03908ebd8bc3051570744533688266d0f Mon Sep 17 00:00:00 2001 From: Jason Wray Date: Tue, 25 Oct 2016 16:35:38 -0700 Subject: [PATCH 1/2] [ios] Add country label language toggle demo - Select best language based on locale setting and our support. --- platform/ios/app/MBXViewController.m | 75 +++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 74f8b5f28a1..a5503f38ed2 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -70,6 +70,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsRuntimeStylingRows) { MBXSettingsRuntimeStylingUpdateGeoJSONSourceFeatures, MBXSettingsRuntimeStylingVectorSource, MBXSettingsRuntimeStylingRasterSource, + MBXSettingsRuntimeStylingCountryLabels, }; typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) { @@ -105,6 +106,7 @@ @interface MBXViewController () *labelLayers = @[ + @"country-label-lg", + @"country-label-md", + @"country-label-sm", + ]; + [self styleLabelLanguageForLayersNamed:labelLayers]; +} + +- (void)styleLabelLanguageForLayersNamed:(NSArray *)layers +{ + _usingLocaleBasedCountryLabels = !_usingLocaleBasedCountryLabels; + NSString *bestLanguageForUser = [NSString stringWithFormat:@"{name_%@}", [self bestLanguageForUser]]; + NSString *language = _usingLocaleBasedCountryLabels ? bestLanguageForUser : @"{name}"; + + for (NSString *layerName in layers) { + MGLSymbolStyleLayer *layer = (MGLSymbolStyleLayer *)[self.mapView.style layerWithIdentifier:layerName]; + + if ([layer isKindOfClass:[MGLSymbolStyleLayer class]]) { + if ([layer.textField isKindOfClass:[MGLStyleConstantValue class]]) { + MGLStyleConstantValue *label = (MGLStyleConstantValue *)layer.textField; + if ([label.rawValue hasPrefix:@"{name"]) { + layer.textField = [MGLStyleValue valueWithRawValue:language]; + } + } else if ([layer.textField isKindOfClass:[MGLStyleFunction class]]) { + MGLStyleFunction *function = (MGLStyleFunction *)layer.textField; + [function.stops enumerateKeysAndObjectsUsingBlock:^(id zoomLevel, id stop, BOOL *done) { + if ([stop isKindOfClass:[MGLStyleConstantValue class]]) { + MGLStyleConstantValue *label = (MGLStyleConstantValue *)stop; + if ([label.rawValue hasPrefix:@"{name"]) { + [function.stops setValue:[MGLStyleValue valueWithRawValue:language] forKey:zoomLevel]; + } + } + }]; + layer.textField = function; + } + } else { + NSLog(@"%@ is not a symbol style layer", layerName); + } + } +} + +- (NSString *)bestLanguageForUser +{ + NSArray *supportedLanguages = @[ @"en", @"es", @"fr", @"de", @"ru", @"zh" ]; + NSArray *preferredLanguages = [NSLocale preferredLanguages]; + NSString *bestLanguage; + + for (NSString *language in preferredLanguages) { + NSString *thisLanguage = [NSLocale localeWithLocaleIdentifier:language].languageCode; + if ([supportedLanguages containsObject:thisLanguage]) { + bestLanguage = thisLanguage; + break; + } + } + + return bestLanguage ?: @"en"; +} + - (IBAction)startWorldTour { _isTouringWorld = YES; @@ -1443,4 +1508,12 @@ - (void)mapView:(MGLMapView *)mapView tapOnCalloutForAnnotation:(id Date: Fri, 28 Oct 2016 14:43:25 -0700 Subject: [PATCH 2/2] [ios] Do iosapp token handling in viewDidLoad Doing this work in viewDidAppear meant that it happened every time the modal debug settings view disappeared (and the map view reappeared). --- platform/ios/app/MBXViewController.m | 5 ----- 1 file changed, 5 deletions(-) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index a5503f38ed2..e8a78abff10 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -140,12 +140,7 @@ - (void)viewDidLoad [self restoreState:nil]; self.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"]; -} -- (void)viewDidAppear:(BOOL)animated -{ - [super viewDidAppear:animated]; - if ([MGLAccountManager accessToken].length) { self.styleIndex = -1;