VLCSettingsViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. @property (strong, nonatomic) NSUserDefaults *userDefaults;
  18. @property (strong, nonatomic) IASKSettingsReader *settingsReader;
  19. @end
  20. @implementation VLCSettingsViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.userDefaults = [NSUserDefaults standardUserDefaults];
  24. self.settingsReader = [[IASKSettingsReader alloc] init];
  25. self.automaticallyAdjustsScrollViewInsets = NO;
  26. self.edgesForExtendedLayout = UIRectEdgeAll ^ UIRectEdgeTop;
  27. self.tableView.opaque = NO;
  28. self.tableView.backgroundColor = [UIColor clearColor];
  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 self.settingsReader.numberOfSections;
  37. }
  38. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  39. {
  40. return [self.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. }
  47. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  48. cell.textLabel.text = [specifier title];
  49. NSString *specifierType = specifier.type;
  50. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  51. NSArray *titles = [specifier multipleTitles];
  52. NSArray *values = [specifier multipleValues];
  53. NSUInteger selectedIndex = [values indexOfObject:[self.userDefaults objectForKey:[specifier key]]];
  54. NSUInteger titlesCount = titles.count;
  55. if (selectedIndex < titlesCount)
  56. cell.detailTextLabel.text = [self.settingsReader titleForStringId:titles[selectedIndex]];
  57. else {
  58. selectedIndex = [values indexOfObject:[specifier defaultValue]];
  59. if (selectedIndex < titlesCount)
  60. cell.detailTextLabel.text = [self.settingsReader titleForStringId:titles[selectedIndex]];
  61. }
  62. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  63. } else if ([specifierType isEqualToString:kIASKPSToggleSwitchSpecifier]) {
  64. cell.detailTextLabel.text = [self.userDefaults boolForKey:[specifier key]] ? NSLocalizedString(@"ON", nil) : NSLocalizedString(@"OFF", nil);
  65. cell.accessoryType = UITableViewCellAccessoryNone;
  66. } else {
  67. cell.detailTextLabel.text = @"";
  68. cell.accessoryType = UITableViewCellAccessoryNone;
  69. }
  70. return cell;
  71. }
  72. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  73. {
  74. return [self.settingsReader titleForSection:section];
  75. }
  76. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  79. NSString *specifierType = specifier.type;
  80. if ([specifierType isEqualToString:kIASKPSMultiValueSpecifier]) {
  81. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:specifier.title
  82. message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  83. NSUInteger count = [specifier multipleValuesCount];
  84. NSArray *titles = [specifier multipleTitles];
  85. NSValue *currentValue = [self.userDefaults objectForKey:[specifier key]] ?: [specifier defaultValue];
  86. NSUInteger indexOfPreferredAction = [[specifier multipleValues] indexOfObject:currentValue];
  87. for (NSUInteger i = 0; i < count; i++) {
  88. id value = [[specifier multipleValues][i] copy];
  89. UIAlertAction *action = [UIAlertAction actionWithTitle:[self.settingsReader titleForStringId:titles[i]]
  90. style:UIAlertActionStyleDefault
  91. handler:^(UIAlertAction * _Nonnull action) {
  92. [self.userDefaults setObject:value forKey:[specifier key]];
  93. [self.tableView reloadData];
  94. }];
  95. [alertController addAction:action];
  96. if (i == indexOfPreferredAction) {
  97. [alertController setPreferredAction:action];
  98. }
  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. [self.userDefaults setBool:![self.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