Files
eqn.ios/Sources/Earthquake Network/Controllers/Alerts/AllerteViewController.m
T
2021-04-06 22:07:44 +02:00

350 lines
13 KiB
Objective-C

//
// AllerteViewController.m
// Earthquake Network
//
// Refactored by Andrea Busi on 04/10/2020
// Copyright © 2020 Earthquake Network. All rights reserved.
//
#import "AllerteViewController.h"
#import "Costanti.h"
#import "ServerRequest.h"
#import "EQNGeneratoreURLServer.h"
#import "EQNManager.h"
#import "EQNUser.h"
#import "EQNAreaCheck.h"
@import StoreKit;
@import SafariServices;
@import CoreLocation;
@import GoogleMobileAds;
@interface AllerteViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableItems;
@property (nonatomic) BOOL isNotificaAttiva;
@end
@implementation AllerteViewController
static NSString * const SegueIdentifierProVersion = @"ShowProVersion";
static NSString * const SegueIdentifierPrioritySubscriptions = @"ShowPrioritySubscriptions";
/// Sections inside the app
typedef NS_ENUM(NSInteger, AllerteTableRow) {
AllerteTableRowLocationPermission = 0,
AllerteTableRowSismiRilevati,
AllerteTableRowAllertePassate,
AllerteTableRowReteSmartphone,
AllerteTableRowServizioPriorita,
AllerteTableRowVersionePro,
AllerteTableRowDatiPosizione
};
#pragma mark - Accessories
- (NSMutableArray *)tableItems
{
if (!_tableItems) {
_tableItems = [[NSMutableArray alloc] init];
}
return _tableItems;
}
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
[self refreshUI];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForegroundNotification:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeAuthorizationStatusNotification:)
name:EQNAuthorizationStatusDidChangeNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self refreshUI];
}
#pragma mark - Notification
- (void)applicationWillEnterForegroundNotification:(NSNotification *)notification
{
[self refreshUI];
}
- (void)didChangeAuthorizationStatusNotification:(NSNotification *)notification
{
[self refreshUI];
}
#pragma mark - Public
- (void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
dispatch_async(dispatch_get_main_queue(), ^{
[self refreshUI];
});
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - Private
- (void)setupUI
{
self.title = NSLocalizedString(@"tab_network", nil);
self.tableView.estimatedRowHeight = 200.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (void)refreshUI
{
[super refreshUI];
NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
if (date) {
if ([EQNUtility getDifferenceMinute:date] < TEMPO_VISUALIZZAZIONE_NOTIFICA)
self.isNotificaAttiva = YES;
else{
self.isNotificaAttiva = NO;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
[self.tableItems removeAllObjects];
// check if locations is enabled
if (CLLocationManager.authorizationStatus != kCLAuthorizationStatusAuthorizedAlways) {
[self.tableItems addObject:@(AllerteTableRowLocationPermission)];
}
// always visible rows
[self.tableItems addObjectsFromArray:@[ @(AllerteTableRowSismiRilevati), @(AllerteTableRowAllertePassate), @(AllerteTableRowReteSmartphone), @(AllerteTableRowServizioPriorita)]];
// pro cell visible only if user hasn't already bought the app
if ([EQNPurchaseUtility isProVersionEnabled] == NO) {
[self.tableItems addObject:@(AllerteTableRowVersionePro)];
}
// location data visible only if last position is known
if ([EQNUser defaultUser].lastPosition != nil) {
[self.tableItems addObject:@(AllerteTableRowDatiPosizione)];
}
[self.tableView reloadData];
}
#pragma mark - Actions
- (IBAction)refreshDataTapped:(id)sender
{
[[EQNManager defaultManager] sincronizza];
}
- (void)actionCloseNotification
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA];
[[NSUserDefaults standardUserDefaults] synchronize];
self.isNotificaAttiva = NO;
[self.tableView reloadData];
}
- (void)actionShareApp
{
NSString *content = NSLocalizedString(@"main_share_text", @"");
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[content]
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
}
- (void)actionRateApp
{
[SKStoreReviewController requestReview];
}
- (void)actionOpenTwitter
{
NSURL *twitterUrl = [NSURL URLWithString:EQNTwitterProfileUrl];
SFSafariViewController *controller = [[SFSafariViewController alloc] initWithURL:twitterUrl];
[self presentViewController:controller animated:YES completion:nil];
}
- (void)actionOpenSimulator
{
AlertSimulatorViewController *controller = [[AlertSimulatorViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
}
- (void)actionHowItWorks
{
NSURL *youtubeUrl = [NSURL URLWithString:NSLocalizedString(@"youtube_video", nil)];
SFSafariViewController *controller = [[SFSafariViewController alloc] initWithURL:youtubeUrl];
[self presentViewController:controller animated:YES completion:nil];
}
- (void)actionTestPush
{
CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
if (status != kCLAuthorizationStatusAuthorizedAlways && status != kCLAuthorizationStatusAuthorizedWhenInUse) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", nil)
message:NSLocalizedString(@"liveview_unknown_location", nil)
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
NSURL *url = [EQNGeneratoreURLServer urlAlertPushTest];
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:EQNTipoChiamataAlertPushTest success:^(id result) {
// nope
} failure:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attenzione", @"")
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
});
}];
}
#pragma table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AllerteTableRow tableRow = [self.tableItems[indexPath.row] integerValue];
if (tableRow == AllerteTableRowLocationPermission) {
AlertsNoLocationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoLocationCell" forIndexPath:indexPath];
cell.status = CLLocationManager.authorizationStatus;
return cell;
} else if (tableRow == AllerteTableRowSismiRilevati) {
if (self.isNotificaAttiva) {
AlertsSeismicNotificationExpandedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeismicNotificationExpandedCell" forIndexPath:indexPath];
NSDictionary *info = [EQNUtility loadDictionaryFromUserDefaultsForKey:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA];
cell.notification = info;
__weak AllerteViewController *weakSelf = self;
cell.onTapClose = ^{
[weakSelf actionCloseNotification];
};
cell.onTapShareApp = ^{
[weakSelf actionShareApp];
};
cell.onTapRateApp = ^{
[weakSelf actionRateApp];
};
cell.onTapOpenTwitter = ^{
[weakSelf actionOpenTwitter];
};
return cell;
}
AlertsSeismicNotificationCompactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeismicNotificationCell" forIndexPath:indexPath];
__weak AllerteViewController *weakSelf = self;
cell.onTapAlertTest = ^{
[weakSelf actionTestPush];
};
cell.onTapSimulator = ^{
[weakSelf actionOpenSimulator];
};
cell.onTapHowItWorks = ^{
[weakSelf actionHowItWorks];
};
cell.onTapShareApp = ^{
[weakSelf actionShareApp];
};
return cell;
} else if (tableRow == AllerteTableRowAllertePassate) {
AlertsPastEartquakesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PastEarthquakesCell" forIndexPath:indexPath];
cell.smartphoneNetwork = [EQNManager defaultManager].rete_smartphone;
cell.onTapMapButton = ^{
PasquakesMapViewController *controller = [[PasquakesMapViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
};
return cell;
} else if (tableRow == AllerteTableRowReteSmartphone) {
AlertsSmartphoneNetworkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SmartphoneNetworkCell" forIndexPath:indexPath];
cell.smartphoneNetwork = [EQNManager defaultManager].rete_smartphone;
cell.onTapButton = ^{
[self visualizzaCopertura];
};
return cell;
} else if (tableRow == AllerteTableRowServizioPriorita) {
AlertsPriorityServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PriorityCell" forIndexPath:indexPath];
cell.smartphoneNetwork = [EQNManager defaultManager].rete_smartphone;
return cell;
} else if (tableRow == AllerteTableRowVersionePro) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProVersionCell" forIndexPath:indexPath];
return cell;
} else if (tableRow == AllerteTableRowDatiPosizione) {
AlertsPositionDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PositionDataCell" forIndexPath:indexPath];
cell.position = [EQNUser defaultUser].lastPosition;
return cell;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
AllerteTableRow tableRow = [self.tableItems[indexPath.row] integerValue];
switch (tableRow) {
case AllerteTableRowServizioPriorita:
[self performSegueWithIdentifier:SegueIdentifierPrioritySubscriptions sender:nil];
break;
case AllerteTableRowVersionePro:
[self performSegueWithIdentifier:SegueIdentifierProVersion sender:nil];
break;
default:
break;
}
}
- (void)visualizzaCopertura
{
[[EQNManager defaultManager] sincronizza];
EQNAreaCheck *areaCheck = [EQNManager defaultManager].area_check;
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"main_areacheck_message", @""), areaCheck.total];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"main_coverage" , @"")
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK" ,@"")
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
@end