VLCSettingsController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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:[VLCSettingsSheetCell class] forCellWithReuseIdentifier:VLCSettingsSheetCell.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. if (passcodeOn) {
  114. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  115. passcodeLockController.delegate = self;
  116. [self presentViewController:passcodeLockController animated:YES completion:nil];
  117. } else {
  118. [self updateForPasscode: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)updateUIAndCoreSpotlightForPasscodeSetting:(BOOL)passcodeOn
  128. {
  129. [self filterCellsWithAnimation:YES];
  130. [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:!passcodeOn];
  131. if (passcodeOn) {
  132. // delete whole index for VLC
  133. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  134. }
  135. }
  136. - (void)showAbout
  137. {
  138. VLCAboutViewController *aboutVC = [[VLCAboutViewController alloc] init];
  139. UINavigationController *modalNavigationController = [[UINavigationController alloc] initWithRootViewController:aboutVC];
  140. [self presentViewController:modalNavigationController animated:YES completion:nil];
  141. }
  142. #pragma mark - PAPasscode delegate
  143. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  144. {
  145. [self updateForPasscode:nil];
  146. }
  147. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  148. {
  149. [self updateForPasscode:controller.passcode];
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  154. VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
  155. if (!cell) {
  156. cell = [[VLCSettingsTableViewCell alloc] initWithReuseIdentifier:specifier.type target:self];
  157. }
  158. [cell configureWithSpecifier:specifier settingsValue:[self.settingsStore objectForKey:specifier.key]];
  159. return cell;
  160. }
  161. - (void)updateForPasscode:(NSString *)passcode
  162. {
  163. NSError *error = nil;
  164. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  165. if (error == nil) {
  166. if (passcode == nil) {
  167. //Set manually the value to NO to disable the UISwitch.
  168. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kVLCSettingPasscodeOnKey];
  169. }
  170. [self updateUIAndCoreSpotlightForPasscodeSetting:passcode != nil];
  171. }
  172. if ([self.navigationController.presentedViewController isKindOfClass:[PAPasscodeViewController class]]) {
  173. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  174. }
  175. }
  176. #pragma mark - InAppSettings customization
  177. - (UIView *)settingsViewController:(id<IASKViewController>)settingsViewController tableView:(UITableView *)tableView viewForHeaderForSection:(NSInteger)section
  178. {
  179. if (section == 0) {
  180. return nil;
  181. }
  182. VLCSectionTableHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kVLCSectionTableHeaderViewIdentifier];
  183. header.label.text = [self.settingsReader titleForSection:section];
  184. return header;
  185. }
  186. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  187. {
  188. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  189. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  190. if ([specifier.type isEqualToString: kIASKPSMultiValueSpecifier]) {
  191. [self displayActionSheetFor:specifier];
  192. } else {
  193. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  194. }
  195. }
  196. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  197. {
  198. return nil;
  199. }
  200. - (void)displayActionSheetFor:(IASKSpecifier *)specifier
  201. {
  202. specifierManager.specifier = specifier;
  203. actionSheet.delegate = specifierManager;
  204. actionSheet.dataSource = specifierManager;
  205. [self presentViewController:actionSheet animated:YES completion:^{
  206. [self->actionSheet.collectionView selectItemAtIndexPath:self->specifierManager.selectedIndex animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  207. }];
  208. }
  209. @end