// // EQNMainTabBarController.m // Earthquake Network // // Created by Busi Andrea on 13/08/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // #import "EQNMainTabBarController.h" #import "AppDelegate.h" #import "EQNBaseViewController.h" #import "SettingsBaseViewController.h" #import "EQNManager.h" #import "ServerRequest.h" @interface EQNMainTabBarController () @property (nonatomic, strong) IBOutlet UIBarButtonItem *sidebarButton; @property (nonatomic) NSInteger debugTapCounter; @end @implementation EQNMainTabBarController static NSString * const SegueIdentifierSettings = @"ShowSettings"; static NSString * const SegueIdentifierLogs = @"ShowLogs"; #pragma mark - View Lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.delegate = self; [self localizeUI]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.mainTabBarController = self; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverRegistrationFailedNotification:) name:EQNServerRegistrationDidFailNotification object:nil]; [self sincronizza]; } #pragma mark - Private - (void)localizeUI { self.tabBar.items[EQNTabBarSectionAllerte].title = [NSLocalizedString(@"tab_network", comment: "") capitalizedString]; self.tabBar.items[EQNTabBarSectionSegnalazioni].title = [NSLocalizedString(@"tab_manual", comment: "") capitalizedString]; self.tabBar.items[EQNTabBarSectionRetiSismiche].title = [NSLocalizedString(@"tab_official", comment: "") capitalizedString]; self.tabBar.items[EQNTabBarSectionImpostazioni].title = [NSLocalizedString(@"drawer_main_settings", comment: "") capitalizedString]; } #pragma mark - Notification - (void)serverRegistrationFailedNotification:(NSNotification *)notification { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"attention", nil) message:NSLocalizedString(@"error_server_registration", nil) preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"retry", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // retry server registration [[EQNUser defaultUser] verificaRegistrazione]; }]]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"options_cancel", nil) style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; }); } #pragma mark - Public - (void)sincronizza { [[EQNManager defaultManager] sincronizza]; } - (void)selectSection:(EQNTabBarSection)section { NSInteger index = 0; switch (section) { case EQNTabBarSectionAllerte: index = 0; break; case EQNTabBarSectionSegnalazioni: index = 1; break; case EQNTabBarSectionRetiSismiche: index = 2; break; case EQNTabBarSectionImpostazioni: index = 3; break; } self.selectedIndex = index; } #pragma mark - Helpers - (UIViewController *)getTopControllerFromController:(UIViewController *)viewController { // check if the given controller is a UINavigationController // and, if yes, returns the top view controller UIViewController *topController = viewController; if ([viewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navController = (UINavigationController *)viewController; topController = navController.viewControllers.firstObject; } return topController; } - (void)openDebugController { EQNDebugViewController *controller = [[EQNDebugViewController alloc] init]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller]; [self presentViewController:navController animated:YES completion:nil]; } #pragma mark - UITabBarControllerDelegate - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { // if user switch from settings page, we need to force a settings save UIViewController *controller = [self getTopControllerFromController:tabBarController.selectedViewController]; if ([controller isKindOfClass:[SettingsViewController class]]) { [SettingsBaseViewController saveSettings]; } return YES; } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { UIViewController *controller = [self getTopControllerFromController:viewController]; // force a UI refresh when tab is changed if ([controller isKindOfClass:[EQNBaseViewController class]]) { EQNBaseViewController *baseController = (EQNBaseViewController *)controller; [baseController refreshUI]; } // tap 5 times on "Settings" to open debug view if ([controller isKindOfClass:[SettingsViewController class]]) { self.debugTapCounter += 1; if (self.debugTapCounter == 5) { self.debugTapCounter = 0; [self openDebugController]; } } } @end