Files
eqn.ios/Sources/Earthquake Network/Controllers/EQNMainTabBarController.m
T
2020-08-16 11:38:19 +02:00

155 lines
5.1 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 "SWRevealViewController.h"
#import "EQNBaseViewController.h"
#import "EQNUtility.h"
#import "EQNManager.h"
#import "EQNGeneratoreURLServer.h"
#import "ServerRequest.h"
@interface EQNMainTabBarController () <UITabBarControllerDelegate>
@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
}];
}
[self addObservers];
[self addHomeButton];
// 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 - Private
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveOpenSettingsNotification:) name:NOTIFICHE_SISMI object:nil];
}
- (void)addHomeButton
{
for (UIViewController *viewController in self.viewControllers) {
UIViewController *controller = [self getTopControllerFromController:viewController];
// add hamburgher menu button
SWRevealViewController *revealViewController = self.revealViewController;
if (revealViewController && controller.navigationItem.leftBarButtonItem == nil) {
UIImage *homeImage = [UIImage imageNamed:@"navbar-icon-menu"];
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithImage:homeImage
style:UIBarButtonItemStylePlain
target:revealViewController
action:@selector(revealToggle:)];
controller.navigationItem.leftBarButtonItem = homeButton;
}
}
// add pan gesture to reveal menu
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
#pragma mark - Notifications
- (void)didReceiveOpenSettingsNotification:(NSNotification *)notification
{
[self performSegueWithIdentifier:SegueIdentifierSettings sender:nil];
}
#pragma mark - Public
- (void)sincronizza
{
[[EQNManager defaultManager] sincronizza];
}
- (void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSURL *url = [EQNGeneratoreURLServer urlPosizione];
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:posizione success:^(id result) {
completionHandler(UIBackgroundFetchResultNewData);
} failure:^(NSError *error) {
completionHandler(UIBackgroundFetchResultFailed);
}];
}
- (void)selectSection:(EQNTabBarSection)section
{
NSInteger index = 0;
switch (section) {
case EQNTabBarSectionAllerte: index = 0; break;
case EQNTabBarSectionSegnalazioni: index = 1; break;
case EQNTabBarSectionRetiSismiche: index = 2; 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
- (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