// // MasterViewController.m // Earthquake Network // // Created by Luca Beretta on 17/10/18. // Copyright © 2018 Luca Beretta. All rights reserved. // #import "MasterViewController.h" #import "SWRevealViewController.h" #import "ReteSmartphone.h" #import "SegnalazioniViewController.h" #import "EQNRetiSismiViewController.h" #import "TsunamiViewController.h" #import "Costanti.h" #import "EQNUtility.h" #import "EQNManager.h" #import "EQNUser.h" #import "ServerRequest.h" #import "EQNGeneratoreURLServer.h" #import "MasterViewController.h" #import "AppDelegate.h" #import "MenuSelezioneEntiViewController.h" #import "ElencoFiltroEntiTableViewController.h" #import "InformazioniViewController.h" #import "MenuFiltroEntiViewController.h" @interface MasterViewController () @property (weak, nonatomic) IBOutlet UIView *containerView; @property (strong, nonatomic) ReteSmartphone *reteSmartphone; @property (strong, nonatomic) SegnalazioniViewController *segnalazioni; @property (strong, nonatomic) EQNRetiSismiViewController *restiSismi; @property (strong, nonatomic) TsunamiViewController *tsunami; @property(nonatomic, strong) GADBannerView *bannerView; @property (nonatomic, strong) UIStoryboard *mainStoreBoard; @property (nonatomic, strong) NSIndexPath *selectIndex; @end @implementation MasterViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.masterViewController = self; [self sincronizza]; NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSData *receipt = [NSData dataWithContentsOfURL:receiptURL]; if (!receipt) { /* No local receipt -- handle the error. */ [[NSUserDefaults standardUserDefaults] setBool:NO forKey:EQN_ACQ_IN_APP]; self.bannerView = [EQNUtility ottieniBannerWithController:self position:YES]; } else { [[ServerRequest defaultServerConnectionSingleton] inviaRicevuta:receipt success:^(id result) { } failure:^(NSError *error) { }]; } /* ... Send the receipt data to your server ... */ SWRevealViewController *revealViewController = self.revealViewController; if ( revealViewController ) { [self.sidebarButton setTarget: self.revealViewController]; [self.sidebarButton setAction: @selector( revealToggle: )]; [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; } [self performSegueWithIdentifier:@"load_view_waiter" sender:self]; UIView *viewNavigationBar = [[UIView alloc] initWithFrame:CGRectMake(-18, 5, 190, 44)]; UIImageView *imageLogo = [[UIImageView alloc] initWithFrame:CGRectMake(-18, 5, 38, 38)]; imageLogo.image = [UIImage imageNamed:@"distquake_app_wave"]; [viewNavigationBar addSubview:imageLogo]; UILabel *textForNavi = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 140, 38)]; textForNavi.text = NSLocalizedString(@"Rilevatore Terremoti", @"titolo navigation bar"); textForNavi.adjustsFontSizeToFitWidth = YES; [textForNavi setMinimumScaleFactor:12.0/[UIFont labelFontSize]]; if(IS_IPHONE_5) { textForNavi.text = @""; } [viewNavigationBar addSubview:textForNavi]; self.navigationItem.titleView = viewNavigationBar; [[UINavigationBar appearance] setBarTintColor:COLORE_BARRA_NAVIGAZIONE]; [[UINavigationBar appearance] setTranslucent:NO]; [self registraNotifiche]; self.listaAzioniCollectView = @[NSLocalizedString(@"RETE SMARTPHONE", @"pulsanti orrizontale"), NSLocalizedString(@"SEGNALAZIONI", @"pulsanti orrizontale"), NSLocalizedString(@"RETI SISMICHE", @"pulsanti orrizontale"), NSLocalizedString(@"TSUNAMI", @"pulsanti orrizontale")/*, NSLocalizedString(@"CHAT", @"pulsanti orrizontale"), NSLocalizedString(@"E-MAIL/SMS", @"pulsanti orrizontale"), NSLocalizedString(@"ALTRO", @"pulsanti orrizontale"), NSLocalizedString(@"SPONSOR", @"pulsanti orrizontale")*/]; [self caricaChildrenViewController:[NSIndexPath indexPathForRow:0 inSection:0]]; if (![[NSUserDefaults standardUserDefaults] boolForKey:EQN_ACQ_IN_APP]){ self.bannerView = [EQNUtility ottieniBannerWithController:self position:YES]; } } - (void)annullaAcquisitiInApp { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:NO forKey:AQUISTO_PRO]; [defaults setBool:NO forKey:AQUISTO_PRO_SCONTATO]; [defaults setBool:NO forKey:@"com.finazzi.distquake.Abbonamento10k.mensileAutomatico"]; [defaults setBool:NO forKey:@"com.finazzi.distquake.Abbonamento10k.annualeAutomatico"]; [defaults setBool:NO forKey:@"com.finazzi.distquake.Abbonamento10k.annualeAutomaticoScontato"]; [defaults setBool:NO forKey:@"com.finazzi.distquake.Abbonamento100k.mensileAutomatico"]; [defaults setBool:NO forKey:@"com.finazzi.distquake.Abbonamento100k.annualeAutomatico"]; [defaults setBool:NO forKey:@"com.finazzi.distquake.Abbonamento100k.annualeAutomaticoscontato"]; } -(void)registraNotifiche{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(apriNotificheSismi) name:NOTIFICHE_SISMI object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(apriInformazioni:) name:INFORMAZIONI_APP_MENU object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acquistiInApp:) name:IAPHelperPurchaseNotification object:nil]; } -(void)apriImpostazioni{ // impostazioniEntiSismi [self performSegueWithIdentifier:@"impostazioniEntiSismi" sender:nil]; } -(void)caricaChildrenViewController:(NSIndexPath *)indexPath{ if (!self.mainStoreBoard) self.mainStoreBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; switch (indexPath.row) { case 0:{ if (!self.reteSmartphone) self.reteSmartphone = (ReteSmartphone *)[self.mainStoreBoard instantiateViewControllerWithIdentifier:@"ReteSmartphone"]; self.navigationItem.rightBarButtonItems = [EQNUtility getButtonForControllerTipe:reteSmartphoneController withController:self]; [self add:self.reteSmartphone]; } break; case 1:{ if (!self.segnalazioni) self.segnalazioni = (SegnalazioniViewController *)[self.mainStoreBoard instantiateViewControllerWithIdentifier:@"SegnalazioniViewController"]; self.navigationItem.rightBarButtonItems = [EQNUtility getButtonForControllerTipe:tsunamiControler withController:self]; [self add:self.segnalazioni]; } break; case 2:{ if (!self.restiSismi) self.restiSismi = (EQNRetiSismiViewController *)[self.mainStoreBoard instantiateViewControllerWithIdentifier:@"EQNRetiSismiViewController"]; self.navigationItem.rightBarButtonItems = [EQNUtility getButtonForControllerTipe:reteSismicaController withController:self]; [self add:self.restiSismi]; } break; case 3:{ if (!self.tsunami) self.tsunami = (TsunamiViewController *)[self.mainStoreBoard instantiateViewControllerWithIdentifier:@"TsunamiViewController"]; self.navigationItem.rightBarButtonItems = [EQNUtility getButtonForControllerTipe:tsunamiControler withController:self]; [self add:self.tsunami]; } break; default: break; } } -(void)acquistiInApp:(NSNotification *)notification{ [self.bannerView removeFromSuperview]; self.bannerView = nil; } -(void)apriImpostazioni:(NSNotification *)notifica{ [self performSegueWithIdentifier:@"apriImpostazioniReteSismiche" sender:nil]; } -(void)sincronizza{ [[EQNManager defaultManager] sincronizza]; } -(void)apriManu{ [self performSegueWithIdentifier:@"FiltriEntiSismici" sender:nil]; } -(void)clock{ } -(void)apriNotificheSismi{ [self performSegueWithIdentifier:@"dettagliImpostazioni" sender:nil]; } -(void)apriInformazioni:(NSNotification *)notification{ [self performSegueWithIdentifier:@"apriInformazioni" sender:notification]; } -(void)heart{ [self performSegueWithIdentifier:@"logViewController" sender:nil]; } -(void)add:(UIViewController *)viewController{ [self addChildViewController:viewController]; [self.containerView addSubview:viewController.view]; viewController.view.frame = self.containerView.bounds; viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [viewController didMoveToParentViewController:self]; } -(void)remove:(NSIndexPath *)indexPath{ if (self.childViewControllers.count>0) { UIViewController *controller = self.childViewControllers[0]; [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; } [self caricaChildrenViewController:indexPath]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma collection view -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.listaAzioniCollectView.count; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0.0f; // This is the minimum inter item spacing, can be more } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"buttoneMenu" forIndexPath:indexPath]; UILabel *titolo = (UILabel *)[cell viewWithTag:10]; titolo.text = self.listaAzioniCollectView[indexPath.row]; UILabel *indicatore = (UILabel *)[cell viewWithTag:11]; if(indexPath.row == self.selectIndex.row) indicatore.backgroundColor = [UIColor redColor]; else indicatore.backgroundColor = [UIColor clearColor]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ [self selezionaItem:indexPath]; } -(void)selezionaItem:(NSIndexPath *)indexPath{ self.selectIndex = indexPath; if (indexPath.row == 0) [self.reteSmartphone setUpView]; [self remove:indexPath]; [self.collectionView reloadData]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; } -(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[EQNGeneratoreURLServer urlPosizione] richiesta:posizione success:^(id result) { completionHandler(UIBackgroundFetchResultNewData); } failure:^(NSError *errore) { completionHandler(UIBackgroundFetchResultFailed); }]; } #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. if([segue.identifier isEqualToString:@"impostazioniEntiSismi"]){ MenuSelezioneEntiViewController *controller = (MenuSelezioneEntiViewController *)segue.destinationViewController; controller.delegate = self; } // if([segue.identifier isEqualToString:@"FiltriEntiSismici"]){ MenuFiltroEntiViewController *controller = (MenuFiltroEntiViewController *)segue.destinationViewController; controller.delegate = self; } if([segue.identifier isEqualToString:@"elencoRetiSismiche"]){ UINavigationController *nav = (UINavigationController *)segue.destinationViewController; ElencoFiltroEntiTableViewController *controller = (ElencoFiltroEntiTableViewController *)nav.topViewController; controller.delegate = self; } if([segue.identifier isEqualToString:@"apriInformazioni"]){ NSNotification *notification = (NSNotification *)sender; NSDictionary *userInfo = notification.userInfo; UINavigationController *nav = (UINavigationController *)segue.destinationViewController; InformazioniViewController *controller = (InformazioniViewController *)nav.topViewController; controller.index = userInfo[@"indice"]; } } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark menuFilterDelegate -(void)inviaDati:(EQNfiltroEnti)filtro{ [[NSUserDefaults standardUserDefaults] setInteger:filtro forKey:IMPOSTAZIONE_TIPO_FILTRO_RETI_SISMICHEI]; [self.restiSismi aggiornaTabella:nil]; } -(void)apriElencoEnti{ [self performSegueWithIdentifier:@"elencoRetiSismiche" sender:nil]; } // elencoRetiSismiche #pragma mark Menu Lista Enti Delegate -(void)inviaDatiLista:(EQNfiltroEnti)filtro{ [[NSUserDefaults standardUserDefaults] setInteger:filtro forKey:IMPOSTAZIONE_TIPO_FILTRO_RETI_SISMICHEI]; [self.restiSismi aggiornaTabella:nil]; } #pragma mark Menu Filtro Enti Delegate -(void)aggiornaFiltro{ [self.restiSismi aggiornaTabella:nil]; } @end