VLCSettingsViewController.m 6.0 KB

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