279 lines
12 KiB
Objective-C
279 lines
12 KiB
Objective-C
//
|
|
// SettingsSeismicNetworkAlertsViewController.m
|
|
// Earthquake Network
|
|
//
|
|
// Refactored by Andrea Busi 25/08/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
#import "SettingsSeismicNetworkAlertsViewController.h"
|
|
#import "EQNNotificeReteSismiche.h"
|
|
#import "EQNUtility.h"
|
|
#import "PickerViewController.h"
|
|
|
|
@interface SettingsSeismicNetworkAlertsViewController () <PickerViewDelegate, UITextFieldDelegate>
|
|
|
|
@property (nonatomic, strong) NSArray<SettingItem *> *settings;
|
|
@property (nonatomic, strong) NSArray<EQNGenericValue *> *dataSourceRaggioSisma;
|
|
@property (nonatomic, strong) NSArray<EQNGenericValue *> *dataSourceMagnitudoDeboli;
|
|
@property (nonatomic, strong) NSArray<EQNGenericValue *> *dataSourceMagnitudoForti;
|
|
|
|
@property (nonatomic, assign) BOOL notificationEnabled;
|
|
@property (nonatomic, assign) BOOL notificationNearEarthquakeEnabled;
|
|
@property (nonatomic, assign) BOOL notificationStrongEarthquakeEnabled;
|
|
|
|
@property (nonatomic, strong) EQNGenericValue *currentUserPositionRadius;
|
|
@property (nonatomic, strong) EQNGenericValue *currentSeismicEnergy;
|
|
@property (nonatomic, strong) EQNGenericValue *currentNearEarthquakeDistance;
|
|
@property (nonatomic, strong) EQNGenericValue *currentStrongEarthquakeDistance;
|
|
@end
|
|
|
|
@implementation SettingsSeismicNetworkAlertsViewController
|
|
|
|
static NSString * const SegueIdentifierListaEnti = @"ShowListaEnti";
|
|
|
|
|
|
typedef NS_ENUM(NSInteger, RowIdentifier) {
|
|
RowIdentifierAbilitaNotifiche = 0,
|
|
RowIdentifierRetiSismiche,
|
|
RowIdentifierRaggioPosizione,
|
|
RowIdentifierEnergiaSisma,
|
|
RowIdentifierTerremotiVicini,
|
|
RowIdentifierTerremotiViciniDistanza,
|
|
RowIdentifierTerremotiForti,
|
|
RowIdentifierTerremotiFortiDistanza
|
|
};
|
|
|
|
#pragma mark - View Lifecycle
|
|
|
|
- (void)viewDidLoad
|
|
{
|
|
[super viewDidLoad];
|
|
|
|
[self setupUI];
|
|
|
|
self.settings = @[
|
|
[[SettingItem alloc] initWithType:SettingTypeEnable title:NSLocalizedString(@"Abilitato", @"") subtitle:NSLocalizedString(@"Ricevi le notifiche dei sismi rilevati dalle agenzie nazionalie internazionali", @"") icon:nil segue:nil],
|
|
[[SettingItem alloc] initWithType:SettingTypeMultiValues title:NSLocalizedString(@"Reti sismiche", @"") subtitle:nil icon:nil segue:SegueIdentifierListaEnti],
|
|
[[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Raggio dalla tua posizione", @"") subtitle:nil icon:nil segue:nil],
|
|
[[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Energia sisma", @"") subtitle:nil icon:nil segue:nil],
|
|
[[SettingItem alloc] initWithType:SettingTypeEnable title:NSLocalizedString(@"Terremoti vicini", @"") subtitle:NSLocalizedString(@"Notifica sismi di qualsiasi magnitudo se la distanza è inferiore a", @"") icon:nil segue:nil],
|
|
[[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Distanza", @"") subtitle:nil icon:nil segue:nil],
|
|
[[SettingItem alloc] initWithType:SettingTypeEnable title:NSLocalizedString(@"Mostra sismi forti a qualsiasi distanza se di", @"") subtitle:nil icon:nil segue:nil],
|
|
[[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Magnitudo", @"") subtitle:nil icon:nil segue:nil]
|
|
];
|
|
|
|
self.dataSourceMagnitudoDeboli = [EQNData magitudoDeboli];
|
|
self.dataSourceRaggioSisma = [EQNData raggioSismi];
|
|
self.dataSourceMagnitudoForti = [EQNData magitudoForti];
|
|
|
|
[self setupView];
|
|
}
|
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
{
|
|
[super viewWillAppear:animated];
|
|
[self.tableView reloadData];
|
|
}
|
|
|
|
#pragma mark - Private
|
|
|
|
- (void)setupUI
|
|
{
|
|
self.tableView.estimatedRowHeight = 200.0;
|
|
self.tableView.rowHeight = UITableViewAutomaticDimension;
|
|
[self.tableView registerClass:[SettingSectionHeaderView class] forHeaderFooterViewReuseIdentifier:SettingSectionHeaderView.Identifier];
|
|
[self.tableView registerClass:[SettingEnableTableViewCell class] forCellReuseIdentifier:SettingEnableTableViewCell.Identifier];
|
|
[self.tableView registerClass:[SettingSliderTableViewCell class] forCellReuseIdentifier:SettingSliderTableViewCell.Identifier];
|
|
[self.tableView registerClass:[SettingMultivaluesTableViewCell class] forCellReuseIdentifier:SettingMultivaluesTableViewCell.Identifier];
|
|
}
|
|
|
|
- (void)setupView
|
|
{
|
|
self.notificationEnabled = [EQNNotificeReteSismiche center].isAbilitato;
|
|
self.notificationNearEarthquakeEnabled = [EQNNotificeReteSismiche center].isAbilitaVicini;
|
|
self.notificationStrongEarthquakeEnabled = [EQNNotificeReteSismiche center].isTerremortiForti;
|
|
|
|
// raggio dalla tua posizione
|
|
EQNGenericValue *raggioSisma = [EQNData raggioSismaFor:[EQNNotificeReteSismiche center].distanzaPosizione];
|
|
self.currentUserPositionRadius = raggioSisma;
|
|
|
|
// energia sisma
|
|
EQNGenericValue *energiaSisma = [EQNData magitudoDeboleFor:[EQNNotificeReteSismiche center].energiaSisma];
|
|
self.currentSeismicEnergy = energiaSisma;
|
|
|
|
// terremoti vicini
|
|
EQNGenericValue *terremotiVicini = [EQNData raggioSismaFor:[EQNNotificeReteSismiche center].distanzaVicini];
|
|
self.currentNearEarthquakeDistance = terremotiVicini;
|
|
|
|
// terremoti forti
|
|
EQNGenericValue *terremotiForti = [EQNData magitudoForteFor:[EQNNotificeReteSismiche center].energiaTerremotiForti];
|
|
self.currentStrongEarthquakeDistance = terremotiForti;
|
|
|
|
// enti
|
|
if (![EQNNotificeReteSismiche center].listaEnti) {
|
|
[EQNNotificeReteSismiche center].listaEnti = [EQNData.seismicNetworkAcronyms copy];
|
|
}
|
|
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
|
|
[self.tableView reloadData];
|
|
}
|
|
|
|
|
|
#pragma mark - Table view data source
|
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
|
|
{
|
|
SettingSectionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SettingSectionHeaderView.Identifier];
|
|
headerView.titleLabel.text = NSLocalizedString(@"Notifiche da reti sismiche", @"titolo impostazioni notifiche");
|
|
return headerView;
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
|
|
{
|
|
return SettingSectionHeaderView.Height;
|
|
}
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
{
|
|
return self.settings.count;
|
|
}
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
SettingItem *setting = self.settings[indexPath.row];
|
|
|
|
if (setting.type == SettingTypeEnable) {
|
|
SettingEnableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingEnableTableViewCell.Identifier forIndexPath:indexPath];
|
|
cell.titleLabel.text = setting.displayTitle;
|
|
cell.descriptionLabel.text = setting.subtitle;
|
|
|
|
if (indexPath.row == RowIdentifierAbilitaNotifiche) {
|
|
cell.toggleSwitch.on = self.notificationEnabled;
|
|
cell.valueChanged = ^(BOOL enabled) {
|
|
self.notificationEnabled = enabled;
|
|
[EQNNotificeReteSismiche center].isAbilitato = self.notificationEnabled;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
[self.tableView reloadData];
|
|
};
|
|
} else if (indexPath.row == RowIdentifierTerremotiVicini) {
|
|
cell.toggleSwitch.on = self.notificationNearEarthquakeEnabled;
|
|
cell.isDisabled = !self.notificationEnabled;
|
|
cell.valueChanged = ^(BOOL enabled) {
|
|
self.notificationNearEarthquakeEnabled = enabled;
|
|
[EQNNotificeReteSismiche center].isAbilitaVicini = self.notificationNearEarthquakeEnabled;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
[self.tableView reloadData];
|
|
};
|
|
} else if (indexPath.row == RowIdentifierTerremotiForti) {
|
|
cell.toggleSwitch.on = self.notificationStrongEarthquakeEnabled;
|
|
cell.isDisabled = !self.notificationEnabled;
|
|
cell.valueChanged = ^(BOOL enabled) {
|
|
self.notificationStrongEarthquakeEnabled = enabled;
|
|
[EQNNotificeReteSismiche center].isTerremortiForti = self.notificationStrongEarthquakeEnabled;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
[self.tableView reloadData];
|
|
};
|
|
}
|
|
|
|
return cell;
|
|
} else if (setting.type == SettingTypeMultiValues) {
|
|
SettingMultivaluesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingMultivaluesTableViewCell.Identifier forIndexPath:indexPath];
|
|
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
cell.isDisabled = !self.notificationEnabled;
|
|
cell.userInteractionEnabled = self.notificationEnabled;
|
|
cell.titleLabel.text = setting.title;
|
|
|
|
if (indexPath.row == RowIdentifierRetiSismiche) {
|
|
cell.valuesLabel.text = [self stringOfSelectedNetworks];
|
|
}
|
|
|
|
return cell;
|
|
} else if (setting.type == SettingTypeSlider) {
|
|
SettingSliderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingSliderTableViewCell.Identifier forIndexPath:indexPath];
|
|
cell.titleLabel.text = setting.displayTitle;
|
|
|
|
if (indexPath.row == RowIdentifierRaggioPosizione) {
|
|
cell.isDisabled = !self.notificationEnabled;
|
|
[cell configureSliderWith:self.dataSourceRaggioSisma current:self.currentUserPositionRadius];
|
|
cell.valueChanged = ^(EQNGenericValue *item) {
|
|
[self updateUserPositionRadius:item];
|
|
};
|
|
} else if (indexPath.row == RowIdentifierEnergiaSisma) {
|
|
cell.isDisabled = !self.notificationEnabled;
|
|
[cell configureSliderWith:self.dataSourceMagnitudoDeboli current:self.currentSeismicEnergy];
|
|
cell.valueChanged = ^(EQNGenericValue *item) {
|
|
[self updateSeismicEnergy:item];
|
|
};
|
|
} else if (indexPath.row == RowIdentifierTerremotiViciniDistanza) {
|
|
cell.isDisabled = !self.notificationEnabled || !self.notificationNearEarthquakeEnabled;
|
|
[cell configureSliderWith:self.dataSourceRaggioSisma current:self.currentNearEarthquakeDistance];
|
|
cell.valueChanged = ^(EQNGenericValue *item) {
|
|
[self updateNearEarthquakeDistance:item];
|
|
};
|
|
} else if (indexPath.row == RowIdentifierTerremotiFortiDistanza) {
|
|
cell.isDisabled = !self.notificationEnabled || !self.notificationStrongEarthquakeEnabled;
|
|
[cell configureSliderWith:self.dataSourceMagnitudoForti current:self.currentStrongEarthquakeDistance];
|
|
cell.valueChanged = ^(EQNGenericValue *item) {
|
|
[self updateStrongEarthquakeEnergy:item];
|
|
};
|
|
}
|
|
|
|
return cell;
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
SettingItem *setting = self.settings[indexPath.row];
|
|
if (setting.segue != nil) {
|
|
[self performSegueWithIdentifier:setting.segue sender:nil];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Private
|
|
|
|
- (void)updateUserPositionRadius:(EQNGenericValue *)radius
|
|
{
|
|
[EQNNotificeReteSismiche center].distanzaPosizione = radius.value;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
|
|
[self setupView];
|
|
}
|
|
|
|
- (void)updateSeismicEnergy:(EQNGenericValue *)energy
|
|
{
|
|
[EQNNotificeReteSismiche center].energiaSisma = energy.value;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
|
|
[self setupView];
|
|
}
|
|
|
|
- (void)updateNearEarthquakeDistance:(EQNGenericValue *)distance
|
|
{
|
|
[EQNNotificeReteSismiche center].distanzaVicini = distance.value;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
|
|
[self setupView];
|
|
}
|
|
|
|
- (void)updateStrongEarthquakeEnergy:(EQNGenericValue *)energy
|
|
{
|
|
[EQNNotificeReteSismiche center].energiaTerremotiForti = energy.value;
|
|
[[EQNNotificeReteSismiche center] saveUserInfo];
|
|
|
|
[self setupView];
|
|
}
|
|
|
|
- (NSString *)stringOfSelectedNetworks
|
|
{
|
|
NSArray *networks = [EQNNotificeReteSismiche center].listaEnti;
|
|
networks = [networks sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
|
|
return [networks componentsJoinedByString:@", "];
|
|
}
|
|
|
|
@end
|