VLCLibraryViewController.m 59 KB

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