Files
eqn.ios/Sources/Earthquake Network/ViewController/SegnalazioniViewController.m
T

333 lines
13 KiB
Objective-C

//
// 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 () <UITableViewDelegate, UITableViewDataSource>
@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
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUI) name:NOTIFICA_DOWNLOAD_TERMINATO object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUI) name:IAPHelperPurchaseNotification object:nil];
self.title = NSLocalizedString(@"tab_manual", nil);
[self.tableView setContentInset:UIEdgeInsetsMake(20,0,0,0)];
self.verde = 0;
self.giallo = 0;
self.rosso = 0;
[self refreshUI];
self.userDefoult = [NSUserDefaults standardUserDefaults];
}
#pragma mark - Private
- (void)refreshUI
{
[super refreshUI];
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;
if ([EQNPurchaseUtility isProVersionEnabled]) {
self.fooTable.constant = 0;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
- (IBAction)apriMappa:(id)sender
{
}
- (IBAction)refreshDataTapped:(id)sender
{
[[EQNManager defaultManager] sincronizza];
}
#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 *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 *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];
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:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
[self sincronizzazione];
});
} 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];
});
}];
}
}];
}
-(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