Files
eqn.ios/Sources/Earthquake Network/Controllers/EQNMainTabBarController.m
T
2020-08-15 16:37:27 +02:00

159 lines
5.4 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 "EQNTabControllerDelegate.h"
#import "EQNUtility.h"
#import "EQNManager.h"
#import "EQNGeneratoreURLServer.h"
#import "ServerRequest.h"
@interface EQNMainTabBarController () <UITabBarControllerDelegate>
@property (nonatomic, strong) IBOutlet UIBarButtonItem *sidebarButton;
@property(nonatomic, strong) GADBannerView *bannerView;
@end
@implementation EQNMainTabBarController
static NSString * const SegueIdentifierInitialLoading = @"ShowInitialLoading";
static NSString * const SegueIdentifierSettings = @"ShowSettings";
#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];
self.bannerView = [EQNUtility ottieniBannerWithController:self position:YES];
// 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];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveInAppNotification:) name:IAPHelperPurchaseNotification object:nil];
}
- (void)addHomeButton
{
for (UIViewController *viewController in self.viewControllers) {
// search for a controller inside a nav controller
UIViewController *controller = viewController;
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)viewController;
controller = navController.viewControllers.firstObject;
}
// 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;
}
}
}
#pragma mark - Notifications
- (void)didReceiveInAppNotification:(NSNotification *)notification
{
[self.bannerView removeFromSuperview];
self.bannerView = nil;
}
- (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 -
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// search for a controller inside a nav controller
UIViewController *controller = viewController;
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)viewController;
controller = navController.viewControllers.firstObject;
}
// force a UI refresh when tab is changed
if ([controller conformsToProtocol:@protocol(EQNTabControllerDelegate)]) {
id<EQNTabControllerDelegate> tabController = (id<EQNTabControllerDelegate>)controller;
if ([tabController respondsToSelector:@selector(refreshUI)]) {
[tabController refreshUI];
}
}
}
@end