VLCSettingsTableViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #import "VLCAboutViewController.h"
  15. #define SettingsReUseIdentifier @"SettingsReUseIdentifier"
  16. #define SettingsHeaderReUseIdentifier @"SettingsHeaderReUseIdentifier"
  17. @interface VLCSettingsTableViewController ()
  18. {
  19. NSUserDefaults *_userDefaults;
  20. IASKSettingsReader *_settingsReader;
  21. }
  22. @end
  23. @implementation VLCSettingsTableViewController
  24. - (void)loadView
  25. {
  26. UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
  27. tableView.delegate = self;
  28. tableView.dataSource = self;
  29. [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:SettingsHeaderReUseIdentifier];
  30. self.view = tableView;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.clearsSelectionOnViewWillAppear = YES;
  35. self.automaticallyAdjustsScrollViewInsets = NO;
  36. self.edgesForExtendedLayout = UIRectEdgeAll ^ UIRectEdgeTop;
  37. _userDefaults = [NSUserDefaults standardUserDefaults];
  38. _settingsReader = [[IASKSettingsReader alloc] init];
  39. }
  40. - (NSString *)title
  41. {
  42. return NSLocalizedString(@"Settings", nil);
  43. }
  44. #pragma mark - Table view data source
  45. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  46. return _settingsReader.numberOfSections;
  47. }
  48. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  49. {
  50. return [_settingsReader numberOfRowsForSection:section];
  51. }
  52. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  53. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingsReUseIdentifier];
  54. if (!cell)
  55. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SettingsReUseIdentifier];
  56. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  57. cell.textLabel.text = [specifier title];
  58. NSString *specifierType = specifier.type;
  59. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  60. NSArray *titles = [specifier multipleTitles];
  61. NSArray *values = [specifier multipleValues];
  62. NSUInteger selectedIndex = [values indexOfObject:[_userDefaults objectForKey:[specifier key]]];
  63. NSUInteger titlesCount = titles.count;
  64. if (selectedIndex < titlesCount)
  65. cell.detailTextLabel.text = [_settingsReader titleForStringId:titles[selectedIndex]];
  66. else {
  67. selectedIndex = [values indexOfObject:[specifier defaultValue]];
  68. if (selectedIndex < titlesCount)
  69. cell.detailTextLabel.text = [_settingsReader titleForStringId:titles[selectedIndex]];
  70. }
  71. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  72. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  73. cell.detailTextLabel.text = [_userDefaults boolForKey:[specifier key]] ? NSLocalizedString(@"ON", nil) : NSLocalizedString(@"OFF", nil);
  74. cell.accessoryType = UITableViewCellAccessoryNone;
  75. } else {
  76. cell.detailTextLabel.text = @"";
  77. cell.accessoryType = UITableViewCellAccessoryNone;
  78. }
  79. return cell;
  80. }
  81. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  82. {
  83. return [_settingsReader titleForSection:section];
  84. }
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  88. NSString *specifierType = specifier.type;
  89. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  90. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:specifier.title
  91. message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  92. NSUInteger count = [specifier multipleValuesCount];
  93. NSArray *titles = [specifier multipleTitles];
  94. NSValue *currentValue = [_userDefaults objectForKey:[specifier key]] ?: [specifier defaultValue];
  95. NSUInteger indexOfPreferredAction = [[specifier multipleValues] indexOfObject:currentValue];
  96. for (NSUInteger i = 0; i < count; i++) {
  97. id value = [[specifier multipleValues][i] copy];
  98. UIAlertAction *action = [UIAlertAction actionWithTitle:[_settingsReader titleForStringId:titles[i]]
  99. style:UIAlertActionStyleDefault
  100. handler:^(UIAlertAction * _Nonnull action) {
  101. [_userDefaults setObject:value forKey:[specifier key]];
  102. [_userDefaults synchronize];
  103. [self.tableView reloadData];
  104. }];
  105. [alertController addAction:action];
  106. if (i == indexOfPreferredAction)
  107. [alertController setPreferredAction:action];
  108. }
  109. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  110. style:UIAlertActionStyleCancel
  111. handler:nil]];
  112. [self presentViewController:alertController animated:YES completion:nil];
  113. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  114. NSString *specifierKey = [specifier key];
  115. [_userDefaults setBool:![_userDefaults boolForKey:specifierKey] forKey:specifierKey];
  116. [_userDefaults synchronize];
  117. [self.tableView reloadData];
  118. } else {
  119. VLCAboutViewController *targetViewController = [[VLCAboutViewController alloc] initWithNibName:nil bundle:nil];
  120. targetViewController.title = specifier.title;
  121. [self presentViewController:targetViewController
  122. animated:YES
  123. completion:nil];
  124. }
  125. }
  126. @end