diff options
Diffstat (limited to 'platform/ios/app/MBXViewController.m')
-rw-r--r-- | platform/ios/app/MBXViewController.m | 185 |
1 files changed, 92 insertions, 93 deletions
diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 893cee1cdc..44b8ae2bac 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -8,6 +8,8 @@ #import "LimeGreenStyleLayer.h" #import "MBXEmbeddedMapViewController.h" #import "MBXOrnamentsViewController.h" +#import "MBXStateManager.h" +#import "MBXState.h" #import "MBXFrameTimeGraphView.h" @@ -198,17 +200,18 @@ CLLocationCoordinate2D randomWorldCoordinate() { @property (nonatomic) IBOutlet MGLMapView *mapView; +@property (nonatomic) MBXState *currentState; @property (weak, nonatomic) IBOutlet UIButton *hudLabel; @property (weak, nonatomic) IBOutlet MBXFrameTimeGraphView *frameTimeGraphView; @property (nonatomic) NSInteger styleIndex; -@property (nonatomic) BOOL debugLoggingEnabled; @property (nonatomic) BOOL customUserLocationAnnnotationEnabled; @property (nonatomic, getter=isLocalizingLabels) BOOL localizingLabels; @property (nonatomic) BOOL reuseQueueStatsEnabled; -@property (nonatomic) BOOL mapInfoHUDEnabled; @property (nonatomic) BOOL frameTimeGraphEnabled; @property (nonatomic) BOOL shouldLimitCameraChanges; @property (nonatomic) BOOL randomWalk; +@property (nonatomic) BOOL zoomLevelOrnamentEnabled; +@property (nonatomic) BOOL debugLoggingEnabled; @property (nonatomic) NSMutableArray<UIWindow *> *helperWindows; @property (nonatomic) NSMutableArray<UIView *> *contentInsetsOverlays; @@ -227,27 +230,33 @@ CLLocationCoordinate2D randomWorldCoordinate() { #pragma mark - Setup & Teardown -+ (void)initialize -{ - if (self == [MBXViewController class]) - { - [[NSUserDefaults standardUserDefaults] registerDefaults:@{ - @"MBXUserTrackingMode": @(MGLUserTrackingModeNone), - @"MBXShowsUserLocation": @NO, - @"MBXDebug": @NO, - }]; - } -} - - (void)viewDidLoad { [super viewDidLoad]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState:) name:UIApplicationDidEnterBackgroundNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreState:) name:UIApplicationWillEnterForegroundNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveState:) name:UIApplicationWillTerminateNotification object:nil]; + // Keep track of current map state and debug preferences, + // saving and restoring when the application's state changes. + self.currentState = [MBXStateManager sharedManager].currentState; - [self restoreState:nil]; + if (!self.currentState) { + // Create a new state with the below default values + self.currentState = [[MBXState alloc] init]; + + self.mapView.showsUserHeadingIndicator = YES; + self.mapView.showsScale = YES; + self.zoomLevelOrnamentEnabled = NO; + self.frameTimeGraphEnabled = NO; + self.debugLoggingEnabled = YES; + } else { + // Revert to the previously saved state + [self restoreMapState:nil]; + } + + [self updateHUD]; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCurrentMapState:) name:UIApplicationDidEnterBackgroundNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restoreMapState:) name:UIApplicationWillEnterForegroundNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCurrentMapState:) name:UIApplicationWillTerminateNotification object:nil]; if ([MGLAccountManager accessToken].length) { @@ -255,9 +264,6 @@ CLLocationCoordinate2D randomWorldCoordinate() { [self cycleStyles:self]; } - self.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"]; - self.mapView.showsScale = YES; - self.mapView.showsUserHeadingIndicator = YES; self.mapView.experimental_enableFrameRateMeasurement = YES; self.hudLabel.titleLabel.font = [UIFont monospacedDigitSystemFontOfSize:10 weight:UIFontWeightRegular]; @@ -298,53 +304,6 @@ CLLocationCoordinate2D randomWorldCoordinate() { }]; } -- (void)saveState:(__unused NSNotification *)notification -{ - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - NSData *archivedCamera = [NSKeyedArchiver archivedDataWithRootObject:self.mapView.camera]; - [defaults setObject:archivedCamera forKey:@"MBXCamera"]; - [defaults setInteger:self.mapView.userTrackingMode forKey:@"MBXUserTrackingMode"]; - [defaults setBool:self.mapView.showsUserLocation forKey:@"MBXShowsUserLocation"]; - [defaults setInteger:self.mapView.debugMask forKey:@"MBXDebugMask"]; - [defaults setBool:self.mapInfoHUDEnabled forKey:@"MBXShowsZoomLevelHUD"]; - [defaults setBool:self.mapInfoHUDEnabled forKey:@"MBXShowsFrameTimeGraph"]; - [defaults synchronize]; -} - -- (void)restoreState:(__unused NSNotification *)notification -{ - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - NSData *archivedCamera = [defaults objectForKey:@"MBXCamera"]; - MGLMapCamera *camera = archivedCamera ? [NSKeyedUnarchiver unarchiveObjectWithData:archivedCamera] : nil; - if (camera) - { - self.mapView.camera = camera; - } - NSInteger uncheckedTrackingMode = [defaults integerForKey:@"MBXUserTrackingMode"]; - if (uncheckedTrackingMode >= 0 && - (NSUInteger)uncheckedTrackingMode >= MGLUserTrackingModeNone && - (NSUInteger)uncheckedTrackingMode <= MGLUserTrackingModeFollowWithCourse) - { - self.mapView.userTrackingMode = (MGLUserTrackingMode)uncheckedTrackingMode; - } - self.mapView.showsUserLocation = [defaults boolForKey:@"MBXShowsUserLocation"]; - NSInteger uncheckedDebugMask = [defaults integerForKey:@"MBXDebugMask"]; - if (uncheckedDebugMask >= 0) - { - self.mapView.debugMask = (MGLMapDebugMaskOptions)uncheckedDebugMask; - } - if ([defaults boolForKey:@"MBXShowsZoomLevelHUD"]) - { - self.mapInfoHUDEnabled = YES; - [self updateHUD]; - } - if ([defaults boolForKey:@"MBXShowsFrameTimeGraph"]) - { - self.frameTimeGraphEnabled = YES; - self.frameTimeGraphView.hidden = NO; - } -} - - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; @@ -360,8 +319,6 @@ CLLocationCoordinate2D randomWorldCoordinate() { - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - - [self saveState:nil]; } #pragma mark - Debugging Interface @@ -405,20 +362,20 @@ CLLocationCoordinate2D randomWorldCoordinate() { { case MBXSettingsDebugTools: [settingsTitles addObjectsFromArray:@[ - @"Reset Position", - [NSString stringWithFormat:@"%@ Tile Boundaries", + @"Reset position", + [NSString stringWithFormat:@"%@ tile boundaries", (debugMask & MGLMapDebugTileBoundariesMask ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Tile Info", + [NSString stringWithFormat:@"%@ tile info", (debugMask & MGLMapDebugTileInfoMask ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Tile Timestamps", + [NSString stringWithFormat:@"%@ tile timestamps", (debugMask & MGLMapDebugTimestampsMask ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Collision Boxes", + [NSString stringWithFormat:@"%@ collision boxes", (debugMask & MGLMapDebugCollisionBoxesMask ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Overdraw Visualization", + [NSString stringWithFormat:@"%@ overdraw visualization", (debugMask & MGLMapDebugOverdrawVisualizationMask ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Map Info HUD", (_mapInfoHUDEnabled ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Frame Time Graph", (_frameTimeGraphEnabled ? @"Hide" :@"Show")], - [NSString stringWithFormat:@"%@ Reuse Queue Stats", (_reuseQueueStatsEnabled ? @"Hide" :@"Show")] + [NSString stringWithFormat:@"%@ zoom level ornament", (self.zoomLevelOrnamentEnabled ? @"Hide" :@"Show")], + [NSString stringWithFormat:@"%@ frame time graph", (self.frameTimeGraphEnabled ? @"Hide" :@"Show")], + [NSString stringWithFormat:@"%@ reuse queue stats", (self.reuseQueueStatsEnabled ? @"Hide" :@"Show")] ]]; break; case MBXSettingsAnnotations: @@ -484,7 +441,7 @@ CLLocationCoordinate2D randomWorldCoordinate() { @"Ornaments Placement", ]]; - if (self.debugLoggingEnabled) + if (self.currentState.debugLoggingEnabled) { [settingsTitles addObjectsFromArray:@[ @"Print Telemetry Logfile", @@ -512,24 +469,25 @@ CLLocationCoordinate2D randomWorldCoordinate() { [self.mapView resetPosition]; break; case MBXSettingsDebugToolsTileBoundaries: - self.mapView.debugMask ^= MGLMapDebugTileBoundariesMask; + self.currentState.debugMask ^= MGLMapDebugTileBoundariesMask; break; case MBXSettingsDebugToolsTileInfo: - self.mapView.debugMask ^= MGLMapDebugTileInfoMask; + self.currentState.debugMask ^= MGLMapDebugTileInfoMask; break; case MBXSettingsDebugToolsTimestamps: - self.mapView.debugMask ^= MGLMapDebugTimestampsMask; + self.currentState.debugMask ^= MGLMapDebugTimestampsMask; break; case MBXSettingsDebugToolsCollisionBoxes: - self.mapView.debugMask ^= MGLMapDebugCollisionBoxesMask; + self.currentState.debugMask ^= MGLMapDebugCollisionBoxesMask; break; case MBXSettingsDebugToolsOverdrawVisualization: - self.mapView.debugMask ^= MGLMapDebugOverdrawVisualizationMask; + self.currentState.debugMask ^= MGLMapDebugOverdrawVisualizationMask; break; case MBXSettingsDebugToolsShowZoomLevel: { - self.mapInfoHUDEnabled = !self.mapInfoHUDEnabled; - self.hudLabel.hidden = !self.mapInfoHUDEnabled; + self.zoomLevelOrnamentEnabled = !self.zoomLevelOrnamentEnabled; + self.currentState.showsZoomLevelOrnament = self.zoomLevelOrnamentEnabled; + self.hudLabel.hidden = !self.zoomLevelOrnamentEnabled; self.reuseQueueStatsEnabled = NO; [self updateHUD]; break; @@ -537,14 +495,16 @@ CLLocationCoordinate2D randomWorldCoordinate() { case MBXSettingsDebugToolsShowFrameTimeGraph: { self.frameTimeGraphEnabled = !self.frameTimeGraphEnabled; + self.currentState.showsTimeFrameGraph = !self.currentState.showsTimeFrameGraph; self.frameTimeGraphView.hidden = !self.frameTimeGraphEnabled; + [self updateHUD]; break; } case MBXSettingsDebugToolsShowReuseQueueStats: { - self.reuseQueueStatsEnabled = !self.reuseQueueStatsEnabled; - self.hudLabel.hidden = !self.reuseQueueStatsEnabled; - self.mapInfoHUDEnabled = NO; + self.reuseQueueStatsEnabled = !self.currentState.reuseQueueStatsEnabled; + self.hudLabel.hidden = !self.currentState.reuseQueueStatsEnabled; + self.zoomLevelOrnamentEnabled = NO; [self updateHUD]; break; } @@ -552,6 +512,9 @@ CLLocationCoordinate2D randomWorldCoordinate() { NSAssert(NO, @"All debug tools setting rows should be implemented"); break; } + + self.mapView.debugMask = self.currentState.debugMask; + break; case MBXSettingsAnnotations: switch (indexPath.row) @@ -2345,9 +2308,10 @@ CLLocationCoordinate2D randomWorldCoordinate() { } - (void)updateHUD { - if (!self.reuseQueueStatsEnabled && !self.mapInfoHUDEnabled) return; - if (self.hudLabel.hidden) self.hudLabel.hidden = NO; + if (self.reuseQueueStatsEnabled == NO && self.zoomLevelOrnamentEnabled == NO) { + return; + } NSString *hudString; @@ -2357,7 +2321,7 @@ CLLocationCoordinate2D randomWorldCoordinate() { queuedAnnotations += queue.count; } hudString = [NSString stringWithFormat:@"Visible: %ld Queued: %ld", (unsigned long)self.mapView.visibleAnnotations.count, (unsigned long)queuedAnnotations]; - } else if (self.mapInfoHUDEnabled) { + } else if (self.zoomLevelOrnamentEnabled) { hudString = [NSString stringWithFormat:@"%.f FPS (%.1fms) ∕ %.2f ∕ ↕\U0000FE0E%.f° ∕ %.f°", roundf(self.mapView.averageFrameRate), self.mapView.averageFrameTime, self.mapView.zoomLevel, self.mapView.camera.pitch, self.mapView.direction]; @@ -2420,4 +2384,39 @@ CLLocationCoordinate2D randomWorldCoordinate() { } } +- (void)saveCurrentMapState:(__unused NSNotification *)notification { + + // The following properties can change after the view loads so we need to save their + // state before exiting the view controller. + self.currentState.camera = self.mapView.camera; + self.currentState.showsUserLocation = self.mapView.showsUserLocation; + self.currentState.userTrackingMode = self.mapView.userTrackingMode; + self.currentState.showsUserHeadingIndicator = self.mapView.showsUserHeadingIndicator; + self.currentState.showsMapScale = self.mapView.showsScale; + self.currentState.showsZoomLevelOrnament = self.zoomLevelOrnamentEnabled; + self.currentState.showsTimeFrameGraph = self.frameTimeGraphEnabled; + self.currentState.debugMask = self.mapView.debugMask; + self.currentState.debugLoggingEnabled = self.debugLoggingEnabled; + self.currentState.reuseQueueStatsEnabled = self.reuseQueueStatsEnabled; + + [[MBXStateManager sharedManager] saveState:self.currentState]; +} + +- (void)restoreMapState:(__unused NSNotification *)notification { + MBXState *currentState = [MBXStateManager sharedManager].currentState; + + self.mapView.camera = currentState.camera; + self.mapView.showsUserLocation = currentState.showsUserLocation; + self.mapView.userTrackingMode = currentState.userTrackingMode; + self.mapView.showsUserHeadingIndicator = currentState.showsUserHeadingIndicator; + self.mapView.showsScale = currentState.showsMapScale; + self.zoomLevelOrnamentEnabled = currentState.showsZoomLevelOrnament; + self.frameTimeGraphEnabled = currentState.showsTimeFrameGraph; + self.mapView.debugMask = currentState.debugMask; + self.debugLoggingEnabled = currentState.debugLoggingEnabled; + self.reuseQueueStatsEnabled = currentState.reuseQueueStatsEnabled; + + self.currentState = currentState; +} + @end |