Files
eqn.ios/Sources/Earthquake Network/Controllers/Reports/SegnalazioniViewController.m
T

201 lines
7.8 KiB
Objective-C

//
// 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 () <UITableViewDelegate, UITableViewDataSource>
@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) capitalizedString];
self.tableView.estimatedRowHeight = 500.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = EQNBaseContainerTableViewCell.EdgeInsets;
[self.tableView registerClass:[SegnalazioniLast24HoursCell class] forCellReuseIdentifier:@"Last24HCell"];
[self.tableView registerClass:[SegnalazioniSendReportCell class] forCellReuseIdentifier:@"ReportEarthquakeCell"];
}
- (void)refreshUI
{
[super refreshUI];
if ([self.userDefoult objectForKey:NSUserDefaults.UserReportMessage]){
NSDate *dateMessage = [self.userDefoult objectForKey:NSUserDefaults.UserReportMessage];
if (![dateMessage isBeforeInterval:EQNSendReportDelayBetweenComments]) {
[self.userDefoult removeObjectForKey:NSUserDefaults.UserReportMessage];
[self.userDefoult removeObjectForKey:NSUserDefaults.UserReportCodeStatus];
}
}
[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;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
SegnalazioniLast24HoursCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Last24HCell" forIndexPath:indexPath];
[cell updateWith:[EQNManager defaultManager].rete_smartphone];
__weak SegnalazioniViewController *weakSelf = self;
cell.onTapMap = ^{
[weakSelf openMap];
};
cell.onTapTwitter = ^{
[weakSelf openTwitter];
};
cell.onTapTelegram = ^{
[weakSelf openTelegram];
};
return cell;
}
SegnalazioniSendReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportEarthquakeCell" forIndexPath:indexPath];
cell.onTapReport = ^(NSInteger magnitude) {
[self sendReportTappedWithMagnitude:magnitude];
};
return cell;
}
#pragma mark - Actions
- (void)openMap
{
SegnalazioniMapViewController *controller = [[SegnalazioniMapViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:navController animated:YES completion:nil];
}
- (void)openTwitter
{
NSURL *twitterUrl = [NSURL URLWithString:EQNTwitterProfileUrl];
SFSafariViewController *controller = [[SFSafariViewController alloc] initWithURL:twitterUrl];
[self presentViewController:controller animated:YES completion:nil];
}
- (void)openTelegram
{
NSURL *telegramUrl = [NSURL URLWithString:EQNTelegramUrl];
[[UIApplication sharedApplication] openURL:telegramUrl options:@{} completionHandler:nil];
}
- (IBAction)refreshDataTapped:(id)sender
{
[[EQNManager defaultManager] sincronizza];
}
- (void)sendReportTappedWithMagnitude:(NSInteger)magnitude
{
// check to avoid multiple consecutive reports
if ([self.userDefoult objectForKey:NSUserDefaults.UserReportCodeStatus]) {
NSDate *dateMessage = [self.userDefoult objectForKey:NSUserDefaults.UserReportMessage];
if (![dateMessage isBeforeInterval:EQNSendReportDelayBetweenMessages]) {
NSString *message = NSLocalizedString(@"manual_wait", @"");
[self showErrorAlertWithMessage:message];
[self performSelectorOnMainThread:@selector(sincronizzazione) withObject:nil waitUntilDone:YES];
return;
}
}
// ask for user confirmation
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"attention", @"") message:NSLocalizedString(@"manual_sure", nil) preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"manual_yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self executeSendReportWithMagnitude:magnitude];
}]];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"status_cancel", 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(@"manual_nolocation", @"");
[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:NSUserDefaults.UserReportCodeStatus];
[self.userDefoult setObject:[NSDate date] forKey:NSUserDefaults.UserReportMessage];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"report", @"")
message:NSLocalizedString(@"manual_ok", @"")
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
});
} failure:^(NSError * error) {
[self showErrorAlertWithMessage:error.localizedDescription];
}];
}
}];
}
#pragma mark - Private
- (void)showErrorAlertWithMessage:(NSString *)errorMessage
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"attention", @"")
message:errorMessage
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
});
}
@end