VLCSettingsTableViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:SettingsHeaderReUseIdentifier];
  29. self.view = tableView;
  30. }
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.clearsSelectionOnViewWillAppear = YES;
  34. _userDefaults = [NSUserDefaults standardUserDefaults];
  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];
  51. if (!cell)
  52. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SettingsReUseIdentifier];
  53. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  54. cell.textLabel.text = [specifier title];
  55. NSString *specifierType = specifier.type;
  56. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  57. NSArray *titles = [specifier multipleTitles];
  58. NSArray *values = [specifier multipleValues];
  59. NSUInteger selectedIndex = [values indexOfObject:[_userDefaults objectForKey:[specifier key]]];
  60. NSUInteger titlesCount = titles.count;
  61. if (selectedIndex < titlesCount)
  62. cell.detailTextLabel.text = [_settingsReader titleForStringId:titles[selectedIndex]];
  63. else {
  64. selectedIndex = [values indexOfObject:[specifier defaultValue]];
  65. if (selectedIndex < titlesCount)
  66. cell.detailTextLabel.text = [_settingsReader titleForStringId:titles[selectedIndex]];
  67. }
  68. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  69. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  70. cell.detailTextLabel.text = [_userDefaults boolForKey:[specifier key]] ? NSLocalizedString(@"ON", nil) : NSLocalizedString(@"OFF", nil);
  71. cell.accessoryType = UITableViewCellAccessoryNone;
  72. } else {
  73. cell.detailTextLabel.text = @"";
  74. cell.accessoryType = UITableViewCellAccessoryNone;
  75. }
  76. return cell;
  77. }
  78. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  79. {
  80. return [_settingsReader titleForSection:section];
  81. }
  82. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  85. NSString *specifierType = specifier.type;
  86. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  87. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:specifier.title
  88. message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  89. NSUInteger count = [specifier multipleValuesCount];
  90. NSArray *titles = [specifier multipleTitles];
  91. NSUInteger indexOfPreferredAction = [[specifier multipleValues] indexOfObject:[_userDefaults objectForKey:[specifier key]]];
  92. for (NSUInteger i = 0; i < count; i++) {
  93. id value = [[specifier multipleValues][i] copy];
  94. UIAlertAction *action = [UIAlertAction actionWithTitle:[_settingsReader titleForStringId:titles[i]]
  95. style:UIAlertActionStyleDefault
  96. handler:^(UIAlertAction * _Nonnull action) {
  97. [_userDefaults setObject:value forKey:[specifier key]];
  98. [_userDefaults synchronize];
  99. [self.tableView reloadData];
  100. }];
  101. [alertController addAction:action];
  102. if (i == indexOfPreferredAction)
  103. [alertController setPreferredAction:action];
  104. }
  105. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  106. style:UIAlertActionStyleCancel
  107. handler:nil]];
  108. [self presentViewController:alertController animated:YES completion:nil];
  109. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  110. NSString *specifierKey = [specifier key];
  111. [_userDefaults setBool:![_userDefaults boolForKey:specifierKey] forKey:specifierKey];
  112. [_userDefaults synchronize];
  113. [self.tableView reloadData];
  114. }
  115. }
  116. @end