VLCSettingsTableViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCSettingsTableViewController.h"
  12. #import "IASKSettingsReader.h"
  13. #import "IASKSpecifier.h"
  14. #define SettingsReUseIdentifier @"SettingsReUseIdentifier"
  15. #define SettingsHeaderReUseIdentifier @"SettingsHeaderReUseIdentifier"
  16. @interface VLCSettingsTableViewController ()
  17. {
  18. NSUserDefaults *_userDefaults;
  19. IASKSettingsReader *_settingsReader;
  20. }
  21. @end
  22. @implementation VLCSettingsTableViewController
  23. - (void)loadView
  24. {
  25. UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
  26. tableView.delegate = self;
  27. tableView.dataSource = self;
  28. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SettingsReUseIdentifier];
  29. [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:SettingsHeaderReUseIdentifier];
  30. self.view = tableView;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.clearsSelectionOnViewWillAppear = YES;
  35. _settingsReader = [[IASKSettingsReader alloc] init];
  36. }
  37. - (NSString *)title
  38. {
  39. return NSLocalizedString(@"Settings", nil);
  40. }
  41. #pragma mark - Table view data source
  42. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  43. return _settingsReader.numberOfSections;
  44. }
  45. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  46. {
  47. return [_settingsReader numberOfRowsForSection:section];
  48. }
  49. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  50. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingsReUseIdentifier forIndexPath:indexPath];
  51. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  52. cell.textLabel.text = [specifier title];
  53. cell.detailTextLabel.text = [specifier subtitle];
  54. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  55. return cell;
  56. }
  57. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  58. {
  59. return [_settingsReader titleForSection:section];
  60. }
  61. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  62. {
  63. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  64. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:specifier.title
  65. message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  66. NSString *specifierType = specifier.type;
  67. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  68. NSUInteger count = [specifier multipleValuesCount];
  69. NSArray *titles = [specifier multipleTitles];
  70. NSUInteger indexOfPreferredAction = [[specifier multipleValues] indexOfObject:[_userDefaults objectForKey:[specifier key]]]; // FIXME: lookup correct value
  71. for (NSUInteger i = 0; i < count; i++) {
  72. id value = [[specifier multipleValues][i] copy];
  73. UIAlertAction *action = [UIAlertAction actionWithTitle:[_settingsReader titleForStringId:titles[i]]
  74. style:UIAlertActionStyleDefault
  75. handler:^(UIAlertAction * _Nonnull action) {
  76. [_userDefaults setObject:value forKey:[specifier key]];
  77. [_userDefaults synchronize];
  78. [self.tableView reloadData];
  79. }];
  80. [alertController addAction:action];
  81. if (i == indexOfPreferredAction)
  82. [alertController setPreferredAction:action];
  83. }
  84. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  85. UIAlertAction *onAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"On", nil)
  86. style:UIAlertActionStyleDefault
  87. handler:^(UIAlertAction * _Nonnull action) {
  88. [_userDefaults setBool:YES forKey:[specifier key]];
  89. [_userDefaults synchronize];
  90. [self.tableView reloadData];
  91. }];
  92. UIAlertAction *offAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Off", nil)
  93. style:UIAlertActionStyleDefault
  94. handler:^(UIAlertAction * _Nonnull action) {
  95. [_userDefaults setBool:NO forKey:[specifier key]];
  96. [_userDefaults synchronize];
  97. [self.tableView reloadData];
  98. }];
  99. [alertController addAction:onAction];
  100. [alertController addAction:offAction];
  101. [alertController setPreferredAction:[_userDefaults boolForKey:[specifier key]] ? onAction : offAction];
  102. }
  103. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  104. style:UIAlertActionStyleCancel
  105. handler:nil]];
  106. [self presentViewController:alertController animated:YES completion:nil];
  107. }
  108. @end