// // SettingsSeismicNetworkAlertsViewController.m // Earthquake Network // // Refactored by Andrea Busi 25/08/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // #import "SettingsSeismicNetworkAlertsViewController.h" #import "EQNNotificheReteSismiche.h" @interface SettingsSeismicNetworkAlertsViewController () @property (nonatomic, strong) NSArray *settings; @property (nonatomic, strong) NSArray *dataSourceRaggioSisma; @property (nonatomic, strong) NSArray *dataSourceMagnitudoDeboli; @property (nonatomic, strong) NSArray *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 *currentStrongEarthquakeDistance; @end @implementation SettingsSeismicNetworkAlertsViewController static NSString * const SegueIdentifierListaEnti = @"ShowListaEnti"; typedef NS_ENUM(NSInteger, RowIdentifier) { RowIdentifierAbilitaNotifiche = 0, RowIdentifierRetiSismiche, RowIdentifierRaggioPosizione, RowIdentifierEnergiaSisma, RowIdentifierTerremotiVicini, RowIdentifierTerremotiForti, RowIdentifierTerremotiFortiDistanza }; #pragma mark - View Lifecycle - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; self.settings = @[ [[SettingItem alloc] initWithType:SettingTypeEnable title:NSLocalizedString(@"Ricevi le notifiche dei sismi rilevati dalle agenzie nazionalie internazionali", @"")], [[SettingItem alloc] initWithType:SettingTypeMultiValues title:NSLocalizedString(@"Reti sismiche", @"") segue:SegueIdentifierListaEnti], [[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Raggio dalla tua posizione", @"")], [[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Energia sisma", @"")], [[SettingItem alloc] initWithType:SettingTypeEnable title:NSLocalizedString(@"Terremoti vicini", @"") subtitle:NSLocalizedString(@"Notifica sismi di qualsiasi magnitudo se la distanza è inferiore a 50 km", @"")], [[SettingItem alloc] initWithType:SettingTypeEnable title:NSLocalizedString(@"Terremoti forti", @"") subtitle:NSLocalizedString(@"Notifica sismi forti a qualsiasi distanza se la magnitudo è maggiore a", @"")], [[SettingItem alloc] initWithType:SettingTypeSlider title:NSLocalizedString(@"Magnitudo", @"")] ]; self.dataSourceMagnitudoDeboli = [EQNData magitudoDeboli]; self.dataSourceRaggioSisma = [EQNData raggioSismi]; self.dataSourceMagnitudoForti = [EQNData magitudoForti]; [self updateUI]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self updateUI]; } #pragma mark - Private - (void)setupUI { self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeNever; 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)updateUI { self.notificationEnabled = [EQNNotificheReteSismiche sharedInstance].isAbilitato; self.notificationNearEarthquakeEnabled = [EQNNotificheReteSismiche sharedInstance].isAbilitaVicini; self.notificationStrongEarthquakeEnabled = [EQNNotificheReteSismiche sharedInstance].isTerremortiForti; // raggio dalla tua posizione EQNGenericValue *raggioSisma = [EQNData raggioSismaFor:[EQNNotificheReteSismiche sharedInstance].distanzaPosizione]; self.currentUserPositionRadius = raggioSisma; // energia sisma EQNGenericValue *energiaSisma = [EQNData magitudoDeboleFor:[EQNNotificheReteSismiche sharedInstance].energiaSisma]; self.currentSeismicEnergy = energiaSisma; // terremoti forti EQNGenericValue *terremotiForti = [EQNData magitudoForteFor:[EQNNotificheReteSismiche sharedInstance].energiaTerremotiForti]; self.currentStrongEarthquakeDistance = terremotiForti; // enti if (![EQNNotificheReteSismiche sharedInstance].listaEnti) { [EQNNotificheReteSismiche sharedInstance].listaEnti = [EQNData.seismicNetworkAcronyms copy]; } [[EQNNotificheReteSismiche sharedInstance] 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; [EQNNotificheReteSismiche sharedInstance].isAbilitato = self.notificationEnabled; [[EQNNotificheReteSismiche sharedInstance] 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; [EQNNotificheReteSismiche sharedInstance].isAbilitaVicini = self.notificationNearEarthquakeEnabled; [[EQNNotificheReteSismiche sharedInstance] 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; [EQNNotificheReteSismiche sharedInstance].isTerremortiForti = self.notificationStrongEarthquakeEnabled; [[EQNNotificheReteSismiche sharedInstance] 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 == 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 { [EQNNotificheReteSismiche sharedInstance].distanzaPosizione = radius.value; [[EQNNotificheReteSismiche sharedInstance] saveUserInfo]; [self updateUI]; } - (void)updateSeismicEnergy:(EQNGenericValue *)energy { [EQNNotificheReteSismiche sharedInstance].energiaSisma = energy.value; [[EQNNotificheReteSismiche sharedInstance] saveUserInfo]; [self updateUI]; } - (void)updateStrongEarthquakeEnergy:(EQNGenericValue *)energy { [EQNNotificheReteSismiche sharedInstance].energiaTerremotiForti = energy.value; [[EQNNotificheReteSismiche sharedInstance] saveUserInfo]; [self updateUI]; } - (NSString *)stringOfSelectedNetworks { NSArray *networks = [EQNNotificheReteSismiche sharedInstance].listaEnti; networks = [networks sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; return [networks componentsJoinedByString:@", "]; } @end