From ac553df6c6220b969fc0a32b00a8cd3d0b9c814c Mon Sep 17 00:00:00 2001 From: Andrea Busi Date: Sat, 15 Aug 2020 16:32:53 +0200 Subject: [PATCH] refactor: Improve code formatting --- .../SegnalazioniViewController.m | 142 +++++++----------- 1 file changed, 56 insertions(+), 86 deletions(-) diff --git a/Sources/Earthquake Network/ViewController/SegnalazioniViewController.m b/Sources/Earthquake Network/ViewController/SegnalazioniViewController.m index ea1d408..32dc47f 100644 --- a/Sources/Earthquake Network/ViewController/SegnalazioniViewController.m +++ b/Sources/Earthquake Network/ViewController/SegnalazioniViewController.m @@ -202,105 +202,75 @@ } } -- (IBAction)inviaMessaggio:(id)sender { - + +- (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]; + UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione ", @"") + message:NSLocalizedString(@"Il messaggio è già stato in viato, al momento non è possibile inviare una nuova segnalazione", @"") + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [self presentViewController:alert 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]; - - }); - - }]; - - } - - }]; + [geocoder reverseGeocodeLocation:[EQNUser defaultUser].lastPosition completionHandler:^(NSArray *placemarks, NSError *error) { + if (error) { + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"") + message:NSLocalizedString(@"La tua posizione geografica è sconosciuta e non è possibile segnalare il sisma", @"") + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [self presentViewController:alert animated:YES completion:nil]; + }); + + return; + } + + // 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 *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Segnalazione ", @"") + message:NSLocalizedString(@"La tua segnalazione è stata inviata correttamente", @"") + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [self presentViewController:alert animated:YES completion:nil]; + + [self performSelectorOnMainThread:@selector(sincronizzazione) withObject:nil waitUntilDone:YES]; + } failure:^(NSError * error) { + + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"") + message:error.localizedDescription + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [self presentViewController:alert animated:YES completion:nil]; + }); + }]; + } + }]; }