// // 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 "EQNUtility.h" #import "EQNManager.h" #import "EQNGeneratoreURLServer.h" #import "ServerRequest.h" @interface EQNMainTabBarController () @property (nonatomic, strong) IBOutlet UIBarButtonItem *sidebarButton; @end @implementation EQNMainTabBarController static NSString * const SegueIdentifierInitialLoading = @"ShowInitialLoading"; static NSString * const SegueIdentifierSettings = @"ShowSettings"; static NSString * const SegueIdentifierLogs = @"ShowLogs"; #pragma mark - View Lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.delegate = self; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.mainTabBarController = self; [self sincronizza]; // check for an AppStore receipt NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSData *receipt = [NSData dataWithContentsOfURL:receiptURL]; if (receipt) { [[ServerRequest defaultServerConnectionSingleton] inviaRicevuta:receipt success:^(id result) { // nope } failure:^(NSError *error) { // nope }]; } // show loader controller during initial data download dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self performSegueWithIdentifier:SegueIdentifierInitialLoading sender:self]; }); } #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; } #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]; } } @end