VLCLibraryViewController.m 60 KB

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