// // SegnalazioniViewController.m // Earthquake Network // // Created by Luca Beretta on 17/10/18. // Copyright © 2018 Luca Beretta. All rights reserved. // #import "SegnalazioniViewController.h" #import "ServerRequest.h" #import "EQNSegnalazione.h" #import "EQNManager.h" #import "EQNUser.h" #import "EQNGeneratoreURLServer.h" #import "EQNUtility.h" @interface SegnalazioniViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic, assign) int verde; @property (nonatomic, assign) int giallo; @property (nonatomic, assign) int rosso; @property (nonatomic, strong) NSUserDefaults *userDefoult; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *fooTable; - (IBAction)inviaMessaggio:(id)sender; @end @implementation SegnalazioniViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aggiornaTabella:) name:NOTIFICA_DOWNLOAD_TERMINATO object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupView) name:IAPHelperPurchaseNotification object:nil]; self.verde = 0; self.giallo = 0; self.rosso = 0; [self setupView]; [self.tableView setContentInset:UIEdgeInsetsMake(20,0,0,0)]; self.userDefoult = [NSUserDefaults standardUserDefaults]; } -(void)aggiornaTabella:(id)sender{ [self setupView]; } -(void)setupView{ if([self.userDefoult objectForKey:DATA_MESSAGE_EQN]){ NSDate *dateMessage = [self.userDefoult objectForKey:DATA_MESSAGE_EQN]; if ([EQNUtility getDifferenceMinute:dateMessage] >= TEMPO_INVIO_COMMENTO){ [self.userDefoult removeObjectForKey:DATA_MESSAGE_EQN]; [self.userDefoult removeObjectForKey:CODE_MESSAGE_EQN]; } } self.verde = 0; self.giallo = 0; self.rosso = 0; /* for(EQNSegnalazione *segn in [EQNManager defaultManager].elencoSelagnazioniManuali){ if ([segn.magnitude intValue] == 1) self.verde++; if ([segn.magnitude intValue] == 2) self.giallo++; if ([segn.magnitude intValue] == 3) self.rosso++; } */ if([[NSUserDefaults standardUserDefaults] objectForKey:AQUISTO_PRO] || [[NSUserDefaults standardUserDefaults] objectForKey:AQUISTO_PRO_SCONTATO]) self.fooTable.constant = 0; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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. } */ - (IBAction)apriMappa:(id)sender { } #pragma table view - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ switch (indexPath.row) { case 0: return 140; break; default: return 446; break; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // modalita base EQNReteSmartphone *reteSmartPhone = [EQNManager defaultManager].rete_smartphone; switch (indexPath.row) { case 0:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"primaCell" forIndexPath:indexPath]; UILabel *verdeLabel = (UILabel *)[cell viewWithTag:1]; verdeLabel.text = [NSString stringWithFormat:@"%@", reteSmartPhone.g_man]; UILabel *giallaLabel = (UILabel *)[cell viewWithTag:2]; giallaLabel.text = [NSString stringWithFormat:@"%@", reteSmartPhone.y_man]; UILabel *rossaLabel = (UILabel *)[cell viewWithTag:3]; rossaLabel.text = [NSString stringWithFormat:@"%@", reteSmartPhone.r_man]; UIButton *mappaButton = (UIButton *)[cell viewWithTag:4]; [mappaButton setTitle:[NSLocalizedString(@"Mappa", @"") uppercaseString] forState:UIControlStateNormal]; mappaButton.layer.borderColor = [UIColor darkGrayColor].CGColor; mappaButton.layer.borderWidth = 1.5; mappaButton.layer.cornerRadius = 4; return cell; } break; default:{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondaCell" forIndexPath:indexPath]; UILabel *titolo = (UILabel *)[cell viewWithTag:1]; titolo.text = NSLocalizedString(@"Usa il bottone per segnalare il sisma", @""); UILabel *verdeLabel = (UILabel *)[cell viewWithTag:2]; verdeLabel.text = NSLocalizedString(@"LEGGERO\n(Solo percepito)", @""); verdeLabel.layer.borderColor = [UIColor darkGrayColor].CGColor; verdeLabel.layer.borderWidth = 1.5; verdeLabel.layer.cornerRadius = 8; verdeLabel.clipsToBounds = YES; UILabel *giallaLabel = (UILabel *)[cell viewWithTag:3]; giallaLabel.text = NSLocalizedString(@"FORTE\n(Caduta di oggetti)", @""); giallaLabel.layer.borderColor = [UIColor darkGrayColor].CGColor; giallaLabel.layer.borderWidth = 1.5; giallaLabel.layer.cornerRadius = 8; giallaLabel.clipsToBounds = YES; UILabel *rossaLabel = (UILabel *)[cell viewWithTag:4]; rossaLabel.text = NSLocalizedString(@"MOLTO FORTE\n(Crollo di edifici)", @""); rossaLabel.layer.borderColor = [UIColor darkGrayColor].CGColor; rossaLabel.layer.borderWidth = 1.5; rossaLabel.layer.cornerRadius = 8; rossaLabel.clipsToBounds = YES; UILabel *info = (UILabel *)[cell viewWithTag:5]; info.text = NSLocalizedString(@"Invia un messaggio che gli altri utenti possono leggere sul sisma che hai segnalato", @""); UIButton *mappaButton = (UIButton *)[cell viewWithTag:6]; [mappaButton setTitle:[NSLocalizedString(@"Invia messaggio", @"") uppercaseString] forState:UIControlStateNormal]; mappaButton.layer.borderColor = [UIColor darkGrayColor].CGColor; mappaButton.layer.borderWidth = 1.5; mappaButton.layer.cornerRadius = 4; [mappaButton addTarget:self action:@selector(aggiungCommento:) forControlEvents:UIControlEventTouchDown]; if([self.userDefoult objectForKey:CODE_MESSAGE_EQN]){ mappaButton.enabled = YES; [mappaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; }else{ mappaButton.enabled = NO; [mappaButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; } return cell; } break; } } - (IBAction)inviaMessaggio:(id)sender { if ([self.userDefoult objectForKey:CODE_MESSAGE_EQN]) { NSDate *dateMessage = [self.userDefoult objectForKey:DATA_MESSAGE_EQN]; if ([EQNUtility getDifferenceMinute:dateMessage] <= TEMPO_INVIO_MESSAGGIO){ UIAlertController *messaggio = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione ", @"") message:NSLocalizedString(@"Il messaggio è già stato in viato, al momento non è possibile inviare una nuova segnalazione", @"") preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { return ; }]; [messaggio addAction:defaultAction]; [self presentViewController:messaggio animated:YES completion:nil]; [self performSelectorOnMainThread:@selector(sincronizzazione) withObject:nil waitUntilDone:YES]; return; } } UIButton *button = (UIButton *)sender; CLGeocoder *geocoder = [CLGeocoder new]; [geocoder reverseGeocodeLocation:[EQNUser defaultUser].lastPosition completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *errore = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"") message:NSLocalizedString(@"La tua posizione geografica è sconosciuta e non è possibile segnalare il sisma", @"") preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { return ; }]; [errore addAction:defaultAction]; [self presentViewController:errore animated:YES completion:nil]; }); } // Check if any placemarks were found if (placemarks && placemarks.count > 0) { CLPlacemark *placemark = placemarks[0]; // Dictionary containing address information // NSDictionary *addressDictionary = placemark.addressDictionary; NSString *address = [NSString stringWithFormat:@"%@-%@", placemark.locality, placemark.country]; NSURL *url = [EQNGeneratoreURLServer urlInvioMessagioTerremoto:button.tag withAdress:address]; [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:segnalazzioneTerremoto success:^(id result) { [self.userDefoult setObject:result forKey:CODE_MESSAGE_EQN]; [self.userDefoult setObject:[NSDate date] forKey:DATA_MESSAGE_EQN]; UIAlertController *messaggio = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Segnalazione ", @"") message:NSLocalizedString(@"La tua segnalazione è stata inviata correttamente", @"") preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { return ; }]; [messaggio addAction:defaultAction]; [self presentViewController:messaggio animated:YES completion:nil]; [self performSelectorOnMainThread:@selector(sincronizzazione) withObject:nil waitUntilDone:YES]; } failure:^(NSError * error) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *errore = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"") message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { return ; }]; [errore addAction:defaultAction]; [self presentViewController:errore animated:YES completion:nil]; }); }]; } }]; } -(void)aggiungCommento:(id)sender{ UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Messaggio" , @"") message: @"" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.textColor = [UIColor blueColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; }]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK" ,@"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSArray * textfields = alertController.textFields; UITextField * messaggio = textfields[0]; NSURL *url = [EQNGeneratoreURLServer urlInvioCommentoTerremoto:messaggio.text codeMessage:[self.userDefoult objectForKey:CODE_MESSAGE_EQN]]; NSLog(@"url %@", url); [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:commentoTerremoto success:^(id result) { NSLog(@"Reseult %@",result); } failure:^(NSError * error) { }]; [self performSelectorOnMainThread:@selector(inviaCommento:) withObject:messaggio.text waitUntilDone:YES]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Annulla" ,@"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } -(void)inviaCommento:(NSString *)commento{ } -(void)sincronizzazione{ [self.tableView reloadData]; [[EQNManager defaultManager] sincronizza]; } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end