// // SegnalazioniViewController.m // Earthquake Network // // Refactored by Andrea Busi on 04/10/2020 // Copyright © 2020 Earthquake Network. All rights reserved. // #import "SegnalazioniViewController.h" #import "ServerRequest.h" #import "EQNManager.h" #import "EQNUser.h" #import "EQNGeneratoreURLServer.h" #import "EQNUtility.h" @import SafariServices; @interface SegnalazioniViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic, strong) NSUserDefaults *userDefoult; @end @implementation SegnalazioniViewController #pragma mark - View Lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.userDefoult = [NSUserDefaults standardUserDefaults]; [self setupUI]; [self refreshUI]; } #pragma mark - Private - (void)setupUI { self.title = NSLocalizedString(@"tab_manual", nil); self.tableView.estimatedRowHeight = 500.0; self.tableView.rowHeight = UITableViewAutomaticDimension; } - (void)refreshUI { [super refreshUI]; if ([self.userDefoult objectForKey:DATA_MESSAGE_EQN]){ NSDate *dateMessage = [self.userDefoult objectForKey:DATA_MESSAGE_EQN]; if ([EQNUtility getDifferenceMinute:dateMessage] >= EQNSendReportDelayBetweenComments){ [self.userDefoult removeObjectForKey:DATA_MESSAGE_EQN]; [self.userDefoult removeObjectForKey:CODE_MESSAGE_EQN]; } } [self.tableView reloadData]; } - (void)sincronizzazione { [self.tableView reloadData]; [[EQNManager defaultManager] sincronizza]; } #pragma mark - Table view data source & delegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 0: return 150; default: return UITableViewAutomaticDimension; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { SegnalazioniLast24HoursCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Last24HCell" forIndexPath:indexPath]; EQNReteSmartphone *reteSmartPhone = [EQNManager defaultManager].rete_smartphone; [cell updateUIFor:reteSmartPhone]; return cell; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportEarthquakeCell" forIndexPath:indexPath]; UILabel *verdeLabel = (UILabel *)[cell viewWithTag:2]; verdeLabel.layer.borderColor = AppTheme.shared.buttonBorderColor.CGColor; verdeLabel.layer.borderWidth = AppTheme.shared.buttonBorderWidth; verdeLabel.layer.cornerRadius = AppTheme.shared.buttonCornerRadius; verdeLabel.clipsToBounds = YES; UILabel *giallaLabel = (UILabel *)[cell viewWithTag:3]; giallaLabel.layer.borderColor = AppTheme.shared.buttonBorderColor.CGColor; giallaLabel.layer.borderWidth = AppTheme.shared.buttonBorderWidth; giallaLabel.layer.cornerRadius = AppTheme.shared.buttonCornerRadius; giallaLabel.clipsToBounds = YES; UILabel *rossaLabel = (UILabel *)[cell viewWithTag:4]; rossaLabel.layer.borderColor = AppTheme.shared.buttonBorderColor.CGColor; rossaLabel.layer.borderWidth = AppTheme.shared.buttonBorderWidth; rossaLabel.layer.cornerRadius = AppTheme.shared.buttonCornerRadius; rossaLabel.clipsToBounds = YES; return cell; } #pragma mark - Actions - (IBAction)openMapTapped:(id)sender { SegnalazioniMapViewController *controller = [[SegnalazioniMapViewController alloc] init]; [self presentViewController:controller animated:YES completion:nil]; } - (IBAction)openTwitterTapped:(id)sender { NSURL *twitterUrl = [NSURL URLWithString:EQNTwitterProfileUrl]; SFSafariViewController *controller = [[SFSafariViewController alloc] initWithURL:twitterUrl]; [self presentViewController:controller animated:YES completion:nil]; } - (IBAction)openTelegramTapped:(id)sender { NSURL *telegramUrl = [NSURL URLWithString:EQNTelegramUrl]; [[UIApplication sharedApplication] openURL:telegramUrl options:@{} completionHandler:nil]; } - (IBAction)refreshDataTapped:(id)sender { [[EQNManager defaultManager] sincronizza]; } - (IBAction)sendReportTapped:(UIButton *)sender { // check to avoid multiple consecutive reports if ([self.userDefoult objectForKey:CODE_MESSAGE_EQN]) { NSDate *dateMessage = [self.userDefoult objectForKey:DATA_MESSAGE_EQN]; if ([EQNUtility getDifferenceMinute:dateMessage] <= EQNSendReportDelayBetweenMessages){ NSString *message = NSLocalizedString(@"Il messaggio è già stato in viato, al momento non è possibile inviare una nuova segnalazione", @""); [self showErrorAlertWithMessage:message]; [self performSelectorOnMainThread:@selector(sincronizzazione) withObject:nil waitUntilDone:YES]; return; } } // ask for user confirmation UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"") message:NSLocalizedString(@"manual_sure", nil) preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"manual_yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self executeSendReportWithMagnitude:sender.tag]; }]]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Annulla", nil) style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; } - (void)executeSendReportWithMagnitude:(NSInteger)magnitude { CLGeocoder *geocoder = [CLGeocoder new]; [geocoder reverseGeocodeLocation:[EQNUser defaultUser].lastPosition completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSString *message = NSLocalizedString(@"La tua posizione geografica è sconosciuta e non è possibile segnalare il sisma", @""); [self showErrorAlertWithMessage:message]; return; } // Check if any placemarks were found if (placemarks && placemarks.count > 0) { CLPlacemark *placemark = placemarks[0]; NSString *address = [NSString stringWithFormat:@"%@-%@", placemark.locality, placemark.country]; NSURL *url = [EQNGeneratoreURLServer urlInvioMessagioTerremoto:magnitude withAdress:address]; [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:EQNTipoChiamataSegnalazioneTerremoto success:^(id result) { [self.userDefoult setObject:result forKey:CODE_MESSAGE_EQN]; [self.userDefoult setObject:[NSDate date] forKey:DATA_MESSAGE_EQN]; dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Segnalazione", @"") message:NSLocalizedString(@"La tua segnalazione è stata inviata correttamente", @"") preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self sendComment]; }]]; [self presentViewController:alert animated:YES completion:nil]; }); } failure:^(NSError * error) { [self showErrorAlertWithMessage:error.localizedDescription]; }]; } }]; } - (void)sendComment { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Messaggio" , @"") message:NSLocalizedString(@"manual_sendmessage", @"") preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.clearButtonMode = UITextFieldViewModeWhileEditing; }]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK" ,@"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UITextField * messaggio = alertController.textFields.firstObject; NSURL *url = [EQNGeneratoreURLServer urlInvioCommentoTerremoto:messaggio.text codeMessage:[self.userDefoult objectForKey:CODE_MESSAGE_EQN]]; [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:EQNTipoChiamataCommentoTerremoto success:^(id result) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Messaggio" , @"") message:NSLocalizedString(@"manual_message_received", @"") preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; }); } failure:^(NSError * error) { [self showErrorAlertWithMessage:error.localizedDescription]; }]; dispatch_async(dispatch_get_main_queue(), ^{ [self refreshUI]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Annulla", @"") style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; } #pragma mark - Private - (void)showErrorAlertWithMessage:(NSString *)errorMessage { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"") message:errorMessage preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; }); } @end