VLCLibraryViewController.m 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*****************************************************************************
  2. * VLCLibraryViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Tamas Timar <ttimar.vlc # gmail.com>
  11. * Carola Nitz <nitz.carola # gmail.com>
  12. * Tobias Conradi <videolan # tobias-conradi.de>
  13. *
  14. * Refer to the COPYING file of the official project for license.
  15. *****************************************************************************/
  16. #import "VLCLibraryViewController.h"
  17. #import "LXReorderableCollectionViewFlowLayout.h"
  18. #import "VLCActivityViewControllerVendor.h"
  19. #import "VLCAppDelegate.h"
  20. #import "VLCBugreporter.h"
  21. #import "VLCFirstStepsViewController.h"
  22. #import "VLCFolderCollectionViewFlowLayout.h"
  23. #import "VLCMediaDataSource.h"
  24. #import "VLCLibrarySearchDisplayDataSource.h"
  25. #import "VLCMovieViewController.h"
  26. #import "VLCPlaylistCollectionViewCell.h"
  27. #import "VLCPlaylistTableViewCell.h"
  28. #import "VLCPlaybackController+MediaLibrary.h"
  29. #import "VLC_iOS-Swift.h"
  30. #import <CoreSpotlight/CoreSpotlight.h>
  31. /* prefs keys */
  32. static NSString *kDisplayedFirstSteps = @"Did we display the first steps tutorial?";
  33. static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
  34. @implementation EmptyLibraryView
  35. - (IBAction)learnMore:(id)sender
  36. {
  37. UIViewController *firstStepsVC = [[VLCFirstStepsViewController alloc] initWithNibName:nil bundle:nil];
  38. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:firstStepsVC];
  39. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  40. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  41. }
  42. @end
  43. @interface VLCLibraryViewController () <VLCFolderCollectionViewDelegateFlowLayout, LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary, VLCMediaListDelegate, UISearchResultsUpdating, UISearchControllerDelegate> {
  44. VLCLibraryMode _libraryMode;
  45. VLCLibraryMode _previousLibraryMode;
  46. UIBarButtonItem *_menuButton;
  47. NSMutableArray *_indexPaths;
  48. id _folderObject;
  49. VLCFolderCollectionViewFlowLayout *_folderLayout;
  50. LXReorderableCollectionViewFlowLayout *_reorderLayout;
  51. BOOL _inFolder;
  52. BOOL _isSelected;
  53. BOOL _deleteFromTableView;
  54. UILongPressGestureRecognizer *_longPressGestureRecognizer;
  55. VLCLibrarySearchDisplayDataSource *_searchDataSource;
  56. VLCMediaDataSource *_mediaDataSource;
  57. UISearchController *_searchController;
  58. UIBarButtonItem *_selectAllBarButtonItem;
  59. UIBarButtonItem *_createFolderBarButtonItem;
  60. UIBarButtonItem *_shareBarButtonItem;
  61. UIBarButtonItem *_removeFromFolderBarButtonItem;
  62. UIBarButtonItem *_deleteSelectedBarButtonItem;
  63. NSObject *dragAndDropManager;
  64. }
  65. @property (nonatomic, strong) UIBarButtonItem *displayModeBarButtonItem;
  66. @property (nonatomic, strong) UITableView *tableView;
  67. @property (nonatomic, strong) UICollectionView *collectionView;
  68. @property (nonatomic, strong) EmptyLibraryView *emptyLibraryView;
  69. @end
  70. @implementation VLCLibraryViewController
  71. + (void)initialize
  72. {
  73. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  74. [defaults registerDefaults:@{kDisplayedFirstSteps : [NSNumber numberWithBool:NO]}];
  75. [defaults registerDefaults:@{kUsingTableViewToShowData : [NSNumber numberWithBool:UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone]}];
  76. }
  77. - (void)loadView
  78. {
  79. _mediaDataSource = [VLCMediaDataSource new];
  80. _searchDataSource = [VLCLibrarySearchDisplayDataSource new];
  81. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  82. _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
  83. _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  84. if (@available(iOS 11.0, *)) {
  85. dragAndDropManager = [VLCDragAndDropManager new];
  86. ((VLCDragAndDropManager *)dragAndDropManager).delegate = _mediaDataSource;
  87. UIDropInteraction *dropInteraction = [[UIDropInteraction alloc] initWithDelegate:(VLCDragAndDropManager *)dragAndDropManager];
  88. [_emptyLibraryView addInteraction:dropInteraction];
  89. }
  90. [self setupContentView];
  91. [self setViewFromDeviceOrientation];
  92. [self updateViewsForCurrentDisplayMode];
  93. _libraryMode = VLCLibraryModeAllFiles;
  94. }
  95. - (void)setupContentView
  96. {
  97. CGRect viewDimensions = [UIApplication sharedApplication].keyWindow.bounds;
  98. UIView *contentView = [[UIView alloc] initWithFrame:viewDimensions];
  99. contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  100. contentView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  101. if (!_tableView) {
  102. _tableView = [[UITableView alloc] initWithFrame:viewDimensions style:UITableViewStylePlain];
  103. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  104. CGRect frame = _tableView.bounds;
  105. frame.origin.y = -frame.size.height;
  106. UIView *topView = [[UIView alloc] initWithFrame:frame];
  107. topView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  108. [_tableView addSubview:topView];
  109. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  110. _tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  111. _tableView.delegate = self;
  112. _tableView.dataSource = self;
  113. if (@available(iOS 11.0, *)) {
  114. _tableView.dragDelegate = ((VLCDragAndDropManager *)dragAndDropManager);
  115. _tableView.dropDelegate = ((VLCDragAndDropManager *)dragAndDropManager);
  116. }
  117. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  118. _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  119. _tableView.tableHeaderView = _searchController.searchBar;
  120. UINib *nib = [UINib nibWithNibName:@"VLCPlaylistTableViewCell" bundle:nil];
  121. [_tableView registerNib:nib forCellReuseIdentifier:kPlaylistCellIdentifier];
  122. }
  123. _tableView.frame = contentView.bounds;
  124. [_tableView reloadData];
  125. if (!_collectionView) {
  126. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  127. _collectionView = [[UICollectionView alloc] initWithFrame:viewDimensions collectionViewLayout:_folderLayout];
  128. _collectionView.alwaysBounceVertical = YES;
  129. if (@available(iOS 11.0, *)) {
  130. _collectionView.dragDelegate = ((VLCDragAndDropManager *)dragAndDropManager);
  131. _collectionView.dropDelegate = ((VLCDragAndDropManager *)dragAndDropManager);
  132. }
  133. _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  134. _collectionView.delegate = self;
  135. _collectionView.dataSource = self;
  136. _collectionView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  137. _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  138. _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_collectionViewHandleLongPressGesture:)];
  139. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  140. [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  141. }
  142. _collectionView.frame = contentView.bounds;
  143. [_collectionView reloadData];
  144. if (self.usingTableViewToShowData) {
  145. [contentView addSubview:_tableView];
  146. } else {
  147. [contentView addSubview:_collectionView];
  148. [_searchController setActive:NO];
  149. }
  150. self.view = contentView;
  151. }
  152. #pragma mark -
  153. - (void)viewWillLayoutSubviews {
  154. UIScrollView *dataView = self.usingTableViewToShowData ? _tableView : _collectionView;
  155. [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  156. [dataView setContentInset:UIEdgeInsetsZero];
  157. }
  158. - (void)viewDidLoad
  159. {
  160. [super viewDidLoad];
  161. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  162. _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  163. self.navigationItem.leftBarButtonItem = _menuButton;
  164. self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT", nil);
  165. self.editButtonItem.tintColor = [UIColor whiteColor];
  166. _selectAllBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ALL", nil) style:UIBarButtonItemStylePlain target:self action:@selector(handleSelection)];
  167. _selectAllBarButtonItem.tintColor = [UIColor whiteColor];
  168. UIFont *font = [UIFont boldSystemFontOfSize:17];
  169. NSDictionary *attributes = @{NSFontAttributeName: font};
  170. [_selectAllBarButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
  171. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
  172. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", nil);
  173. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  174. [_emptyLibraryView.learnMoreButton setTitle:NSLocalizedString(@"BUTTON_LEARN_MORE", nil) forState:UIControlStateNormal];
  175. _createFolderBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  176. // Better visual alignment with the action button
  177. _createFolderBarButtonItem.imageInsets = UIEdgeInsetsMake(3, 0, -3, 0);
  178. _createFolderBarButtonItem.landscapeImagePhoneInsets = UIEdgeInsetsMake(2, 0, -2, 0);
  179. _removeFromFolderBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(removeFromFolder)];
  180. _removeFromFolderBarButtonItem.imageInsets = UIEdgeInsetsMake(2, 0, -2, 0);
  181. _removeFromFolderBarButtonItem.landscapeImagePhoneInsets = UIEdgeInsetsMake(1, 0, -1, 0);
  182. _removeFromFolderBarButtonItem.enabled = NO;
  183. _shareBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share:)];
  184. _shareBarButtonItem.enabled = NO;
  185. _deleteSelectedBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteSelection:)];
  186. _deleteFromTableView = NO;
  187. UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  188. fixedSpace.width = 20;
  189. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  190. fixedSpace.width *= 2;
  191. }
  192. [self setToolbarItems:@[_shareBarButtonItem,
  193. fixedSpace,
  194. _createFolderBarButtonItem,
  195. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
  196. [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_RENAME", nil) target:self andSelector:@selector(renameSelection)],
  197. [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
  198. _removeFromFolderBarButtonItem,
  199. fixedSpace,
  200. _deleteSelectedBarButtonItem]];
  201. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  202. self.navigationController.toolbar.tintColor = [UIColor whiteColor];
  203. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  204. self.edgesForExtendedLayout = UIRectEdgeNone;
  205. [self setupSearchController];
  206. }
  207. - (void)setupSearchController
  208. {
  209. _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  210. _searchController.searchResultsUpdater = self;
  211. _searchController.dimsBackgroundDuringPresentation = NO;
  212. _searchController.delegate = self;
  213. [self setSearchBar:YES resetContent:YES];
  214. }
  215. - (void)viewWillAppear:(BOOL)animated
  216. {
  217. [super viewWillAppear:animated];
  218. [self.collectionView.collectionViewLayout invalidateLayout];
  219. [self setViewFromDeviceOrientation];
  220. [self updateViewsForCurrentDisplayMode];
  221. }
  222. - (void)viewDidAppear:(BOOL)animated
  223. {
  224. [super viewDidAppear:animated];
  225. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  226. if (![[defaults objectForKey:kDisplayedFirstSteps] boolValue]) {
  227. [self.emptyLibraryView performSelector:@selector(learnMore:) withObject:nil afterDelay:1.];
  228. [defaults setObject:[NSNumber numberWithBool:YES] forKey:kDisplayedFirstSteps];
  229. }
  230. if ([_mediaDataSource numberOfFiles] < 1)
  231. [self updateViewContents];
  232. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  233. }
  234. - (void)viewDidDisappear:(BOOL)animated
  235. {
  236. [super viewDidDisappear:animated];
  237. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  238. }
  239. - (BOOL)canBecomeFirstResponder
  240. {
  241. return YES;
  242. }
  243. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  244. {
  245. if (motion == UIEventSubtypeMotionShake)
  246. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  247. }
  248. - (void)openMediaObject:(NSManagedObject *)mediaObject
  249. {
  250. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShow class]]) {
  251. [_mediaDataSource updateContentsForSelection:mediaObject];
  252. BOOL isAlbum = [mediaObject isKindOfClass:[MLAlbum class]];
  253. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  254. if (_libraryMode == VLCLibraryModeAllFiles)
  255. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", nil);
  256. else
  257. [self.navigationItem.leftBarButtonItem setTitle: isAlbum ? NSLocalizedString(@"LIBRARY_MUSIC", nil) : NSLocalizedString(@"LIBRARY_SERIES", nil)];
  258. self.title = [(MLAlbum*)mediaObject name];
  259. [self reloadViews];
  260. } else if ([mediaObject isKindOfClass:[MLLabel class]]) {
  261. [_mediaDataSource updateContentsForSelection:mediaObject];
  262. _inFolder = YES;
  263. if (!self.usingTableViewToShowData) {
  264. if (@available(iOS 11.0, *)) {
  265. } else {
  266. if (![self.collectionView.collectionViewLayout isEqual:_reorderLayout]) {
  267. for (UIGestureRecognizer *recognizer in _collectionView.gestureRecognizers) {
  268. if (recognizer == _folderLayout.panGestureRecognizer || recognizer == _folderLayout.longPressGestureRecognizer || recognizer == _longPressGestureRecognizer)
  269. [self.collectionView removeGestureRecognizer:recognizer];
  270. }
  271. _reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
  272. [self.collectionView setCollectionViewLayout:_reorderLayout animated:NO];
  273. }
  274. }
  275. }
  276. _libraryMode = VLCLibraryModeFolder;
  277. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  278. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", nil);
  279. self.title = [(MLLabel*)mediaObject name];
  280. _removeFromFolderBarButtonItem.enabled = YES;
  281. _createFolderBarButtonItem.enabled = NO;
  282. [self reloadViews];
  283. return;
  284. } else {
  285. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  286. [vpc playMediaLibraryObject:mediaObject];
  287. [self createSpotlightItem:mediaObject];
  288. }
  289. }
  290. - (void)createSpotlightItem:(nonnull NSManagedObject *)mediaObject
  291. {
  292. if (@available(iOS 9.0, *) && ![VLCKeychainCoordinator passcodeLockEnabled]) {
  293. self.userActivity = [[NSUserActivity alloc] initWithActivityType:kVLCUserActivityPlaying];
  294. MLFile *file = nil;
  295. if ([mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  296. file = [(MLAlbumTrack *)mediaObject anyFileFromTrack];
  297. } else if ([mediaObject isKindOfClass:[MLShowEpisode class]]) {
  298. file = [(MLShowEpisode *)mediaObject anyFileFromEpisode];
  299. } else if ([mediaObject isKindOfClass:[MLFile class]]){
  300. file = (MLFile *)mediaObject;
  301. }
  302. self.userActivity.title = file.title;
  303. self.userActivity.contentAttributeSet = file.coreSpotlightAttributeSet;
  304. self.userActivity.userInfo = @{@"playingmedia":mediaObject.objectID.URIRepresentation};
  305. self.userActivity.eligibleForSearch = YES;
  306. self.userActivity.eligibleForHandoff = YES;
  307. //self.userActivity.contentUserAction = NSUserActivityContentUserActionPlay;
  308. [self.userActivity becomeCurrent];
  309. }
  310. }
  311. - (void)removeMediaObject:(id)managedObject updateDatabase:(BOOL)updateDB
  312. {
  313. [_mediaDataSource removeMediaObject:managedObject];
  314. if (updateDB) {
  315. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  316. [self updateViewContents];
  317. }
  318. }
  319. - (void)_displayEmptyLibraryViewIfNeeded
  320. {
  321. if (self.emptyLibraryView.superview)
  322. [self.emptyLibraryView removeFromSuperview];
  323. if ([_mediaDataSource numberOfFiles] == 0) {
  324. _inFolder = (_libraryMode == VLCLibraryModeFolder || _libraryMode == VLCLibraryModeCreateFolder);
  325. self.emptyLibraryView.emptyLibraryLabel.text = _inFolder ? NSLocalizedString(@"FOLDER_EMPTY", nil) : NSLocalizedString(@"EMPTY_LIBRARY", nil);
  326. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.text = _inFolder ? NSLocalizedString(@"FOLDER_EMPTY_LONG", nil) : NSLocalizedString(@"EMPTY_LIBRARY_LONG", nil);
  327. self.emptyLibraryView.learnMoreButton.hidden = _inFolder;
  328. self.emptyLibraryView.frame = self.view.bounds;
  329. [self.view addSubview:self.emptyLibraryView];
  330. self.navigationItem.rightBarButtonItems = nil;
  331. } else {
  332. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  333. UIBarButtonItem *toggleDisplayedView = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tableViewIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(toggleDisplayedView:)];
  334. self.navigationItem.rightBarButtonItems = @[toggleDisplayedView, self.editButtonItem];
  335. self.displayModeBarButtonItem = toggleDisplayedView;
  336. } else {
  337. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  338. }
  339. }
  340. if (self.usingTableViewToShowData) {
  341. _tableView.separatorStyle = [_mediaDataSource numberOfFiles] > 0 ? UITableViewCellSeparatorStyleSingleLine:
  342. UITableViewCellSeparatorStyleNone;
  343. } else {
  344. [self.collectionView.collectionViewLayout invalidateLayout];
  345. }
  346. [self updateViewsForCurrentDisplayMode];
  347. }
  348. - (void)setViewFromDeviceOrientation
  349. {
  350. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  351. BOOL isPortrait = YES;
  352. if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact)
  353. isPortrait = NO;
  354. [self setUsingTableViewToShowData:isPortrait];
  355. [self _displayEmptyLibraryViewIfNeeded];
  356. }
  357. }
  358. - (void)setSearchBar:(BOOL)enable resetContent:(BOOL)resetContent
  359. {
  360. _tableView.tableHeaderView = enable ? _searchController.searchBar : nil;
  361. if (resetContent) {
  362. CGPoint contentOffset = _tableView.contentOffset;
  363. contentOffset.y += CGRectGetHeight(_tableView.tableHeaderView.frame);
  364. _tableView.contentOffset = contentOffset;
  365. }
  366. }
  367. - (void)libraryUpgradeComplete
  368. {
  369. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  370. self.navigationItem.leftBarButtonItem = _menuButton;
  371. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
  372. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
  373. [self.emptyLibraryView.activityIndicator stopAnimating];
  374. [self.emptyLibraryView removeFromSuperview];
  375. [self updateViewContents];
  376. }
  377. - (void)updateViewContents
  378. {
  379. self.navigationItem.leftBarButtonItem = _menuButton;
  380. [_mediaDataSource updateContentsForSelection:nil];
  381. _removeFromFolderBarButtonItem.enabled = NO;
  382. switch (_libraryMode) {
  383. case VLCLibraryModeAllAlbums: {
  384. self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
  385. _createFolderBarButtonItem.enabled = NO;
  386. [_mediaDataSource addAlbumsInAllAlbumMode:YES];
  387. } break;
  388. case VLCLibraryModeAllSeries: {
  389. self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
  390. _createFolderBarButtonItem.enabled = NO;
  391. [_mediaDataSource addAllShows];
  392. } break;
  393. //Todo: I'm not sure if updateViewContents should be called in VLCLibraryModeFolder
  394. //Here should maybe be an NSAssert to prevent this but for now due to refactoring these calls would've been made in that case
  395. case VLCLibraryModeAllFiles:
  396. case VLCLibraryModeFolder:
  397. case VLCLibraryModeCreateFolder: {
  398. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
  399. _createFolderBarButtonItem.enabled = YES;
  400. [_mediaDataSource addAlbumsInAllAlbumMode:NO];
  401. [_mediaDataSource addAllShows];
  402. [_mediaDataSource addAllFolders];
  403. [_mediaDataSource addRemainingFiles];
  404. }
  405. }
  406. [self reloadViews];
  407. }
  408. - (void)reloadViews
  409. {
  410. // Since this gets can get called at any point and wipe away the selections, we update the actionBarButtonItem here because this can happen if you tap "Save Video" in the UIActivityController and a media access alert view takes away focus (the corresponding 'became active' notification of UIApplication will call this). Or simply try bringing down the notification center to trigger this. Any existing UIActivityViewController session should be safe as it would have copies of the selected file references.
  411. if (self.usingTableViewToShowData) {
  412. [self.tableView reloadData];
  413. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[self.tableView indexPathsForSelectedRows]];
  414. _isSelected = NO;
  415. } else {
  416. [self.collectionView reloadData];
  417. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[self.collectionView indexPathsForSelectedItems]];
  418. if (_libraryMode == VLCLibraryModeAllFiles) {
  419. if (self.collectionView.collectionViewLayout != _folderLayout) {
  420. for (UIGestureRecognizer *recognizer in _collectionView.gestureRecognizers) {
  421. if (recognizer == _reorderLayout.panGestureRecognizer ||
  422. recognizer == _reorderLayout.longPressGestureRecognizer) {
  423. [self.collectionView removeGestureRecognizer:recognizer];
  424. }
  425. }
  426. [self.collectionView setCollectionViewLayout:_folderLayout animated:NO];
  427. [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
  428. }
  429. }
  430. }
  431. [self _displayEmptyLibraryViewIfNeeded];
  432. }
  433. #pragma mark - Table View
  434. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  435. {
  436. return [_mediaDataSource numberOfFiles];
  437. }
  438. // Customize the appearance of table view cells.
  439. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  440. {
  441. static NSString *CellIdentifier = @"PlaylistCell";
  442. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  443. UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightOnTableViewCellGestureAction:)];
  444. [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
  445. [cell addGestureRecognizer:swipeRight];
  446. cell.mediaObject = [_mediaDataSource objectAtIndex:indexPath.row];
  447. return cell;
  448. }
  449. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  450. {
  451. [_mediaDataSource moveObjectFromIndex:fromIndexPath.item toIndex:toIndexPath.item];
  452. }
  453. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  454. {
  455. if (@available(iOS 11.0, *)) {
  456. return true;
  457. }
  458. return _inFolder;
  459. }
  460. - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
  461. {
  462. if (@available(iOS 11.0, *)) {
  463. return true;
  464. }
  465. return _inFolder;
  466. }
  467. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  468. {
  469. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  470. cell.multipleSelectionBackgroundView.backgroundColor = cell.backgroundColor;
  471. }
  472. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  473. {
  474. if (editingStyle == UITableViewCellEditingStyleDelete) {
  475. NSInteger row = indexPath.row;
  476. _deleteFromTableView = YES;
  477. if (row < [_mediaDataSource numberOfFiles])
  478. [self deleteSelection:indexPath];
  479. }
  480. }
  481. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  482. {
  483. if (tableView.isEditing) {
  484. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[tableView indexPathsForSelectedRows]];
  485. }
  486. }
  487. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  488. {
  489. VLCPlaylistTableViewCell *playlistTableViewCell = (VLCPlaylistTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  490. if ([playlistTableViewCell isExpanded]) {
  491. [playlistTableViewCell collapsWithAnimation:YES];
  492. return;
  493. }
  494. for (VLCPlaylistTableViewCell *cell in [tableView visibleCells]) {
  495. if ([cell isExpanded])
  496. [cell collapsWithAnimation:NO];
  497. }
  498. if (tableView.isEditing) {
  499. if (_libraryMode == VLCLibraryModeCreateFolder) {
  500. _folderObject = [_mediaDataSource objectAtIndex:indexPath.row];
  501. _libraryMode = _previousLibraryMode;
  502. [self updateViewContents];
  503. [self createFolderWithName:nil];
  504. } else {
  505. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[tableView indexPathsForSelectedRows]];
  506. }
  507. return;
  508. }
  509. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  510. [self openMediaObject:playlistTableViewCell.mediaObject];
  511. }
  512. #pragma mark - Gesture Action
  513. - (void)swipeRightOnTableViewCellGestureAction:(UIGestureRecognizer *)recognizer
  514. {
  515. [self setEditing:!self.tableView.isEditing animated:YES];
  516. if (!self.tableView.isEditing) {
  517. NSIndexPath *path = [self.tableView indexPathForRowAtPoint:[recognizer locationInView:self.tableView]];
  518. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:path.section]
  519. animated:YES
  520. scrollPosition:UITableViewScrollPositionNone];
  521. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[self.tableView indexPathsForSelectedRows]];
  522. }
  523. }
  524. - (void)swipeRightOnCollectionViewCellGestureAction:(UIGestureRecognizer *)recognizer
  525. {
  526. NSIndexPath *path = [self.collectionView indexPathForItemAtPoint:[recognizer locationInView:self.collectionView]];
  527. VLCPlaylistCollectionViewCell *cell = (VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:path];
  528. [cell showMetadata:!cell.showsMetaData];
  529. }
  530. #pragma mark - Collection View
  531. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  532. {
  533. return [_mediaDataSource numberOfFiles];
  534. }
  535. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  536. {
  537. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  538. cell.mediaObject = [_mediaDataSource objectAtIndex:indexPath.row];
  539. cell.collectionView = _collectionView;
  540. [cell setEditing:self.editing animated:NO];
  541. UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightOnCollectionViewCellGestureAction:)];
  542. [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
  543. [cell addGestureRecognizer:swipeRight];
  544. return cell;
  545. }
  546. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  547. {
  548. const CGFloat maxCellWidth = [UIScreen mainScreen].bounds.size.width / 3;
  549. const CGFloat aspectRatio = 9.0/16.0;
  550. CGRect windowFrame = [UIApplication sharedApplication].keyWindow.frame;
  551. CGFloat windowWidth = windowFrame.size.width;
  552. int numberOfCellsPerRow = ceil(windowWidth/maxCellWidth);
  553. CGFloat cellWidth = windowWidth/numberOfCellsPerRow;
  554. cellWidth -= 5;
  555. return CGSizeMake(cellWidth, cellWidth * aspectRatio);
  556. }
  557. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  558. {
  559. return UIEdgeInsetsMake(5, 5, 5, 5);
  560. }
  561. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  562. {
  563. return 2.5;
  564. }
  565. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  566. {
  567. return 0.;
  568. }
  569. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  570. {
  571. if (self.editing) {
  572. if (_libraryMode == VLCLibraryModeCreateFolder) {
  573. _folderObject = [_mediaDataSource objectAtIndex:indexPath.item];
  574. [self updateViewContents];
  575. [self createFolderWithName:nil];
  576. _libraryMode = _previousLibraryMode;
  577. } else {
  578. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[collectionView indexPathsForSelectedItems]];
  579. }
  580. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  581. return;
  582. }
  583. [collectionView deselectItemAtIndexPath:indexPath animated:NO];
  584. NSArray *visibleCells = [[collectionView visibleCells] copy];
  585. NSUInteger cellCount = visibleCells.count;
  586. for (NSUInteger x = 0; x < cellCount; x++) {
  587. VLCPlaylistCollectionViewCell *cell = visibleCells[x];
  588. if ([cell showsMetaData])
  589. [cell showMetadata:NO];
  590. }
  591. NSManagedObject *selectedObject = [_mediaDataSource objectAtIndex:indexPath.row];
  592. if (selectedObject != nil)
  593. [self openMediaObject:selectedObject];
  594. }
  595. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  596. {
  597. if (self.editing) {
  598. [self updateActionBarButtonItemStateWithSelectedIndexPaths:[collectionView indexPathsForSelectedItems]];
  599. }
  600. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  601. }
  602. - (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
  603. {
  604. id mediaObject = [_mediaDataSource objectAtIndex:indexPath.item];
  605. [_mediaDataSource removeMediaObjectFromFolder:mediaObject];
  606. [self backToAllItems:nil];
  607. }
  608. - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
  609. {
  610. [_mediaDataSource moveObjectFromIndex:fromIndexPath.item toIndex:toIndexPath.item];
  611. }
  612. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSpringLoadItemAtIndexPath:(NSIndexPath *)indexPath withContext:(id<UISpringLoadedInteractionContext>)context NS_AVAILABLE_IOS(11.0)
  613. {
  614. id mediaObject = [_mediaDataSource objectAtIndex:indexPath.item];
  615. return [mediaObject isKindOfClass:[MLLabel class]];
  616. }
  617. - (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
  618. {
  619. id folderPathItem = [_mediaDataSource objectAtIndex:folderPath.item];
  620. id itemPathItem = [_mediaDataSource objectAtIndex:itemPath.item];
  621. BOOL validFileTypeAtFolderPath = ([folderPathItem isKindOfClass:[MLFile class]] || [folderPathItem isKindOfClass:[MLLabel class]]) && [itemPathItem isKindOfClass:[MLFile class]];
  622. if (!validFileTypeAtFolderPath) {
  623. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_INVALID_TYPE_TITLE", nil) message:NSLocalizedString(@"FOLDER_INVALID_TYPE_MESSAGE", nil) cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", nil)]];
  624. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  625. [self updateViewContents];
  626. };
  627. [alert show];
  628. return;
  629. }
  630. BOOL isFolder = [folderPathItem isKindOfClass:[MLLabel class]];
  631. if (isFolder){
  632. MLLabel *folder = folderPathItem;
  633. MLFile *file = itemPathItem;
  634. [file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
  635. file.folderTrackNumber = @([folder.files count] - 1);
  636. [_mediaDataSource removeObjectAtIndex:itemPath.item];
  637. [self updateViewContents];
  638. } else {
  639. _folderObject = folderPathItem;
  640. _indexPaths = [NSMutableArray arrayWithArray:@[itemPath]];
  641. [self showCreateFolderAlert];
  642. }
  643. }
  644. #pragma mark - collection/tableView helper
  645. - (NSArray *)selectedIndexPaths
  646. {
  647. NSArray *indexPaths;
  648. if (self.usingTableViewToShowData)
  649. indexPaths = [self.tableView indexPathsForSelectedRows];
  650. else
  651. indexPaths = [self.collectionView indexPathsForSelectedItems];
  652. return indexPaths ?: [NSArray array];
  653. }
  654. #pragma mark - Folder implementation
  655. - (void)showCreateFolderAlert
  656. {
  657. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_CHOOSE_NAME_TITLE", nil) message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", nil)]];
  658. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  659. UITextField *zeroTextField = [alert textFieldAtIndex:0];
  660. [zeroTextField setText:NSLocalizedString(@"FOLDER_NAME_PLACEHOLDER", nil)];
  661. [zeroTextField setClearButtonMode:UITextFieldViewModeAlways];
  662. __weak VLCAlertView *weakAlert = alert;
  663. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  664. if (cancelled)
  665. [self updateViewContents];
  666. else
  667. [self createFolderWithName:[weakAlert textFieldAtIndex:0].text];
  668. };
  669. [alert show];
  670. }
  671. - (void)createFolder
  672. {
  673. if (_libraryMode == VLCLibraryModeCreateFolder) {
  674. _libraryMode = _previousLibraryMode;
  675. [self updateViewContents];
  676. [self showCreateFolderAlert];
  677. return;
  678. }
  679. _indexPaths = [NSMutableArray arrayWithArray:[self selectedIndexPaths]];
  680. for (NSInteger i = _indexPaths.count - 1; i >=0; i--) {
  681. NSIndexPath *path = _indexPaths[i];
  682. id mediaObject = [_mediaDataSource objectAtIndex:path.row];
  683. if ([mediaObject isKindOfClass:[MLLabel class]])
  684. [_indexPaths removeObject:path];
  685. }
  686. if ([_indexPaths count] != 0) {
  687. NSArray *folder = [MLLabel allLabels];
  688. //if we already have folders display them
  689. if ([folder count] > 0) {
  690. [_mediaDataSource updateContentsForSelection:nil];
  691. [_mediaDataSource addAllFolders];
  692. self.title = NSLocalizedString(@"SELECT_FOLDER", nil);
  693. _previousLibraryMode = _libraryMode;
  694. _libraryMode = VLCLibraryModeCreateFolder;
  695. [self reloadViews];
  696. return;
  697. }
  698. }
  699. //no selected items or no existing folder ask for foldername
  700. [self showCreateFolderAlert];
  701. }
  702. - (void)removeFromFolder
  703. {
  704. _indexPaths = [NSMutableArray arrayWithArray:[self selectedIndexPaths]];
  705. [_indexPaths sortUsingSelector:@selector(compare:)];
  706. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  707. NSIndexPath *path = _indexPaths[i];
  708. id item = [_mediaDataSource objectAtIndex:path.row];
  709. [_mediaDataSource removeMediaObjectFromFolder:item];
  710. [_mediaDataSource removeObjectAtIndex:path.row];
  711. }
  712. [self reloadViews];
  713. }
  714. - (void)createFolderWithName:(NSString *)folderName
  715. {
  716. NSArray *labels = [MLLabel allLabels];
  717. for (MLLabel *label in labels){
  718. if ([label.name isEqualToString:folderName]) {
  719. _folderObject = nil;
  720. _indexPaths = nil;
  721. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_TITLE", nil) message:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_MESSAGE", nil) cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", nil)]];
  722. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  723. [self updateViewContents];
  724. };
  725. [alert show];
  726. return;
  727. }
  728. }
  729. if (_folderObject != nil) {
  730. id mediaObject;
  731. NSUInteger folderIndex = [_mediaDataSource indexOfObject:_folderObject];
  732. if (folderIndex != NSNotFound) {
  733. mediaObject = [_mediaDataSource objectAtIndex:folderIndex];
  734. }
  735. //item got dragged onto item
  736. if ([mediaObject isKindOfClass:[MLFile class]]) {
  737. MLFile *file = (MLFile *)mediaObject;
  738. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  739. label.name = folderName;
  740. file.labels = [NSSet setWithObjects:label,nil];
  741. NSNumber *folderTrackNumber = [NSNumber numberWithInt:(int)[label files].count - 1];
  742. file.folderTrackNumber = folderTrackNumber;
  743. [_mediaDataSource removeObjectAtIndex:folderIndex];
  744. [_mediaDataSource insertObject:label atIndex:folderIndex];
  745. id item = [_mediaDataSource objectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
  746. if (![item isKindOfClass:[MLFile class]])
  747. return;
  748. MLFile *itemFile = (MLFile *)item;
  749. itemFile.labels = file.labels;
  750. [_mediaDataSource removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
  751. itemFile.folderTrackNumber = @([label files].count - 1);
  752. } else {
  753. //item got dragged onto folder or items should be added to folder
  754. id label = [_mediaDataSource objectAtIndex:folderIndex];
  755. if (![label isKindOfClass:[MLLabel class]])
  756. return;
  757. [_indexPaths sortUsingSelector:@selector(compare:)];
  758. NSUInteger count = [_mediaDataSource numberOfFiles];
  759. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  760. NSIndexPath *path = _indexPaths[i];
  761. if (path.row >= count)
  762. continue;
  763. id object = [_mediaDataSource objectAtIndex:path.row];
  764. if (_libraryMode != VLCLibraryModeCreateFolder && ![object isKindOfClass:[MLFile class]])
  765. continue;
  766. if (_libraryMode == VLCLibraryModeCreateFolder)
  767. [self updateViewContents];
  768. id item = [_mediaDataSource objectAtIndex:path.row];
  769. if (![item isKindOfClass:[MLFile class]])
  770. continue;
  771. MLFile *file = (MLFile *)item;
  772. file.labels = [NSSet setWithObjects:label, nil];
  773. [_mediaDataSource removeObjectAtIndex:path.row];
  774. file.folderTrackNumber = @([label files].count - 1);
  775. }
  776. }
  777. _folderObject = nil;
  778. } else {
  779. //create new folder
  780. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  781. label.name = folderName;
  782. //if items were selected
  783. if ([_indexPaths count] != 0) {
  784. [_indexPaths sortUsingSelector:@selector(compare:)];
  785. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  786. NSIndexPath *path = _indexPaths[i];
  787. NSUInteger index = self.usingTableViewToShowData ? path.row : path.item;
  788. if (index < [_mediaDataSource numberOfFiles]) {
  789. id item = [_mediaDataSource objectAtIndex:index];
  790. if (![item isKindOfClass:[MLFile class]])
  791. continue;
  792. MLFile *file = (MLFile *)item;
  793. file.labels = [NSSet setWithObjects:label, nil];
  794. file.folderTrackNumber = @([label files].count - 1);
  795. [_mediaDataSource removeObjectAtIndex:index];
  796. }
  797. }
  798. }
  799. }
  800. _indexPaths = nil;
  801. [self setEditing:NO];
  802. [self updateViewContents];
  803. }
  804. - (void)_collectionViewHandleLongPressGesture:(UIGestureRecognizer *) sender
  805. {
  806. if (sender.state == UIGestureRecognizerStateBegan && !self.isEditing)
  807. [self setEditing:YES animated:YES];
  808. }
  809. #pragma mark - UI implementation
  810. - (void)handleSelection
  811. {
  812. if (self.usingTableViewToShowData) {
  813. NSInteger numberOfSections = [self.tableView numberOfSections];
  814. for (NSInteger section = 0; section < numberOfSections; section++) {
  815. NSInteger numberOfRowInSection = [self.tableView numberOfRowsInSection:section];
  816. for (NSInteger row = 0; row < numberOfRowInSection; row++) {
  817. if (!_isSelected)
  818. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionNone];
  819. else
  820. [self.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO];
  821. }
  822. }
  823. } else {
  824. NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
  825. for (NSInteger item = 0; item < numberOfItems; item++) {
  826. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:item inSection:0];
  827. if (!_isSelected)
  828. [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  829. else
  830. [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
  831. [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  832. }
  833. }
  834. _isSelected = !_isSelected;
  835. }
  836. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  837. {
  838. [super setEditing:editing animated:animated];
  839. _isSelected = NO;
  840. UIBarButtonItem *editButton = self.editButtonItem;
  841. editButton.tintColor = [UIColor whiteColor];
  842. if (!editing && self.navigationItem.rightBarButtonItems.lastObject == _selectAllBarButtonItem)
  843. [self.navigationItem setRightBarButtonItems: [self.navigationItem.rightBarButtonItems subarrayWithRange:NSMakeRange(0, self.navigationItem.rightBarButtonItems.count - 1)]];
  844. else
  845. [self.navigationItem setRightBarButtonItems:editing ? [self.navigationItem.rightBarButtonItems arrayByAddingObject:_selectAllBarButtonItem] : [self.navigationItem rightBarButtonItems] animated:YES];
  846. [self setSearchBar:!editing resetContent:!editing];
  847. self.tableView.allowsMultipleSelectionDuringEditing = editing;
  848. [self.tableView setEditing:editing animated:YES];
  849. NSArray *visibleCells = self.collectionView.visibleCells;
  850. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  851. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  852. [aCell setEditing:editing animated:animated];
  853. }];
  854. self.collectionView.allowsMultipleSelection = editing;
  855. /* UIKit doesn't clear the selection automagically if we leave the editing mode
  856. * so we need to do so manually */
  857. if (!editing) {
  858. [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
  859. NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
  860. NSUInteger count = selectedItems.count;
  861. for (NSUInteger x = 0; x < count; x++)
  862. [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
  863. } else
  864. [self.collectionView removeGestureRecognizer:_longPressGestureRecognizer];
  865. if (_libraryMode == VLCLibraryModeCreateFolder) {
  866. _libraryMode = _previousLibraryMode;
  867. _indexPaths = nil;
  868. [self updateViewContents];
  869. }
  870. [self.navigationController setToolbarHidden:!editing animated:YES];
  871. [UIView performWithoutAnimation:^{
  872. [editButton setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL", nil) : NSLocalizedString(@"BUTTON_EDIT", nil)];
  873. }];
  874. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  875. NSMutableArray *rightBarButtonItems = [self.navigationItem.rightBarButtonItems mutableCopy];
  876. UIBarButtonItem *toggleDisplayedView = rightBarButtonItems[0];
  877. toggleDisplayedView.enabled = !editing;
  878. rightBarButtonItems[0] = toggleDisplayedView;
  879. self.navigationItem.rightBarButtonItems = rightBarButtonItems;
  880. }
  881. }
  882. - (void)toggleDisplayedView:(UIBarButtonItem *)button
  883. {
  884. self.usingTableViewToShowData = !self.usingTableViewToShowData;
  885. UIImage *newButtonImage = [UIImage imageNamed: self.usingTableViewToShowData ? @"collectionViewIcon" : @"tableViewIcon"];
  886. [button setImage:newButtonImage];
  887. [self updateViewsForCurrentDisplayMode];
  888. }
  889. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  890. {
  891. return UITableViewCellEditingStyleDelete;
  892. }
  893. - (IBAction)leftButtonAction:(id)sender
  894. {
  895. [[VLCSidebarController sharedInstance] toggleSidebar];
  896. if (self.isEditing)
  897. [self setEditing:NO animated:YES];
  898. }
  899. - (IBAction)backToAllItems:(id)sender
  900. {
  901. _inFolder = NO;
  902. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  903. toolbarItems[2] = _createFolderBarButtonItem;
  904. self.toolbarItems = toolbarItems;
  905. [self setLibraryMode:_previousLibraryMode];
  906. if (!self.isEditing) {
  907. [self setSearchBar:YES resetContent:NO];
  908. }
  909. [self updateViewContents];
  910. }
  911. - (void)_endEditingWithHardReset:(BOOL)hardReset
  912. {
  913. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  914. if (hardReset)
  915. [self updateViewContents];
  916. else
  917. [self reloadViews];
  918. [self setEditing:NO animated:YES];
  919. }
  920. - (void)deleteSelection:(id)sender
  921. {
  922. NSArray *indexPaths = [self usingTableViewToShowData] ? [self.tableView indexPathsForSelectedRows] : [self.collectionView indexPathsForSelectedItems];
  923. if ((!indexPaths || [indexPaths count] == 0) && !_deleteFromTableView) {
  924. if (@available(iOS 8, *)) {
  925. UIAlertController *invalidSelection = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DELETE_INVALID_TITLE", nil) message:NSLocalizedString(@"DELETE_INVALID_MESSAGE", nil) preferredStyle:UIAlertControllerStyleAlert];
  926. UIAlertAction *doneAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_OK", nil) style:UIAlertActionStyleDefault handler:nil];
  927. [invalidSelection addAction:doneAction];
  928. [self presentViewController:invalidSelection animated:YES completion:nil];
  929. } else {
  930. //TODO remove when we drop iOS7
  931. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DELETE_INVALID_TITLE", nil)
  932. message:NSLocalizedString(@"DELETE_INVALID_MESSAGE", nil)
  933. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  934. otherButtonTitles:nil];
  935. alert.completion = nil;
  936. [alert show];
  937. }
  938. } else {
  939. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DELETE_TITLE", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  940. UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  941. if (_deleteFromTableView) {
  942. NSIndexPath *indexPath = (NSIndexPath *)sender;
  943. if (indexPath && indexPath.row < [_mediaDataSource numberOfFiles])
  944. [self removeMediaObject:[_mediaDataSource objectAtIndex:indexPath.row] updateDatabase:YES];
  945. } else
  946. [self deletionAfterConfirmation];
  947. _deleteFromTableView = NO;
  948. }];
  949. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  950. _deleteFromTableView ? [self setEditing:NO animated:YES] : [self reloadViews];
  951. _deleteFromTableView = NO;
  952. }];
  953. [alert addAction:deleteAction];
  954. [alert addAction:cancelAction];
  955. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  956. CGRect tmpBounds = self.view.bounds;
  957. [alert setTitle:NSLocalizedString(@"DELETE_TITLE", nil)];
  958. [alert setMessage:NSLocalizedString(@"DELETE_MESSAGE", nil)];
  959. [alert.popoverPresentationController setPermittedArrowDirections:0];
  960. [alert.popoverPresentationController setSourceView:self.view];
  961. [alert.popoverPresentationController setSourceRect:CGRectMake(tmpBounds.size.width / 2.0, tmpBounds.size.height / 2.0, 1.0, 1.0)];
  962. }
  963. [self presentViewController:alert animated:YES completion:nil];
  964. }
  965. }
  966. - (void)deletionAfterConfirmation
  967. {
  968. NSArray *indexPaths;
  969. if (self.usingTableViewToShowData) {
  970. indexPaths = [self.tableView indexPathsForSelectedRows];
  971. } else {
  972. indexPaths = [self.collectionView indexPathsForSelectedItems];
  973. }
  974. NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:indexPaths.count];
  975. for (NSIndexPath *indexPath in indexPaths)
  976. [objects addObject:[_mediaDataSource objectAtIndex:indexPath.row]];
  977. for (id object in objects)
  978. [self removeMediaObject:object updateDatabase:NO];
  979. [self _endEditingWithHardReset:YES];
  980. }
  981. - (void)renameSelection
  982. {
  983. NSArray *indexPaths = [self selectedIndexPaths];
  984. if (indexPaths.count < 1) {
  985. [self _endEditingWithHardReset:NO];
  986. return;
  987. }
  988. NSString *itemName;
  989. if (self.usingTableViewToShowData)
  990. itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
  991. else
  992. itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
  993. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", nil), itemName] message:nil cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:@[NSLocalizedString(@"BUTTON_RENAME", nil)]];
  994. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  995. [[alert textFieldAtIndex:0] setText:itemName];
  996. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  997. __weak VLCAlertView *weakAlert = alert;
  998. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  999. if (cancelled)
  1000. [self _endEditingWithHardReset:NO];
  1001. else
  1002. [self renameMediaObjectTo:[weakAlert textFieldAtIndex:0].text];
  1003. };
  1004. [alert show];
  1005. }
  1006. - (void)renameMediaObjectTo:(NSString*)newName
  1007. {
  1008. NSArray *indexPaths = [self selectedIndexPaths];
  1009. if (indexPaths.count < 1)
  1010. return;
  1011. NSUInteger row = [indexPaths[0] row];
  1012. id mediaObject = [_mediaDataSource objectAtIndex:row];
  1013. if (!mediaObject) return;
  1014. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]] || [mediaObject isKindOfClass:[MLLabel class]] )
  1015. [mediaObject setName:newName];
  1016. else
  1017. [mediaObject setTitle:newName];
  1018. if (self.usingTableViewToShowData)
  1019. [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
  1020. else
  1021. [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
  1022. if (indexPaths.count > 1)
  1023. [self renameSelection];
  1024. else
  1025. [self _endEditingWithHardReset:NO];
  1026. }
  1027. - (void)updateViewsForCurrentDisplayMode
  1028. {
  1029. UIImage *newButtonImage = [UIImage imageNamed: self.usingTableViewToShowData ? @"collectionViewIcon" : @"tableViewIcon"];
  1030. [self.displayModeBarButtonItem setImage:newButtonImage];
  1031. }
  1032. #pragma mark - Sharing
  1033. // We take the array of index paths (instead of a count) to actually examine if the content is shareable. Selecting a single folder should not enable the share button.
  1034. - (void)updateActionBarButtonItemStateWithSelectedIndexPaths:(NSArray *)indexPaths
  1035. {
  1036. NSUInteger count = [indexPaths count];
  1037. if (!indexPaths || count == 0) {
  1038. _shareBarButtonItem.enabled = NO;
  1039. } else {
  1040. // Look for at least one MLFile
  1041. for (NSIndexPath *indexPath in indexPaths) {
  1042. id mediaItem = [_mediaDataSource objectAtIndex:[indexPath row]];
  1043. if ([mediaItem isKindOfClass:[MLFile class]] || [mediaItem isKindOfClass:[MLAlbumTrack class]] | [mediaItem isKindOfClass:[MLShowEpisode class]]) {
  1044. _shareBarButtonItem.enabled = YES;
  1045. return;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. - (NSArray *)fileURLsFromSelection
  1051. {
  1052. NSArray *indexPaths = [self selectedIndexPaths];
  1053. if (indexPaths.count == 0) return nil;
  1054. NSMutableArray<NSURL*> *fileURLObjects = [[NSMutableArray alloc] initWithCapacity:indexPaths.count];
  1055. for (NSIndexPath *indexpath in indexPaths) {
  1056. id mediaItem = [_mediaDataSource objectAtIndex:[indexpath row]];
  1057. NSURL *fileURL;
  1058. if ([mediaItem isKindOfClass:[MLFile class]]) {
  1059. fileURL = [(MLFile *) mediaItem url];
  1060. } else if ([mediaItem isKindOfClass:[MLAlbumTrack class]]) {
  1061. fileURL = [[(MLAlbumTrack *) mediaItem anyFileFromTrack] url];
  1062. } else if ([mediaItem isKindOfClass:[MLShowEpisode class]]) {
  1063. fileURL = [[(MLShowEpisode *) mediaItem anyFileFromEpisode] url];
  1064. }
  1065. if ([fileURL isFileURL]) {
  1066. [fileURLObjects addObject:fileURL];
  1067. }
  1068. }
  1069. return [fileURLObjects copy];
  1070. }
  1071. - (void)share:(UIBarButtonItem *)barButtonItem
  1072. {
  1073. NSParameterAssert(barButtonItem);
  1074. if (!barButtonItem) {
  1075. APLog(@"Missing a UIBarButtonItem to present from");
  1076. return;
  1077. }
  1078. //disable any possible changes to selection (or exit from this screen)
  1079. [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
  1080. UIActivityViewController *controller = [VLCActivityViewControllerVendor activityViewControllerForFiles:[self fileURLsFromSelection] presentingButton:barButtonItem presentingViewController:self];
  1081. if (!controller) {
  1082. [[UIApplication sharedApplication] endIgnoringInteractionEvents];
  1083. } else {
  1084. controller.popoverPresentationController.sourceView = self.navigationController.toolbar;
  1085. [self.navigationController presentViewController:controller animated:YES completion:^{
  1086. [[UIApplication sharedApplication] endIgnoringInteractionEvents];
  1087. }];
  1088. }
  1089. }
  1090. #pragma mark - properties
  1091. - (void)setLibraryMode:(VLCLibraryMode)mode
  1092. {
  1093. _libraryMode = mode;
  1094. [self updateViewContents];
  1095. if (mode == VLCLibraryModeAllAlbums ||
  1096. mode == VLCLibraryModeAllSeries ||
  1097. mode == VLCLibraryModeAllFiles) {
  1098. _previousLibraryMode = mode;
  1099. }
  1100. }
  1101. - (BOOL)usingTableViewToShowData
  1102. {
  1103. return [[NSUserDefaults standardUserDefaults] boolForKey:kUsingTableViewToShowData];
  1104. }
  1105. - (void)setUsingTableViewToShowData:(BOOL)usingTableViewToShowData
  1106. {
  1107. [[NSUserDefaults standardUserDefaults] setBool:usingTableViewToShowData forKey:kUsingTableViewToShowData];
  1108. [self updateViewsForCurrentDisplayMode];
  1109. [self setupContentView];
  1110. }
  1111. #pragma mark - autorotation
  1112. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  1113. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  1114. /* introduced in iOS 6 */
  1115. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  1116. {
  1117. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  1118. return UIInterfaceOrientationMaskAll;
  1119. return ([_mediaDataSource numberOfFiles] > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  1120. UIInterfaceOrientationMaskPortrait;
  1121. }
  1122. /* introduced in iOS 6 */
  1123. - (BOOL)shouldAutorotate
  1124. {
  1125. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || ([_mediaDataSource numberOfFiles] > 0);
  1126. }
  1127. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  1128. {
  1129. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  1130. [self setViewFromDeviceOrientation];
  1131. NSArray *visibleCells = [self.tableView visibleCells];
  1132. NSUInteger cellCount = visibleCells.count;
  1133. for (NSUInteger x = 0; x < cellCount; x++) {
  1134. if ([visibleCells[x] isExpanded])
  1135. [visibleCells[x] metaDataLabel].hidden = YES;
  1136. }
  1137. [self.collectionView.collectionViewLayout invalidateLayout];
  1138. }
  1139. // >= iOS 8
  1140. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  1141. {
  1142. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  1143. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  1144. [self setViewFromDeviceOrientation];
  1145. [self.collectionView.collectionViewLayout invalidateLayout];
  1146. } completion:nil];
  1147. }
  1148. #pragma mark - UIScrollViewDelegate
  1149. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  1150. {
  1151. if ([_searchController.searchBar isFirstResponder])
  1152. [_searchController.searchBar resignFirstResponder];
  1153. }
  1154. #pragma mark - SearchController Delegate
  1155. - (void)didPresentSearchController:(UISearchController *)searchController
  1156. {
  1157. _tableView.dataSource = _searchDataSource;
  1158. }
  1159. - (void)didDismissSearchController:(UISearchController *)searchController
  1160. {
  1161. _tableView.dataSource = self;
  1162. }
  1163. #pragma mark - Search Research Updater
  1164. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  1165. {
  1166. [_searchDataSource shouldReloadTableForSearchString:searchController.searchBar.text searchableFiles:[_mediaDataSource allObjects]];
  1167. [_tableView reloadData];
  1168. }
  1169. #pragma mark - handoff
  1170. - (void)restoreUserActivityState:(NSUserActivity *)activity
  1171. {
  1172. NSString *userActivityType = activity.activityType;
  1173. if([userActivityType isEqualToString:kVLCUserActivityLibraryMode] ||
  1174. [userActivityType isEqualToString:kVLCUserActivityLibrarySelection]) {
  1175. NSDictionary *dict = activity.userInfo;
  1176. NSString *folderPath = dict[@"folder"];
  1177. if (!folderPath) return;
  1178. NSURL *folderURL = [NSURL URLWithString:folderPath];
  1179. NSUInteger count = [_mediaDataSource numberOfFiles];
  1180. for (NSUInteger i = 0; i < count; i++) {
  1181. NSManagedObject *object = [_mediaDataSource objectAtIndex:i];
  1182. if([object.objectID.URIRepresentation isEqual:folderURL]) {
  1183. [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
  1184. }
  1185. }
  1186. }
  1187. }
  1188. @end