// // ViewController.m // Earthquake Network // // Created by Luca Beretta on 12/09/18. // Copyright © 2018 Luca Beretta. All rights reserved. // #import "ReteSmartphone.h" #import "EQNCalibrazione.h" #include "math.h" #import #import "Costanti.h" #import "ServerRequest.h" #import "EQNGeneratoreURLServer.h" #import "SWRevealViewController.h" #import "EQNManager.h" #import "EQNReteSmartphone.h" #import "PastquakesDettagliMappa.h" #import "PastquakesAnnotation.h" #import "EQNUser.h" #import "AppDelegate.h" #import "ServerRequest.h" #import "EQNAreaCheck.h" @import StoreKit; @import GoogleMobileAds; @interface ReteSmartphone () @property (assign, nonatomic) CMAcceleration acceleration; @property (strong, nonatomic) CMMotionManager *motionManager; @property (strong, nonatomic) NSOperationQueue *queue; @property (nonatomic, strong) NSMutableArray *bufferAccelerometro; @property (nonatomic, strong) NSTimer *timer; //@property (nonatomic, strong) NSArray *listaAzioniCollectView; @property (nonatomic, strong) NSMutableArray *listaItemTableView; @property (nonatomic, strong) IBOutlet UIBarButtonItem *sidebarButton; @property (nonatomic, strong) IBOutlet UICollectionView *collectionView; @property (nonatomic, strong) IBOutlet UITableView *tableView; @property (nonatomic, assign) BOOL isNotificaAttiva; @property (nonatomic, strong) NSString *testoPerUtenti; @property(nonatomic, strong) GADBannerView *bannerView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *fooTable; @end @implementation ReteSmartphone - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpView) name:NOTIFICA_DOWNLOAD_TERMINATO object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpView) name:IAPHelperPurchaseNotification object:nil]; SWRevealViewController *revealViewController = self.revealViewController; if ( revealViewController ) { [self.sidebarButton setTarget: self.revealViewController]; [self.sidebarButton setAction: @selector( revealToggle: )]; [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; } [self.tableView setContentInset:UIEdgeInsetsMake(20,0,0,0)]; UIView *viewNavigationBar = [[UIView alloc] initWithFrame:CGRectMake(-18, 5, 150, 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, 130, 38)]; textForNavi.text = NSLocalizedString(@"Rilevatore Terremoti", @"titolo navigation bar"); [viewNavigationBar addSubview:textForNavi]; self.navigationItem.titleView = viewNavigationBar; [[UINavigationBar appearance] setBarTintColor:COLORE_BARRA_NAVIGAZIONE]; [[UINavigationBar appearance] setTranslucent:NO]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.reteSmartphone = self; self.listaItemTableView = [@[] mutableCopy]; /* [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_LINK_MESSAGGIO] richiesta:downloadDati success:^(id result) { self.testoPerUtenti = result[0][@"message"]; [self performSelectorOnMainThread:@selector(setUpView) withObject:nil waitUntilDone:YES]; } failure:^(NSError * error) { // NSLog(@"Errore scaricaDatiReteSmartphone %@", error); }];*/ NSString *appId = @"https://itunes.apple.com/app/id1449893235"; self.testoPerUtenti = [NSString stringWithFormat:@"%@ %@ %@", NSLocalizedString(@"testo utenti1" , @""), appId, NSLocalizedString(@"testo utenti2" , @"")]; [self setUpView]; // self.bannerView = [EQNUtility ottieniBannerWithController:self]; } -(void)setUpView{ NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA]; if (date) { if ([EQNUtility getDifferenceMinute:date] < TEMPO_VISUALIZZAZIONE_NOTIFICA) self.isNotificaAttiva = YES; else{ self.isNotificaAttiva = NO; [[NSUserDefaults standardUserDefaults] removeObjectForKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA]; [[NSUserDefaults standardUserDefaults] synchronize]; } } [self aggiornaTabella:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)aggiornaTabella:(NSNotification *)notifica{ self.listaItemTableView = [@[@"Allerta sismica",/* NSLocalizedString(@"Dispositivo affidabile", @"item tabelle") ,*/ NSLocalizedString(@"Sismi rilevati da smartphone", @"item tabelle"), NSLocalizedString(@"Rete smartphone", @"item tabelle"), NSLocalizedString(@"Lista priorità - NUOVA!", @"item tabelle"), NSLocalizedString(@"Versione PRO", @"item tabelle"), NSLocalizedString(@"messaggio per utenti", @"item tabelle")] mutableCopy]; if ([EQNPurchaseUtility isProVersionEnabled]) { self.fooTable.constant = 0; [self.listaItemTableView removeObject:NSLocalizedString(@"Versione PRO", @"item tabelle")]; } dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; //reload table view }); } -(void)apriMappa:(id)sender{ [self performSegueWithIdentifier:@"dettagliMappa" sender:sender]; } -(void)apriPriorita:(id)sender{ [self performSegueWithIdentifier:@"servizioPriorità" sender:sender]; } -(void)apriVersionePRO:(id)sender{ [self performSegueWithIdentifier:@"passaVersionePro" sender:sender]; } -(void)chiudiNotifica:(id)sender{ [[NSUserDefaults standardUserDefaults] removeObjectForKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA]; [[NSUserDefaults standardUserDefaults] synchronize]; self.isNotificaAttiva = NO; [self.tableView reloadData]; } #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 { if ([segue.identifier isEqualToString:@"dettagliMappa"]) { UINavigationController *controller = (UINavigationController *)segue.destinationViewController; PastquakesDettagliMappa *dettagliMappa = (PastquakesDettagliMappa *)controller.topViewController; dettagliMappa.tipoMappa = reteSmartphone; } // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } #pragma table view - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.listaItemTableView.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ switch (indexPath.row) { case 0: if (self.isNotificaAttiva) return 480; return 100; break; // case 1: // return 50; break; case 1: return 124; break; case 2: return 140; break; case 3: return 127; break; case 4:{ if (self.listaItemTableView.count == 5) return 250; return 127; } break; case 5:{ if (self.listaItemTableView.count == 6) return 250; return 127; } break; default: return 127; break; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // modalita base switch (indexPath.row) { case 0:{ if (self.isNotificaAttiva) { NSDictionary *info = [EQNUtility readDictyWithCustomObjFromUserDefaults:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA]; int intensita = [info[@"intensity"] intValue]; UIColor *backgroudColor; UIColor *buttonColor; UIColor *buttonBackroundColor; switch (intensita) { case 0: backgroudColor = COLORE_CELLA_MAGNITUTE_LEGGERA; buttonBackroundColor = COLORE_CELLA_MAGNITUTE_LEGGERA_ETI; buttonColor = COLORE_CELLA_MAGNITUTE_LEGGERA_TESTO; break; case 1: backgroudColor = COLORE_CELLA_MAGNITUTE_MEDIA; buttonBackroundColor = COLORE_CELLA_MAGNITUTE_MEDIA_ETI; buttonColor = COLORE_CELLA_MAGNITUTE_MEDIA_TESTO; break; case 2: backgroudColor = COLORE_CELLA_MAGNITUTE_FORTE; buttonBackroundColor = COLORE_CELLA_MAGNITUTE_FORTE_ETI; buttonColor = COLORE_CELLA_MAGNITUTE_FORTE_TESTO; break; default: break; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellNotifica" forIndexPath:indexPath]; UIView *containerView = [cell viewWithTag:1]; containerView.backgroundColor = backgroudColor; NSLog(@"Info %@", info); NSString *titoloString = info[@"aps"][@"alert"][@"title-loc-key"]; NSArray *arrayArgs = info[@"aps"][@"alert"][@"loc-args"]; NSString *bodyString = [NSString stringWithFormat:NSLocalizedString(info[@"aps"][@"alert"][@"loc-key"], @""), info[@"aps"][@"alert"][@"loc-args"][0]]; if (arrayArgs.count == 0){ NSString *bodyString1 = NSLocalizedString(info[@"aps"][@"alert"][@"loc-key"], @""); bodyString = [bodyString1 substringToIndex:[bodyString1 length]-2]; } UILabel *titolo = (UILabel *)[containerView viewWithTag:2]; titolo.text = NSLocalizedString(titoloString, @""); titolo.textColor = buttonColor; UIButton *button = (UIButton *)[containerView viewWithTag:3]; button.layer.cornerRadius = button.frame.size.width/2; [button addTarget:self action:@selector(chiudiNotifica:) forControlEvents:UIControlEventTouchUpInside]; button.backgroundColor = buttonColor; UILabel *dettagli = (UILabel *)[containerView viewWithTag:4]; dettagli.text = bodyString; MKMapView *mappa = (MKMapView *)[containerView viewWithTag:5]; CLLocation *coordinate = [[CLLocation alloc] initWithLatitude:[info[@"latitude"] doubleValue] longitude:[info[@"longitude"] doubleValue]]; MKCoordinateSpan span = MKCoordinateSpanMake(10.5, 10.5); MKCoordinateRegion region = MKCoordinateRegionMake(coordinate.coordinate, span); mappa.delegate = self; [mappa setCenterCoordinate:coordinate.coordinate animated:NO]; [mappa setRegion:region animated:YES]; mappa.scrollEnabled = NO; PastquakesAnnotation *pasquaAnnotation = [[PastquakesAnnotation alloc] initWithTitle:info[@"title"] location:coordinate.coordinate intensita:[info[@"intensity"] intValue]]; [mappa addAnnotation:pasquaAnnotation]; UIButton *condividi = (UIButton *)[containerView viewWithTag:6]; condividi.layer.borderWidth = 1; condividi.layer.borderColor = [UIColor blackColor].CGColor; condividi.layer.cornerRadius = 5; condividi.backgroundColor = buttonBackroundColor; [condividi setTitle:NSLocalizedString(@"CONDIVIDI", @"titolo pulsante notifica rete smartphone") forState:UIControlStateNormal]; [condividi addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside]; UIButton *votaApp = (UIButton *)[containerView viewWithTag:7]; votaApp.layer.borderWidth = 1; votaApp.layer.borderColor = [UIColor blackColor].CGColor; votaApp.layer.cornerRadius = 5; votaApp.backgroundColor = buttonBackroundColor; [votaApp setTitle:NSLocalizedString(@"VOTA L'APP", @"titolo pulsante notifica rete smartphone") forState:UIControlStateNormal]; [votaApp addTarget:self action:@selector(votaApp:) forControlEvents:UIControlEventTouchUpInside]; UIButton *twitter = (UIButton *)[containerView viewWithTag:8]; twitter.layer.borderWidth = 1; twitter.layer.borderColor = [UIColor blackColor].CGColor; twitter.layer.cornerRadius = 5; twitter.backgroundColor = buttonBackroundColor; [twitter setTitle:NSLocalizedString(@"VEDI IN TWITTER", @"titolo pulsante notifica rete smartphone") forState:UIControlStateNormal]; [twitter addTarget:self action:@selector(apriTwitter:) forControlEvents:UIControlEventTouchUpInside]; UILabel *NOTE = (UILabel *)[containerView viewWithTag:9]; NOTE.text = NSLocalizedString(@"La magnitudo sarà calcola e cominicata dalla rete sismica nazionale e comparirà nella sezione Reti Sismiche dell'app.", @""); return cell; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_1" forIndexPath:indexPath]; return cell; } break; /* case 1:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_2" forIndexPath:indexPath]; return cell; } break;*/ case 1:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_3" forIndexPath:indexPath]; UILabel *ultime24h = (UILabel *)[cell viewWithTag:10]; ultime24h.text = [EQNManager defaultManager].rete_smartphone.eq; UILabel *totali = (UILabel *)[cell viewWithTag:20]; totali.text = [EQNManager defaultManager].rete_smartphone.eq_p; UIButton *mappaButton = (UIButton *)[cell viewWithTag:30]; [mappaButton addTarget:self action:@selector(apriMappa:) forControlEvents:UIControlEventTouchDown]; return cell; } break; case 2:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_4" forIndexPath:indexPath]; UILabel *smartphoneAttivi = (UILabel *)[cell viewWithTag:10]; smartphoneAttivi.text = [[EQNManager defaultManager].rete_smartphone.green stringValue]; UIButton *pulsante = (UIButton *)[cell viewWithTag:200]; pulsante.layer.cornerRadius = 5; pulsante.layer.borderWidth = 1; pulsante.layer.borderColor = [UIColor blackColor].CGColor; [pulsante addTarget:self action:@selector(visualizzaCopertura) forControlEvents:UIControlEventTouchUpInside]; return cell; } break; case 3:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_5" forIndexPath:indexPath]; UIButton *buttonPriorita = (UIButton *)[cell viewWithTag:10]; [buttonPriorita addTarget:self action:@selector(apriPriorita:) forControlEvents:UIControlEventTouchDown]; return cell; } break; case 4:{ if (self.listaItemTableView.count == 5){ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_7" forIndexPath:indexPath]; UITextView *textView = (UITextView *)[cell viewWithTag:12]; textView.text = self.testoPerUtenti; UIButton *condividi = (UIButton *)[cell viewWithTag:100]; [condividi addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside]; return cell; }else{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_6" forIndexPath:indexPath]; UIButton *buttonPRO = (UIButton *)[cell viewWithTag:10]; [buttonPRO addTarget:self action:@selector(apriVersionePRO:) forControlEvents:UIControlEventTouchDown]; return cell; } } break; case 5:{ if (self.listaItemTableView.count == 6){ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_7" forIndexPath:indexPath]; UITextView *textView = (UITextView *)[cell viewWithTag:12]; textView.text = self.testoPerUtenti; UIButton *condividi = (UIButton *)[cell viewWithTag:100]; [condividi addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside]; return cell; }else{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_6" forIndexPath:indexPath]; UIButton *buttonPRO = (UIButton *)[cell viewWithTag:10]; [buttonPRO addTarget:self action:@selector(apriVersionePRO:) forControlEvents:UIControlEventTouchDown]; return cell; } } break; default:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_1" forIndexPath:indexPath]; return cell; } break; } } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ switch (indexPath.row) { case 3: [self apriPriorita:nil]; break; default: break; } } -(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ dispatch_async(dispatch_get_main_queue(), ^{ [self setUpView]; }); completionHandler(UIBackgroundFetchResultNewData); } -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{ if ([annotation isKindOfClass:[PastquakesAnnotation class]]) { PastquakesAnnotation *anLocation = (PastquakesAnnotation *)annotation; MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:IDENTIFIER_ANNOTATION_PASTQUAKES]; if (annotationView == nil) annotationView = anLocation.annotationView; else annotationView.annotation = anLocation; return annotationView; } else return nil; } - (void)share:(id)sender { NSString *appName = [NSProcessInfo processInfo].processName; NSString *url=@"https://itunes.apple.com/app/id1449893235?mt=8"; NSString * title =[NSString stringWithFormat:@"%@%@ %@",NSLocalizedString(@"Scarica ", @""),appName,url]; [self presentViewController: [EQNUtility shareFileWithPath:title] animated:YES completion:^{}]; } -(void)visualizzaCopertura{ [[EQNManager defaultManager] sincronizza]; EQNAreaCheck *areaCheck = [EQNManager defaultManager].area_check; UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Copertura" , @"") message:[NSString stringWithFormat:@"%@ %@ %@", NSLocalizedString(@"Dispositivi totali", @""), areaCheck.total, NSLocalizedString(@"Dispositivi attivi", @"")] preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK" ,@"") style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; } -(void)apriTwitter:(id)sender{ // TWITTER_LINK UIApplication *mySafari = [UIApplication sharedApplication]; NSURL *myURL = [[NSURL alloc]initWithString:TWITTER_LINK]; [mySafari openURL:myURL options:@{} completionHandler:nil]; } -(void)votaApp:(id)sender{ [SKStoreReviewController requestReview]; } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end