VLCLibraryViewController.m 59 KB

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