VLCSettingsViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  30. - (NSString *)title
  31. {
  32. return NSLocalizedString(@"Settings", nil);
  33. }
  34. #pragma mark - Table view data source
  35. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  36. return _settingsReader.numberOfSections;
  37. }
  38. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  39. {
  40. return [_settingsReader numberOfRowsForSection:section];
  41. }
  42. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  43. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingsReUseIdentifier];
  44. if (!cell)
  45. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SettingsReUseIdentifier];
  46. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  47. cell.textLabel.text = [specifier title];
  48. NSString *specifierType = specifier.type;
  49. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  50. NSArray *titles = [specifier multipleTitles];
  51. NSArray *values = [specifier multipleValues];
  52. NSUInteger selectedIndex = [values indexOfObject:[_userDefaults objectForKey:[specifier key]]];
  53. NSUInteger titlesCount = titles.count;
  54. if (selectedIndex < titlesCount)
  55. cell.detailTextLabel.text = [_settingsReader titleForStringId:titles[selectedIndex]];
  56. else {
  57. selectedIndex = [values indexOfObject:[specifier defaultValue]];
  58. if (selectedIndex < titlesCount)
  59. cell.detailTextLabel.text = [_settingsReader titleForStringId:titles[selectedIndex]];
  60. }
  61. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  62. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  63. cell.detailTextLabel.text = [_userDefaults boolForKey:[specifier key]] ? NSLocalizedString(@"ON", nil) : NSLocalizedString(@"OFF", nil);
  64. cell.accessoryType = UITableViewCellAccessoryNone;
  65. } else {
  66. cell.detailTextLabel.text = @"";
  67. cell.accessoryType = UITableViewCellAccessoryNone;
  68. }
  69. return cell;
  70. }
  71. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  72. {
  73. return [_settingsReader titleForSection:section];
  74. }
  75. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. IASKSpecifier *specifier = [_settingsReader specifierForIndexPath:indexPath];
  78. NSString *specifierType = specifier.type;
  79. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  80. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:specifier.title
  81. message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  82. NSUInteger count = [specifier multipleValuesCount];
  83. NSArray *titles = [specifier multipleTitles];
  84. NSValue *currentValue = [_userDefaults objectForKey:[specifier key]] ?: [specifier defaultValue];
  85. NSUInteger indexOfPreferredAction = [[specifier multipleValues] indexOfObject:currentValue];
  86. for (NSUInteger i = 0; i < count; i++) {
  87. id value = [[specifier multipleValues][i] copy];
  88. UIAlertAction *action = [UIAlertAction actionWithTitle:[_settingsReader titleForStringId:titles[i]]
  89. style:UIAlertActionStyleDefault
  90. handler:^(UIAlertAction * _Nonnull action) {
  91. [_userDefaults setObject:value forKey:[specifier key]];
  92. [_userDefaults synchronize];
  93. [self.tableView reloadData];
  94. }];
  95. [alertController addAction:action];
  96. if (i == indexOfPreferredAction)
  97. [alertController setPreferredAction:action];
  98. }
  99. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  100. style:UIAlertActionStyleCancel
  101. handler:nil]];
  102. [self presentViewController:alertController animated:YES completion:nil];
  103. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  104. NSString *specifierKey = [specifier key];
  105. [_userDefaults setBool:![_userDefaults boolForKey:specifierKey] forKey:specifierKey];
  106. [_userDefaults synchronize];
  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