123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*****************************************************************************
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCSettingsTableViewController.h"
- #import "IASKSettingsReader.h"
- #import "IASKSpecifier.h"
- #define SettingsReUseIdentifier @"SettingsReUseIdentifier"
- #define SettingsHeaderReUseIdentifier @"SettingsHeaderReUseIdentifier"
- @interface VLCSettingsTableViewController ()
- {
- NSUserDefaults *_userDefaults;
- IASKSettingsReader *_settingsReader;
- }
- @end
- @implementation VLCSettingsTableViewController
- - (void)loadView
- {
- UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
- tableView.delegate = self;
- tableView.dataSource = self;
- [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SettingsReUseIdentifier];
- [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:SettingsHeaderReUseIdentifier];
- self.view = tableView;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.clearsSelectionOnViewWillAppear = YES;
- _settingsReader = [[IASKSettingsReader alloc] init];
- }
- - (NSString *)title
- {
- return NSLocalizedString(@"Settings", nil);
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return _settingsReader.numberOfSections;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [_settingsReader numberOfRowsForSection:section];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingsReUseIdentifier forIndexPath:indexPath];
- IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
- cell.textLabel.text = [specifier title];
- cell.detailTextLabel.text = [specifier subtitle];
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- return cell;
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- return [_settingsReader titleForSection:section];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:specifier.title
- message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- NSString *specifierType = specifier.type;
- if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
- NSUInteger count = [specifier multipleValuesCount];
- NSArray *titles = [specifier multipleTitles];
- NSUInteger indexOfPreferredAction = [[specifier multipleValues] indexOfObject:[_userDefaults objectForKey:[specifier key]]]; // FIXME: lookup correct value
- for (NSUInteger i = 0; i < count; i++) {
- id value = [[specifier multipleValues][i] copy];
- UIAlertAction *action = [UIAlertAction actionWithTitle:[_settingsReader titleForStringId:titles[i]]
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction * _Nonnull action) {
- [_userDefaults setObject:value forKey:[specifier key]];
- [_userDefaults synchronize];
- [self.tableView reloadData];
- }];
- [alertController addAction:action];
- if (i == indexOfPreferredAction)
- [alertController setPreferredAction:action];
- }
- } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
- UIAlertAction *onAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"On", nil)
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction * _Nonnull action) {
- [_userDefaults setBool:YES forKey:[specifier key]];
- [_userDefaults synchronize];
- [self.tableView reloadData];
- }];
- UIAlertAction *offAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Off", nil)
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction * _Nonnull action) {
- [_userDefaults setBool:NO forKey:[specifier key]];
- [_userDefaults synchronize];
- [self.tableView reloadData];
- }];
- [alertController addAction:onAction];
- [alertController addAction:offAction];
- [alertController setPreferredAction:[_userDefaults boolForKey:[specifier key]] ? onAction : offAction];
- }
- [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
- style:UIAlertActionStyleCancel
- handler:nil]];
- [self presentViewController:alertController animated:YES completion:nil];
- }
- @end
|