163 lines
5.9 KiB
Objective-C
163 lines
5.9 KiB
Objective-C
//
|
|
// 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 "EQNManager.h"
|
|
#import "ServerRequest.h"
|
|
|
|
@interface EQNMainTabBarController () <UITabBarControllerDelegate>
|
|
@property (nonatomic, strong) IBOutlet UIBarButtonItem *sidebarButton;
|
|
@property (nonatomic) NSInteger debugTapCounter;
|
|
@end
|
|
|
|
@implementation EQNMainTabBarController
|
|
|
|
#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];
|
|
[self migrationV5_8];
|
|
}
|
|
|
|
#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];
|
|
}
|
|
|
|
- (void)migrationV5_8
|
|
{
|
|
// forziamo il salvataggio delle impostazioni di notifica, perchè i vari valori devono essere migrati
|
|
BOOL alreadyMigrated = [NSUserDefaults.standardUserDefaults boolForKey:NSUserDefaults.SaveSettingsNotificationMigrationV5_8];
|
|
if (alreadyMigrated) {
|
|
return;
|
|
}
|
|
|
|
NSLog(@"[MIGRATION] perform notification settings save");
|
|
[SettingsBaseTableViewController saveSettingsWithCompletion:^(BOOL success) {
|
|
if (success) {
|
|
NSLog(@"[MIGRATION] settings saved");
|
|
[NSUserDefaults.standardUserDefaults setBool:true forKey:NSUserDefaults.SaveSettingsNotificationMigrationV5_8];
|
|
}
|
|
}];
|
|
}
|
|
|
|
#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] retryUserRegistration];
|
|
}]];
|
|
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"status_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]]) {
|
|
[SettingsBaseTableViewController 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]] && EQNEnableDebugView) {
|
|
self.debugTapCounter += 1;
|
|
if (self.debugTapCounter == 5) {
|
|
self.debugTapCounter = 0;
|
|
[self openDebugController];
|
|
}
|
|
}
|
|
}
|
|
|
|
@end
|