VLCSettingsController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. MediaLibraryService *_medialibraryService;
  25. }
  26. @end
  27. @implementation VLCSettingsController
  28. - (instancetype)initWithMediaLibraryService:(MediaLibraryService *)medialibraryService
  29. {
  30. self = [super initWithStyle:UITableViewStyleGrouped];
  31. if (self) {
  32. [self setupUI];
  33. _medialibraryService = medialibraryService;
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingDidChange:) name:kIASKAppSettingChanged object:nil];
  35. }
  36. return self;
  37. }
  38. - (void)setupUI
  39. {
  40. self.title = NSLocalizedString(@"Settings", nil);
  41. self.tabBarItem = [[UITabBarItem alloc] initWithTitle: NSLocalizedString(@"Settings", nil)
  42. image: [UIImage imageNamed:@"Settings"]
  43. selectedImage: [UIImage imageNamed:@"Settings"]];
  44. self.tabBarItem.accessibilityIdentifier = VLCAccessibilityIdentifier.settings;
  45. }
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. self.modalPresentationStyle = UIModalPresentationFormSheet;
  50. self.delegate = self;
  51. self.showDoneButton = NO;
  52. self.showCreditsFooter = NO;
  53. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ABOUT", nil) style:UIBarButtonItemStylePlain target:self action:@selector(showAbout)];
  54. self.navigationItem.leftBarButtonItem.accessibilityIdentifier = VLCAccessibilityIdentifier.about;
  55. self.neverShowPrivacySettings = YES;
  56. self.tableView.rowHeight = UITableViewAutomaticDimension;
  57. self.tableView.estimatedRowHeight = 100;
  58. self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
  59. self.tableView.estimatedSectionHeaderHeight = 64;
  60. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  61. [self.tableView registerClass:[VLCSectionTableHeaderView class] forHeaderFooterViewReuseIdentifier:kVLCSectionTableHeaderViewIdentifier];
  62. [self themeDidChange];
  63. actionSheet = [[VLCActionSheet alloc] init];
  64. actionSheet.modalPresentationStyle = UIModalPresentationCustom;
  65. specifierManager = [[VLCSettingsSpecifierManager alloc] initWithSettingsReader:self.settingsReader settingsStore:self.settingsStore];
  66. [[NSNotificationCenter defaultCenter] addObserver:self
  67. selector:@selector(themeDidChange)
  68. name:kVLCThemeDidChangeNotification
  69. object:nil];
  70. }
  71. - (void)themeDidChange
  72. {
  73. self.view.backgroundColor = PresentationTheme.current.colors.background;
  74. [self setNeedsStatusBarAppearanceUpdate];
  75. }
  76. - (void)viewWillAppear:(BOOL)animated
  77. {
  78. [super viewWillAppear:animated];
  79. [self filterCellsWithAnimation:NO];
  80. }
  81. - (UIStatusBarStyle)preferredStatusBarStyle
  82. {
  83. return PresentationTheme.current.colors.statusBarStyle;
  84. }
  85. - (NSSet *)hiddenBiometryKeys
  86. {
  87. if (@available(iOS 11.0.1, *)) {
  88. LAContext *laContext = [[LAContext alloc] init];
  89. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  90. switch (laContext.biometryType) {
  91. case LABiometryTypeFaceID:
  92. return [NSSet setWithObject:kVLCSettingPasscodeAllowTouchID];
  93. case LABiometryTypeTouchID:
  94. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  95. case LABiometryNone:
  96. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  97. }
  98. }
  99. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  100. }
  101. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  102. }
  103. - (void)filterCellsWithAnimation:(BOOL)shouldAnimate
  104. {
  105. NSMutableSet *hideKeys = [[NSMutableSet alloc] init];
  106. if (![VLCKeychainCoordinator passcodeLockEnabled]) {
  107. [hideKeys addObject:kVLCSettingPasscodeAllowTouchID];
  108. [hideKeys addObject:kVLCSettingPasscodeAllowFaceID];
  109. [self setHiddenKeys:hideKeys animated:shouldAnimate];
  110. return;
  111. }
  112. [self setHiddenKeys:[self hiddenBiometryKeys] animated:shouldAnimate];
  113. }
  114. - (void)settingDidChange:(NSNotification*)notification
  115. {
  116. if ([notification.userInfo objectForKey: kVLCSettingPasscodeOnKey]) {
  117. BOOL passcodeOn = [[notification.userInfo objectForKey:kVLCSettingPasscodeOnKey] boolValue];
  118. [self updateForPasscode:nil];
  119. if (passcodeOn) {
  120. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  121. passcodeLockController.delegate = self;
  122. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:passcodeLockController];
  123. // Specify modal presentation style due to iOS 13 behaviour
  124. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  125. [self.navigationController presentViewController:navigationController animated:YES completion:nil];
  126. }
  127. }
  128. }
  129. - (void)showAbout
  130. {
  131. VLCAboutViewController *aboutVC = [[VLCAboutViewController alloc] init];
  132. UINavigationController *modalNavigationController = [[UINavigationController alloc] initWithRootViewController:aboutVC];
  133. [self presentViewController:modalNavigationController animated:YES completion:nil];
  134. }
  135. #pragma mark - PAPasscode delegate
  136. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  137. {
  138. [self updateForPasscode:nil];
  139. [self.settingsStore setBool:false forKey:kVLCSettingPasscodeOnKey];
  140. }
  141. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  142. {
  143. [self updateForPasscode:controller.passcode];
  144. }
  145. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  148. VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
  149. if (!cell) {
  150. cell = [[VLCSettingsTableViewCell alloc] initWithReuseIdentifier:specifier.type target:self];
  151. }
  152. [cell configureWithSpecifier:specifier settingsValue:[self.settingsStore objectForKey:specifier.key]];
  153. return cell;
  154. }
  155. - (void)updateForPasscode:(NSString *)passcode
  156. {
  157. NSError *error = nil;
  158. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  159. if (error == nil) {
  160. if (passcode != nil) {
  161. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  162. } else {
  163. [_medialibraryService reindexAllMediaForSpotlight];
  164. }
  165. //Set manually the value to enable/disable the UISwitch.
  166. [self filterCellsWithAnimation:YES];
  167. }
  168. if ([self.navigationController.presentedViewController isKindOfClass:[UINavigationController class]] && [((UINavigationController *)self.navigationController.presentedViewController).viewControllers.firstObject isKindOfClass:[PAPasscodeViewController class]]) {
  169. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  170. }
  171. }
  172. #pragma mark - InAppSettings customization
  173. - (UIView *)settingsViewController:(id<IASKViewController>)settingsViewController tableView:(UITableView *)tableView viewForHeaderForSection:(NSInteger)section
  174. {
  175. if (section == 0) {
  176. return nil;
  177. }
  178. VLCSectionTableHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kVLCSectionTableHeaderViewIdentifier];
  179. header.label.text = [self.settingsReader titleForSection:section];
  180. return header;
  181. }
  182. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  183. {
  184. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  185. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  186. if ([specifier.type isEqualToString: kIASKPSMultiValueSpecifier]) {
  187. [self displayActionSheetFor:specifier];
  188. } else if ([specifier.type isEqualToString: kIASKButtonSpecifier]) {
  189. [self buttonTappedFor:specifier];
  190. } else {
  191. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  192. }
  193. }
  194. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  195. {
  196. return UITableViewAutomaticDimension;
  197. }
  198. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  199. {
  200. return nil;
  201. }
  202. - (void)displayActionSheetFor:(IASKSpecifier *)specifier
  203. {
  204. specifierManager.specifier = specifier;
  205. actionSheet.delegate = specifierManager;
  206. actionSheet.dataSource = specifierManager;
  207. [self presentViewController:actionSheet animated:NO completion:^{
  208. [self->actionSheet.collectionView selectItemAtIndexPath:self->specifierManager.selectedIndex animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  209. }];
  210. }
  211. - (void)buttonTappedFor:(IASKSpecifier *)specifier
  212. {
  213. __weak typeof(self) weakSelf = self;
  214. if ([specifier.specifierDict[@"Key"] isEqual: @"forceMediaLibraryRescan"]) {
  215. UIAlertController *alert = [UIAlertController
  216. alertControllerWithTitle:NSLocalizedString(@"FORCE_RESCAN_TITLE", "")
  217. message:NSLocalizedString(@"FORCE_RESCAN_MESSAGE", "")
  218. preferredStyle:UIAlertControllerStyleAlert];
  219. UIAlertAction* rescanAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_RESCAN", "")
  220. style:UIAlertActionStyleDestructive
  221. handler:^(UIAlertAction * action) {
  222. __strong typeof(weakSelf) strongSelf = weakSelf;
  223. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  224. [strongSelf->_medialibraryService forceRescan];
  225. });
  226. }];
  227. UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", "")
  228. style:UIAlertActionStyleCancel
  229. handler:nil];
  230. [alert addAction:cancelAction];
  231. [alert addAction:rescanAction];
  232. [self presentViewController:alert animated:YES completion:nil];
  233. }
  234. }
  235. @end