VLCLibraryViewController.m 59 KB

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