VLCSettingsController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*****************************************************************************
  2. * VLCSettingsController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <nitz.carola # googlemail.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCSettingsController.h"
  15. #import "IASKSettingsReader.h"
  16. #import "PAPasscodeViewController.h"
  17. #import <LocalAuthentication/LocalAuthentication.h>
  18. #import "VLC-Swift.h"
  19. NSString * const kVLCSectionTableHeaderViewIdentifier = @"VLCSectionTableHeaderViewIdentifier";
  20. @interface VLCSettingsController ()<PAPasscodeViewControllerDelegate>
  21. {
  22. VLCActionSheet *actionSheet;
  23. VLCSettingsSpecifierManager *specifierManager;
  24. }
  25. @end
  26. @implementation VLCSettingsController
  27. - (instancetype)initWithStyle:(UITableViewStyle)style
  28. {
  29. self = [super initWithStyle:style];
  30. if (self) {
  31. [self setupUI];
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingDidChange:) name:kIASKAppSettingChanged object:nil];
  33. }
  34. return self;
  35. }
  36. - (void)setupUI
  37. {
  38. self.title = NSLocalizedString(@"Settings", nil);
  39. self.tabBarItem = [[UITabBarItem alloc] initWithTitle: NSLocalizedString(@"Settings", nil)
  40. image: [UIImage imageNamed:@"Settings"]
  41. selectedImage: [UIImage imageNamed:@"Settings"]];
  42. self.tabBarItem.accessibilityIdentifier = VLCAccessibilityIdentifier.settings;
  43. }
  44. - (void)viewDidLoad
  45. {
  46. [super viewDidLoad];
  47. self.modalPresentationStyle = UIModalPresentationFormSheet;
  48. self.delegate = self;
  49. self.showDoneButton = NO;
  50. self.showCreditsFooter = NO;
  51. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ABOUT", nil) style:UIBarButtonItemStylePlain target:self action:@selector(showAbout)];
  52. self.navigationItem.leftBarButtonItem.accessibilityIdentifier = VLCAccessibilityIdentifier.about;
  53. self.neverShowPrivacySettings = YES;
  54. self.tableView.rowHeight = UITableViewAutomaticDimension;
  55. self.tableView.estimatedRowHeight = 100;
  56. self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
  57. self.tableView.estimatedSectionHeaderHeight = 64;
  58. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  59. [self.tableView registerClass:[VLCSectionTableHeaderView class] forHeaderFooterViewReuseIdentifier:kVLCSectionTableHeaderViewIdentifier];
  60. [self themeDidChange];
  61. actionSheet = [[VLCActionSheet alloc] init];
  62. actionSheet.modalPresentationStyle = UIModalPresentationCustom;
  63. [actionSheet.collectionView registerClass:[VLCActionSheetCell class] forCellWithReuseIdentifier:VLCActionSheetCell.identifier];
  64. specifierManager = [[VLCSettingsSpecifierManager alloc] initWithSettingsReader:self.settingsReader settingsStore:self.settingsStore];
  65. }
  66. - (void)themeDidChange
  67. {
  68. self.view.backgroundColor = PresentationTheme.current.colors.background;
  69. [self setNeedsStatusBarAppearanceUpdate];
  70. }
  71. - (void)viewWillAppear:(BOOL)animated
  72. {
  73. [super viewWillAppear:animated];
  74. [self filterCellsWithAnimation:NO];
  75. }
  76. - (UIStatusBarStyle)preferredStatusBarStyle
  77. {
  78. return PresentationTheme.current.colors.statusBarStyle;
  79. }
  80. - (NSSet *)hiddenBiometryKeys
  81. {
  82. if (@available(iOS 11.0.1, *)) {
  83. LAContext *laContext = [[LAContext alloc] init];
  84. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  85. switch (laContext.biometryType) {
  86. case LABiometryTypeFaceID:
  87. return [NSSet setWithObject:kVLCSettingPasscodeAllowTouchID];
  88. case LABiometryTypeTouchID:
  89. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  90. case LABiometryNone:
  91. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  92. }
  93. }
  94. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  95. }
  96. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  97. }
  98. - (void)filterCellsWithAnimation:(BOOL)shouldAnimate
  99. {
  100. NSMutableSet *hideKeys = [[NSMutableSet alloc] init];
  101. if (![VLCKeychainCoordinator passcodeLockEnabled]) {
  102. [hideKeys addObject:kVLCSettingPasscodeAllowTouchID];
  103. [hideKeys addObject:kVLCSettingPasscodeAllowFaceID];
  104. [self setHiddenKeys:hideKeys animated:shouldAnimate];
  105. return;
  106. }
  107. [self setHiddenKeys:[self hiddenBiometryKeys] animated:shouldAnimate];
  108. }
  109. - (void)settingDidChange:(NSNotification*)notification
  110. {
  111. if ([notification.object isEqual:kVLCSettingPasscodeOnKey]) {
  112. BOOL passcodeOn = [[notification.userInfo objectForKey:kVLCSettingPasscodeOnKey] boolValue];
  113. [self updateForPasscode:nil];
  114. if (passcodeOn) {
  115. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  116. passcodeLockController.delegate = self;
  117. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:passcodeLockController];
  118. [self.navigationController presentViewController:navigationController animated:YES completion:nil];
  119. }
  120. }
  121. if ([notification.object isEqual:kVLCSettingAppTheme]) {
  122. BOOL darkTheme = [[notification.userInfo objectForKey:kVLCSettingAppTheme] boolValue];
  123. PresentationTheme.current = darkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme;
  124. [self themeDidChange];
  125. }
  126. }
  127. - (void)showAbout
  128. {
  129. VLCAboutViewController *aboutVC = [[VLCAboutViewController alloc] init];
  130. UINavigationController *modalNavigationController = [[UINavigationController alloc] initWithRootViewController:aboutVC];
  131. [self presentViewController:modalNavigationController animated:YES completion:nil];
  132. }
  133. #pragma mark - PAPasscode delegate
  134. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  135. {
  136. [self updateForPasscode:nil];
  137. [self.settingsStore setBool:false forKey:kVLCSettingPasscodeOnKey];
  138. }
  139. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  140. {
  141. [self updateForPasscode:controller.passcode];
  142. }
  143. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  146. VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
  147. if (!cell) {
  148. cell = [[VLCSettingsTableViewCell alloc] initWithReuseIdentifier:specifier.type target:self];
  149. }
  150. [cell configureWithSpecifier:specifier settingsValue:[self.settingsStore objectForKey:specifier.key]];
  151. return cell;
  152. }
  153. - (void)updateForPasscode:(NSString *)passcode
  154. {
  155. NSError *error = nil;
  156. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  157. if (error == nil) {
  158. if (passcode != nil) {
  159. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  160. }
  161. //Set manually the value to enable/disable the UISwitch.
  162. [self filterCellsWithAnimation:YES];
  163. }
  164. if ([self.navigationController.presentedViewController isKindOfClass:[UINavigationController class]] && [((UINavigationController *)self.navigationController.presentedViewController).viewControllers.firstObject isKindOfClass:[PAPasscodeViewController class]]) {
  165. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  166. }
  167. }
  168. #pragma mark - InAppSettings customization
  169. - (UIView *)settingsViewController:(id<IASKViewController>)settingsViewController tableView:(UITableView *)tableView viewForHeaderForSection:(NSInteger)section
  170. {
  171. if (section == 0) {
  172. return nil;
  173. }
  174. VLCSectionTableHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kVLCSectionTableHeaderViewIdentifier];
  175. header.label.text = [self.settingsReader titleForSection:section];
  176. return header;
  177. }
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  181. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  182. if ([specifier.type isEqualToString: kIASKPSMultiValueSpecifier]) {
  183. [self displayActionSheetFor:specifier];
  184. } else {
  185. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  186. }
  187. }
  188. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  189. {
  190. return nil;
  191. }
  192. - (void)displayActionSheetFor:(IASKSpecifier *)specifier
  193. {
  194. specifierManager.specifier = specifier;
  195. actionSheet.delegate = specifierManager;
  196. actionSheet.dataSource = specifierManager;
  197. [self presentViewController:actionSheet animated:YES completion:^{
  198. [self->actionSheet.collectionView selectItemAtIndexPath:self->specifierManager.selectedIndex animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  199. }];
  200. }
  201. @end