VLCSettingsController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_iOS-Swift.h"
  19. @interface VLCSettingsController ()<PAPasscodeViewControllerDelegate>
  20. @end
  21. @implementation VLCSettingsController
  22. - (instancetype)initWithStyle:(UITableViewStyle)style
  23. {
  24. self = [super initWithStyle:style];
  25. if (self) {
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingDidChange:) name:kIASKAppSettingChanged object:nil];
  27. }
  28. return self;
  29. }
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. self.modalPresentationStyle = UIModalPresentationFormSheet;
  34. self.delegate = self;
  35. self.showDoneButton = NO;
  36. self.showCreditsFooter = NO;
  37. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ABOUT", nil) style:UIBarButtonItemStylePlain target:self action:@selector(showAbout)];
  38. self.navigationItem.leftBarButtonItem.accessibilityIdentifier = VLCAccessibilityIdentifier.about;
  39. self.neverShowPrivacySettings = YES;
  40. self.tableView.estimatedRowHeight = 100;
  41. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  42. self.tableView.rowHeight = UITableViewAutomaticDimension;
  43. [self themeDidChange];
  44. }
  45. - (void)themeDidChange
  46. {
  47. self.view.backgroundColor = PresentationTheme.current.colors.background;
  48. [self.tableView reloadData];
  49. [self setNeedsStatusBarAppearanceUpdate];
  50. }
  51. - (void)viewWillAppear:(BOOL)animated
  52. {
  53. [super viewWillAppear:animated];
  54. [self filterCellsWithAnimation:NO];
  55. }
  56. - (UIStatusBarStyle)preferredStatusBarStyle
  57. {
  58. return PresentationTheme.current.colors.statusBarStyle;
  59. }
  60. - (NSSet *)hiddenBiometryKeys
  61. {
  62. if (@available(iOS 11.0.1, *)) {
  63. LAContext *laContext = [[LAContext alloc] init];
  64. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  65. switch (laContext.biometryType) {
  66. case LABiometryTypeFaceID:
  67. return [NSSet setWithObject:kVLCSettingPasscodeAllowTouchID];
  68. case LABiometryTypeTouchID:
  69. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  70. case LABiometryNone:
  71. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  72. }
  73. }
  74. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  75. }
  76. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  77. }
  78. - (void)filterCellsWithAnimation:(BOOL)shouldAnimate
  79. {
  80. NSMutableSet *hideKeys = [[NSMutableSet alloc] init];
  81. if (![VLCKeychainCoordinator passcodeLockEnabled]) {
  82. [hideKeys addObject:kVLCSettingPasscodeAllowTouchID];
  83. [hideKeys addObject:kVLCSettingPasscodeAllowFaceID];
  84. [self setHiddenKeys:hideKeys animated:shouldAnimate];
  85. return;
  86. }
  87. [self setHiddenKeys:[self hiddenBiometryKeys] animated:shouldAnimate];
  88. }
  89. - (void)settingDidChange:(NSNotification*)notification
  90. {
  91. if ([notification.object isEqual:kVLCSettingPasscodeOnKey]) {
  92. BOOL passcodeOn = [[notification.userInfo objectForKey:kVLCSettingPasscodeOnKey] boolValue];
  93. if (passcodeOn) {
  94. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  95. passcodeLockController.delegate = self;
  96. [self presentViewController:passcodeLockController animated:YES completion:nil];
  97. } else {
  98. [self updateForPasscode:nil];
  99. }
  100. }
  101. if ([notification.object isEqual:kVLCSettingAppTheme]) {
  102. BOOL darkTheme = [[notification.userInfo objectForKey:kVLCSettingAppTheme] boolValue];
  103. PresentationTheme.current = darkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme;
  104. [self themeDidChange];
  105. }
  106. }
  107. - (void)updateUIAndCoreSpotlightForPasscodeSetting:(BOOL)passcodeOn
  108. {
  109. [self filterCellsWithAnimation:YES];
  110. [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:!passcodeOn];
  111. if (passcodeOn) {
  112. // delete whole index for VLC
  113. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  114. }
  115. }
  116. - (void)showAbout
  117. {
  118. VLCAboutViewController *aboutVC = [[VLCAboutViewController alloc] init];
  119. UINavigationController *modalNavigationController = [[UINavigationController alloc] initWithRootViewController:aboutVC];
  120. [self presentViewController:modalNavigationController animated:YES completion:nil];
  121. }
  122. #pragma mark - PAPasscode delegate
  123. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  124. {
  125. [self updateForPasscode:nil];
  126. }
  127. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  128. {
  129. [self updateForPasscode:controller.passcode];
  130. }
  131. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  134. VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
  135. if (!cell) {
  136. cell = [VLCSettingsTableViewCell cellWithIdentifier:specifier.type target:self];
  137. }
  138. [cell configureWithSpecifier:specifier value:[self.settingsStore objectForKey:specifier.key]];
  139. return cell;
  140. }
  141. - (void)updateForPasscode:(NSString *)passcode
  142. {
  143. NSError *error = nil;
  144. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  145. if (error == nil) {
  146. if (passcode == nil) {
  147. //Set manually the value to NO to disable the UISwitch.
  148. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kVLCSettingPasscodeOnKey];
  149. }
  150. [self updateUIAndCoreSpotlightForPasscodeSetting:passcode != nil];
  151. }
  152. if ([self.navigationController.presentedViewController isKindOfClass:[PAPasscodeViewController class]]) {
  153. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  154. }
  155. }
  156. #pragma mark - InAppSettings customization
  157. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. return indexPath;
  160. }
  161. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  162. {
  163. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  164. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  165. if ([specifier.type isEqual: kIASKPSToggleSwitchSpecifier]) {
  166. VLCSettingsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  167. IASKSwitch *toggle = (IASKSwitch *)cell.accessoryView;
  168. [toggle setOn:!toggle.isOn animated:YES];
  169. [self toggledValue:toggle];
  170. } else {
  171. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  172. }
  173. }
  174. @end