VLCPlaylistViewController.m 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*****************************************************************************
  2. * VLCPlaylistViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2014 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. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLCPlaylistViewController.h"
  16. #import "VLCMovieViewController.h"
  17. #import "VLCPlaylistTableViewCell.h"
  18. #import "VLCPlaylistCollectionViewCell.h"
  19. #import "UINavigationController+Theme.h"
  20. #import "NSString+SupportedMedia.h"
  21. #import "VLCBugreporter.h"
  22. #import "VLCAppDelegate.h"
  23. #import "UIBarButtonItem+Theme.h"
  24. #import "VLCFirstStepsViewController.h"
  25. #import "VLCFolderCollectionViewFlowLayout.h"
  26. #import "LXReorderableCollectionViewFlowLayout.h"
  27. #import "VLCAlertView.h"
  28. /* prefs keys */
  29. static NSString *kDisplayedFirstSteps = @"Did we display the first steps tutorial?";
  30. @implementation EmptyLibraryView
  31. - (IBAction)learnMore:(id)sender
  32. {
  33. UIViewController *firstStepsVC = [[VLCFirstStepsViewController alloc] initWithNibName:nil bundle:nil];
  34. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:firstStepsVC];
  35. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  36. [navCon loadTheme];
  37. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  38. }
  39. @end
  40. @interface VLCPlaylistViewController () <VLCFolderCollectionViewDelegateFlowLayout, LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary> {
  41. NSMutableArray *_foundMedia;
  42. VLCLibraryMode _libraryMode;
  43. VLCLibraryMode _previousLibraryMode;
  44. UIBarButtonItem *_menuButton;
  45. NSMutableArray *_indexPaths;
  46. id _folderObject;
  47. VLCFolderCollectionViewFlowLayout *_folderLayout;
  48. LXReorderableCollectionViewFlowLayout *_reorderLayout;
  49. BOOL inFolder;
  50. UILongPressGestureRecognizer *_longPressGestureRecognizer;
  51. }
  52. @property (nonatomic, strong) UITableView *tableView;
  53. @property (nonatomic, strong) UICollectionView *collectionView;
  54. @property (nonatomic, strong) EmptyLibraryView *emptyLibraryView;
  55. @end
  56. @implementation VLCPlaylistViewController
  57. + (void)initialize
  58. {
  59. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  60. [defaults registerDefaults:@{kDisplayedFirstSteps : [NSNumber numberWithBool:NO]}];
  61. }
  62. - (void)loadView
  63. {
  64. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  65. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  66. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  67. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  68. _tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  69. _tableView.delegate = self;
  70. _tableView.dataSource = self;
  71. _tableView.opaque = YES;
  72. self.view = _tableView;
  73. } else {
  74. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  75. _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_folderLayout];
  76. _collectionView.alwaysBounceVertical = YES;
  77. _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  78. _collectionView.delegate = self;
  79. _collectionView.dataSource = self;
  80. _collectionView.opaque = YES;
  81. _collectionView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  82. self.view = _collectionView;
  83. _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_collectionViewHandleLongPressGesture:)];
  84. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  85. if (SYSTEM_RUNS_IOS7_OR_LATER)
  86. [_collectionView registerNib:[UINib nibWithNibName:@"VLCFuturePlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  87. else
  88. [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  89. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  90. }
  91. _libraryMode = VLCLibraryModeAllFiles;
  92. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  93. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  94. _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
  95. _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  96. }
  97. #pragma mark -
  98. - (void)viewDidLoad
  99. {
  100. [super viewDidLoad];
  101. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  102. _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  103. self.navigationItem.leftBarButtonItem = _menuButton;
  104. if (SYSTEM_RUNS_IOS7_OR_LATER)
  105. self.editButtonItem.tintColor = [UIColor whiteColor];
  106. else {
  107. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  108. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  109. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  110. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  111. }
  112. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  113. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  114. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  115. [_emptyLibraryView.learnMoreButton setTitle:NSLocalizedString(@"BUTTON_LEARN_MORE", @"") forState:UIControlStateNormal];
  116. UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  117. [self setToolbarItems:@[createFolderItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_RENAME", @"") target:self andSelector:@selector(renameSelection)], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteSelection)]]];
  118. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  119. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  120. self.navigationController.toolbar.tintColor = [UIColor whiteColor];
  121. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  122. } else
  123. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  124. }
  125. - (void)viewWillAppear:(BOOL)animated
  126. {
  127. [super viewWillAppear:animated];
  128. [self.collectionView.collectionViewLayout invalidateLayout];
  129. [self _displayEmptyLibraryViewIfNeeded];
  130. }
  131. - (void)viewDidAppear:(BOOL)animated
  132. {
  133. [super viewDidAppear:animated];
  134. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  135. if (![[defaults objectForKey:kDisplayedFirstSteps] boolValue]) {
  136. [self.emptyLibraryView performSelector:@selector(learnMore:) withObject:nil afterDelay:1.];
  137. [defaults setObject:[NSNumber numberWithBool:YES] forKey:kDisplayedFirstSteps];
  138. [defaults synchronize];
  139. }
  140. if ([[MLMediaLibrary sharedMediaLibrary] libraryNeedsUpgrade]) {
  141. self.navigationItem.rightBarButtonItem = nil;
  142. self.navigationItem.leftBarButtonItem = nil;
  143. self.title = @"";
  144. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"UPGRADING_LIBRARY", @"");
  145. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = YES;
  146. [self.emptyLibraryView.activityIndicator startAnimating];
  147. self.emptyLibraryView.frame = self.view.bounds;
  148. [self.view addSubview:self.emptyLibraryView];
  149. [[MLMediaLibrary sharedMediaLibrary] setDelegate: self];
  150. [[MLMediaLibrary sharedMediaLibrary] performSelectorInBackground:@selector(upgradeLibrary) withObject:nil];
  151. return;
  152. }
  153. if (_foundMedia.count < 1)
  154. [self updateViewContents];
  155. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  156. }
  157. - (void)viewDidDisappear:(BOOL)animated
  158. {
  159. [super viewDidDisappear:animated];
  160. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  161. }
  162. - (BOOL)canBecomeFirstResponder
  163. {
  164. return YES;
  165. }
  166. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  167. {
  168. if (motion == UIEventSubtypeMotionShake)
  169. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  170. }
  171. - (void)openMediaObject:(NSManagedObject *)mediaObject
  172. {
  173. if ([mediaObject isKindOfClass:[MLAlbum class]]) {
  174. _foundMedia = [NSMutableArray arrayWithArray:[(MLAlbum *)mediaObject sortedTracks]];
  175. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  176. if (_libraryMode == VLCLibraryModeAllFiles)
  177. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  178. else
  179. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  180. self.title = [(MLAlbum*)mediaObject name];
  181. [self reloadViews];
  182. } else if ([mediaObject isKindOfClass:[MLShow class]]) {
  183. _foundMedia = [NSMutableArray arrayWithArray:[(MLShow *)mediaObject sortedEpisodes]];
  184. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  185. if (_libraryMode == VLCLibraryModeAllFiles)
  186. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  187. else
  188. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  189. self.title = [(MLShow*)mediaObject name];
  190. [self reloadViews];
  191. } else if ([mediaObject isKindOfClass:[MLLabel class]]) {
  192. MLLabel *folder = (MLLabel*) mediaObject;
  193. inFolder = YES;
  194. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  195. if (![self.collectionView.collectionViewLayout isEqual:_reorderLayout]) {
  196. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  197. if (recognizer == _folderLayout.panGestureRecognizer || recognizer == _folderLayout.longPressGestureRecognizer || recognizer == _longPressGestureRecognizer)
  198. [self.collectionView removeGestureRecognizer:recognizer];
  199. }
  200. _reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
  201. [self.collectionView setCollectionViewLayout:_reorderLayout animated:NO];
  202. _folderLayout = nil;
  203. }
  204. }
  205. _foundMedia = [NSMutableArray arrayWithArray:[folder sortedFolderItems]];
  206. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  207. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  208. self.title = [folder name];
  209. UIBarButtonItem *removeFromFolder = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(removeFromFolder)];
  210. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  211. toolbarItems[0] = removeFromFolder;
  212. self.toolbarItems = toolbarItems;
  213. [self reloadViews];
  214. return;
  215. } else
  216. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaFromManagedObject:mediaObject];
  217. }
  218. - (void)removeMediaObject:(id)managedObject updateDatabase:(BOOL)updateDB
  219. {
  220. // delete all tracks from an album
  221. if ([managedObject isKindOfClass:[MLAlbum class]]) {
  222. MLAlbum *album = managedObject;
  223. NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
  224. for (MLAlbumTrack *track in iterAlbumTrack) {
  225. NSSet *iterFiles = [NSSet setWithSet:track.files];
  226. for (MLFile *file in iterFiles)
  227. [self _deleteMediaObject:file];
  228. }
  229. // delete all episodes from a show
  230. } else if ([managedObject isKindOfClass:[MLShow class]]) {
  231. MLShow *show = managedObject;
  232. NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
  233. for (MLShowEpisode *episode in iterShowEpisodes) {
  234. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  235. for (MLFile *file in iterFiles)
  236. [self _deleteMediaObject:file];
  237. }
  238. // delete all files from an episode
  239. } else if ([managedObject isKindOfClass:[MLShowEpisode class]]) {
  240. MLShowEpisode *episode = managedObject;
  241. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  242. for (MLFile *file in iterFiles)
  243. [self _deleteMediaObject:file];
  244. // delete all files from a track
  245. } else if ([managedObject isKindOfClass:[MLAlbumTrack class]]) {
  246. MLAlbumTrack *track = managedObject;
  247. NSSet *iterFiles = [NSSet setWithSet:track.files];
  248. for (MLFile *file in iterFiles)
  249. [self _deleteMediaObject:file];
  250. } else if ([managedObject isKindOfClass:[MLLabel class]]) {
  251. MLLabel *folder = managedObject;
  252. NSSet *iterFiles = [NSSet setWithSet:folder.files];
  253. [folder removeFiles:folder.files];
  254. for (MLFile *file in iterFiles)
  255. [self _deleteMediaObject:file];
  256. [[MLMediaLibrary sharedMediaLibrary] removeObject:folder];
  257. }
  258. else
  259. [self _deleteMediaObject:managedObject];
  260. if (updateDB) {
  261. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  262. [self updateViewContents];
  263. }
  264. }
  265. - (void)_deleteMediaObject:(MLFile *)mediaObject
  266. {
  267. NSFileManager *fileManager = [NSFileManager defaultManager];
  268. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  269. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  270. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  271. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  272. return ([obj rangeOfString:fileName].location != NSNotFound);
  273. }];
  274. NSUInteger count = indexSet.count;
  275. NSString *additionalFilePath;
  276. NSUInteger currentIndex = [indexSet firstIndex];
  277. for (unsigned int x = 0; x < count; x++) {
  278. additionalFilePath = allfiles[currentIndex];
  279. if ([additionalFilePath isSupportedSubtitleFormat])
  280. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  281. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  282. }
  283. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  284. }
  285. - (void)_displayEmptyLibraryViewIfNeeded
  286. {
  287. if (self.emptyLibraryView.superview)
  288. [self.emptyLibraryView removeFromSuperview];
  289. if (_foundMedia.count == 0) {
  290. self.emptyLibraryView.emptyLibraryLabel.text = inFolder ? NSLocalizedString(@"FOLDER_EMPTY", @"") : NSLocalizedString(@"EMPTY_LIBRARY", @"");
  291. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.text = inFolder ? NSLocalizedString(@"FOLDER_EMPTY_LONG", @"") : NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  292. self.emptyLibraryView.learnMoreButton.hidden = inFolder;
  293. self.emptyLibraryView.frame = self.view.bounds;
  294. [self.view addSubview:self.emptyLibraryView];
  295. self.navigationItem.rightBarButtonItem = nil;
  296. } else
  297. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  298. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  299. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  300. UITableViewCellSeparatorStyleNone;
  301. } else
  302. [self.collectionView.collectionViewLayout invalidateLayout];
  303. }
  304. - (void)libraryUpgradeComplete
  305. {
  306. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  307. self.navigationItem.leftBarButtonItem = _menuButton;
  308. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
  309. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  310. [self.emptyLibraryView.activityIndicator stopAnimating];
  311. [self.emptyLibraryView removeFromSuperview];
  312. [self updateViewContents];
  313. }
  314. - (void)libraryWasUpdated
  315. {
  316. [self updateViewContents];
  317. }
  318. - (void)updateViewContents
  319. {
  320. _foundMedia = [[NSMutableArray alloc] init];
  321. self.navigationItem.leftBarButtonItem = _menuButton;
  322. if (_libraryMode == VLCLibraryModeAllAlbums)
  323. self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
  324. else if( _libraryMode == VLCLibraryModeAllSeries)
  325. self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
  326. else
  327. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  328. /* add all albums */
  329. if (_libraryMode != VLCLibraryModeAllSeries) {
  330. NSArray *rawAlbums = [MLAlbum allAlbums];
  331. for (MLAlbum *album in rawAlbums) {
  332. if (album.name.length > 0 && album.tracks.count > 1)
  333. [_foundMedia addObject:album];
  334. }
  335. }
  336. if (_libraryMode == VLCLibraryModeAllAlbums) {
  337. [self reloadViews];
  338. return;
  339. }
  340. /* add all shows */
  341. NSArray *rawShows = [MLShow allShows];
  342. for (MLShow *show in rawShows) {
  343. if (show.name.length > 0 && show.episodes.count > 1)
  344. [_foundMedia addObject:show];
  345. }
  346. if (_libraryMode == VLCLibraryModeAllSeries) {
  347. [self reloadViews];
  348. return;
  349. }
  350. /* add all folders*/
  351. NSArray *allFolders = [MLLabel allLabels];
  352. for (MLLabel *folder in allFolders)
  353. [_foundMedia addObject:folder];
  354. /* add all remaining files */
  355. NSArray *allFiles = [MLFile allFiles];
  356. for (MLFile *file in allFiles) {
  357. if (file.labels.count > 0) continue;
  358. if (!file.isShowEpisode && !file.isAlbumTrack)
  359. [_foundMedia addObject:file];
  360. else if (file.isShowEpisode) {
  361. if (file.showEpisode.show.episodes.count < 2)
  362. [_foundMedia addObject:file];
  363. /* older MediaLibraryKit versions don't send a show name in a popular
  364. * corner case. hence, we need to work-around here and force a reload
  365. * afterwards as this could lead to the 'all my shows are gone'
  366. * syndrome (see #10435, #10464, #10432 et al) */
  367. if (file.showEpisode.show.name.length == 0) {
  368. file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", @"");
  369. [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:0.1];
  370. }
  371. } else if (file.isAlbumTrack) {
  372. if (file.albumTrack.album.tracks.count < 2)
  373. [_foundMedia addObject:file];
  374. }
  375. }
  376. [self reloadViews];
  377. }
  378. - (void)reloadViews
  379. {
  380. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  381. [self.tableView reloadData];
  382. else
  383. [self.collectionView reloadData];
  384. [self _displayEmptyLibraryViewIfNeeded];
  385. }
  386. #pragma mark - Table View
  387. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  388. {
  389. return 1;
  390. }
  391. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  392. {
  393. return _foundMedia.count;
  394. }
  395. // Customize the appearance of table view cells.
  396. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  397. {
  398. static NSString *CellIdentifier = @"PlaylistCell";
  399. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  400. if (cell == nil)
  401. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  402. UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightGestureAction:)];
  403. [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
  404. [cell addGestureRecognizer:swipeRight];
  405. NSInteger row = indexPath.row;
  406. cell.mediaObject = _foundMedia[row];
  407. return cell;
  408. }
  409. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  410. {
  411. MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
  412. [_foundMedia removeObjectAtIndex:fromIndexPath.item];
  413. [_foundMedia insertObject:object atIndex:toIndexPath.item];
  414. object.folderTrackNumber = @(toIndexPath.item - 1);
  415. object = [_foundMedia objectAtIndex:fromIndexPath.item];
  416. object.folderTrackNumber = @(fromIndexPath.item - 1);
  417. }
  418. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  419. {
  420. return inFolder;
  421. }
  422. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  423. {
  424. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  425. cell.multipleSelectionBackgroundView.backgroundColor = cell.backgroundColor;
  426. }
  427. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  428. {
  429. return YES;
  430. }
  431. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  432. {
  433. if (editingStyle == UITableViewCellEditingStyleDelete)
  434. [self removeMediaObject: _foundMedia[indexPath.row] updateDatabase:YES];
  435. }
  436. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  437. {
  438. if (tableView.isEditing) {
  439. if (_libraryMode == VLCLibraryModeCreateFolder) {
  440. _folderObject = _foundMedia[indexPath.row];
  441. _libraryMode = _previousLibraryMode;
  442. [self updateViewContents];
  443. [self createFolderWithName:nil];
  444. }
  445. return;
  446. }
  447. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  448. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  449. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  450. VLCMediaList *list;
  451. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  452. NSUInteger count = tracks.count;
  453. list = [[VLCMediaList alloc] init];
  454. MLFile *file;
  455. VLCMedia *media;
  456. for (NSInteger x = count - 1; x > -1; x--) {
  457. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  458. media = [VLCMedia mediaWithURL: [NSURL URLWithString:file.url]];
  459. [media parse];
  460. [list addMedia:media];
  461. }
  462. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  463. } else
  464. [self openMediaObject:selectedObject];
  465. }
  466. #pragma mark - table view gestures
  467. - (void)swipeRightGestureAction:(UIGestureRecognizer *)recognizer
  468. {
  469. if ([[self.editButtonItem title] isEqualToString:NSLocalizedString(@"BUTTON_CANCEL",@"")])
  470. [self setEditing:NO animated:YES];
  471. else {
  472. [self setEditing:YES animated:YES];
  473. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  474. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:path.section]
  475. animated:YES
  476. scrollPosition:UITableViewScrollPositionNone];
  477. }
  478. }
  479. #pragma mark - Collection View
  480. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  481. {
  482. return _foundMedia.count;
  483. }
  484. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  485. {
  486. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  487. cell.mediaObject = _foundMedia[indexPath.row];
  488. cell.collectionView = _collectionView;
  489. [cell setEditing:self.editing animated:NO];
  490. return cell;
  491. }
  492. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  493. {
  494. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  495. if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
  496. return CGSizeMake(341., 190.);
  497. else
  498. return CGSizeMake(384., 216.);
  499. }
  500. return CGSizeMake(298.0, 220.0);
  501. }
  502. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  503. {
  504. if (SYSTEM_RUNS_IOS7_OR_LATER)
  505. return UIEdgeInsetsMake(0., 0., 0., 0.);
  506. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  507. }
  508. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  509. {
  510. if (SYSTEM_RUNS_IOS7_OR_LATER)
  511. return 0.;
  512. return 10.0;
  513. }
  514. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  515. {
  516. if (SYSTEM_RUNS_IOS7_OR_LATER)
  517. return 0.;
  518. return 10.0;
  519. }
  520. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  521. {
  522. if (self.editing) {
  523. if (_libraryMode == VLCLibraryModeCreateFolder) {
  524. _folderObject = _foundMedia[indexPath.item];
  525. _libraryMode = _previousLibraryMode;
  526. [self createFolderWithName:nil];
  527. }
  528. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  529. return;
  530. }
  531. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  532. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  533. VLCMediaList *list;
  534. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  535. NSUInteger count = tracks.count;
  536. list = [[VLCMediaList alloc] init];
  537. MLFile *file;
  538. for (NSInteger x = count - 1; x > -1; x--) {
  539. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  540. [list addMedia:[VLCMedia mediaWithURL: [NSURL URLWithString:file.url]]];
  541. }
  542. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  543. } else if ([selectedObject isKindOfClass:[MLFile class]] && [((MLFile *)selectedObject).labels count] > 0) {
  544. VLCMediaList *list;
  545. MLLabel *folder = [((MLFile *)selectedObject).labels anyObject];
  546. NSArray *folderTracks = [folder.files allObjects];
  547. NSUInteger count = folderTracks.count;
  548. list = [[VLCMediaList alloc] init];
  549. MLFile *file;
  550. for (NSInteger x = count - 1; x > -1; x--) {
  551. file = (MLFile *)folderTracks[x];
  552. [list addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:file.url]]];
  553. }
  554. [(VLCAppDelegate *)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[folderTracks indexOfObject:selectedObject]];
  555. } else
  556. [self openMediaObject:selectedObject];
  557. }
  558. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  559. {
  560. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  561. }
  562. - (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
  563. {
  564. MLFile *mediaObject = (MLFile *)_foundMedia[indexPath.item];
  565. mediaObject.labels = nil;
  566. mediaObject.folderTrackNumber = nil;
  567. [self backToAllItems:nil];
  568. }
  569. - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
  570. {
  571. MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
  572. [_foundMedia removeObjectAtIndex:fromIndexPath.item];
  573. [_foundMedia insertObject:object atIndex:toIndexPath.item];
  574. object.folderTrackNumber = @(toIndexPath.item - 1);
  575. object = [_foundMedia objectAtIndex:fromIndexPath.item];
  576. object.folderTrackNumber = @(fromIndexPath.item - 1);
  577. }
  578. - (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
  579. {
  580. BOOL validFileTypeAtFolderPath = ([_foundMedia[folderPath.item] isKindOfClass:[MLFile class]] || [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]]) && [_foundMedia[itemPath.item] isKindOfClass:[MLFile class]];
  581. if (!validFileTypeAtFolderPath) {
  582. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_INVALID_TYPE_TITLE", @"") message:NSLocalizedString(@"FOLDER_INVALID_TYPE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  583. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  584. [self updateViewContents];
  585. };
  586. [alert show];
  587. return;
  588. }
  589. BOOL isFolder = [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
  590. if (isFolder){
  591. MLLabel *folder = _foundMedia[folderPath.item];
  592. MLFile *file = _foundMedia[itemPath.item];
  593. [file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
  594. file.folderTrackNumber = @([folder.files count] - 1);
  595. [_foundMedia removeObjectAtIndex:itemPath.item];
  596. [self updateViewContents];
  597. } else {
  598. _folderObject = _foundMedia[folderPath.item];
  599. _indexPaths = [NSMutableArray arrayWithArray:@[itemPath]];
  600. [self showCreateFolderAlert];
  601. }
  602. }
  603. #pragma mark - Folder implementation
  604. - (void)showCreateFolderAlert
  605. {
  606. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_CHOOSE_NAME_TITLE", @"") message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", @"") cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", @"")]];
  607. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  608. [[alert textFieldAtIndex:0] setText:NSLocalizedString(@"FOLDER_NAME_PLACEHOLDER", @"")];
  609. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  610. __weak VLCAlertView *weakAlert = alert;
  611. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  612. if (cancelled)
  613. [self updateViewContents];
  614. else
  615. [self createFolderWithName:[weakAlert textFieldAtIndex:0].text];
  616. };
  617. [alert show];
  618. }
  619. - (void)createFolder
  620. {
  621. if (_libraryMode == VLCLibraryModeCreateFolder) {
  622. _libraryMode = _previousLibraryMode;
  623. [self updateViewContents];
  624. [self showCreateFolderAlert];
  625. return;
  626. }
  627. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  628. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  629. else
  630. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  631. for (NSIndexPath *path in _indexPaths) {
  632. id mediaObject;
  633. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  634. mediaObject = _foundMedia[path.item];
  635. else
  636. mediaObject = _foundMedia[path.row];
  637. if ([mediaObject isKindOfClass:[MLLabel class]])
  638. [_indexPaths removeObject:path];
  639. }
  640. if ([_indexPaths count] != 0) {
  641. NSArray *folder = [MLLabel allLabels];
  642. //if we already have folders display them
  643. if ([folder count] > 0) {
  644. _foundMedia = [NSMutableArray arrayWithArray:folder];
  645. self.title = NSLocalizedString(@"SELECT_FOLDER", @"");
  646. _previousLibraryMode = _libraryMode;
  647. _libraryMode = VLCLibraryModeCreateFolder;
  648. [self reloadViews];
  649. return;
  650. }
  651. }
  652. //no selected items or no existing folder ask for foldername
  653. [self showCreateFolderAlert];
  654. }
  655. - (void)removeFromFolder
  656. {
  657. BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
  658. if (isPad)
  659. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  660. else
  661. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  662. [_indexPaths sortUsingSelector:@selector(compare:)];
  663. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  664. NSIndexPath *path = _indexPaths[i];
  665. MLFile *file = (MLFile *)_foundMedia[isPad ? path.item : path.row];
  666. MLLabel *folder = [file.labels anyObject];
  667. file.labels = nil;
  668. file.folderTrackNumber = nil;
  669. [_foundMedia removeObject:file];
  670. if ([folder.files count] == 0) {
  671. [self removeMediaObject:folder updateDatabase:YES];
  672. [self backToAllItems:nil];
  673. }
  674. }
  675. [self reloadViews];
  676. }
  677. - (void)createFolderWithName:(NSString *)folderName
  678. {
  679. NSArray *labels = [MLLabel allLabels];
  680. for (MLLabel *label in labels){
  681. if ([label.name isEqualToString:folderName]) {
  682. _folderObject = nil;
  683. _indexPaths = nil;
  684. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_TITLE", @"") message:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  685. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  686. [self updateViewContents];
  687. };
  688. [alert show];
  689. return;
  690. }
  691. }
  692. if (_folderObject != nil) {
  693. NSUInteger folderIndex = [_foundMedia indexOfObject:_folderObject];
  694. //item got dragged onto item
  695. if ([_foundMedia[folderIndex] isKindOfClass:[MLFile class]]) {
  696. MLFile *file = _foundMedia[folderIndex];
  697. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  698. label.name = folderName;
  699. file.labels = [NSSet setWithObjects:label,nil];
  700. NSNumber *folderTrackNumber = [NSNumber numberWithInt:(int)[label files].count - 1];
  701. file.folderTrackNumber = folderTrackNumber;
  702. [_foundMedia removeObjectAtIndex:folderIndex];
  703. [_foundMedia insertObject:label atIndex:folderIndex];
  704. MLFile *itemFile = _foundMedia[((NSIndexPath *)_indexPaths[0]).item];
  705. itemFile.labels = file.labels;
  706. [_foundMedia removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
  707. itemFile.folderTrackNumber = @([label files].count - 1);
  708. } else {
  709. //item got dragged onto folder or items should be added to folder
  710. MLLabel *label = _foundMedia[folderIndex];
  711. [_indexPaths sortUsingSelector:@selector(compare:)];
  712. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  713. NSIndexPath *path = _indexPaths[i];
  714. if (![_foundMedia[path.row] isKindOfClass:[MLFile class]])
  715. continue;
  716. MLFile *file = _foundMedia[path.row];
  717. file.labels = [NSSet setWithObjects:label, nil];
  718. [_foundMedia removeObjectAtIndex:path.row];
  719. file.folderTrackNumber = @([label files].count - 1);
  720. }
  721. }
  722. _folderObject = nil;
  723. } else {
  724. //create new folder
  725. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  726. label.name = folderName;
  727. //if items were selected
  728. if ([_indexPaths count] != 0) {
  729. [_indexPaths sortUsingSelector:@selector(compare:)];
  730. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  731. NSIndexPath *path = _indexPaths[i];
  732. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  733. MLFile *file = _foundMedia[path.item];
  734. file.labels = [NSSet setWithObjects:label, nil];
  735. file.folderTrackNumber = @([label files].count - 1);
  736. [_foundMedia removeObjectAtIndex:path.item];
  737. } else {
  738. MLFile *file = _foundMedia[path.row];
  739. file.labels = [NSSet setWithObjects:label, nil];
  740. file.folderTrackNumber = @([label files].count - 1);
  741. [_foundMedia removeObjectAtIndex:path.row];
  742. }
  743. }
  744. }
  745. }
  746. _indexPaths = nil;
  747. [self setEditing:NO];
  748. [self updateViewContents];
  749. }
  750. - (void)_collectionViewHandleLongPressGesture:(UIGestureRecognizer *) sender
  751. {
  752. [self setEditing:YES animated:YES];
  753. }
  754. #pragma mark - UI implementation
  755. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  756. {
  757. [super setEditing:editing animated:animated];
  758. UIBarButtonItem *editButton = self.editButtonItem;
  759. NSString *editImage = editing? @"doneButton": @"button";
  760. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  761. if (SYSTEM_RUNS_IOS7_OR_LATER)
  762. editButton.tintColor = [UIColor whiteColor];
  763. else {
  764. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  765. barMetrics:UIBarMetricsDefault];
  766. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  767. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  768. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  769. }
  770. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  771. NSArray *visibleCells = self.collectionView.visibleCells;
  772. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  773. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  774. [aCell setEditing:editing animated:animated];
  775. }];
  776. self.collectionView.allowsMultipleSelection = editing;
  777. /* UIKit doesn't clear the selection automagically if we leave the editing mode
  778. * so we need to do so manually */
  779. if (!editing) {
  780. [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
  781. NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
  782. NSUInteger count = selectedItems.count;
  783. for (NSUInteger x = 0; x < count; x++)
  784. [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
  785. } else
  786. [self.collectionView removeGestureRecognizer:_longPressGestureRecognizer];
  787. } else {
  788. self.tableView.allowsMultipleSelectionDuringEditing = editing;
  789. [self.tableView setEditing:editing animated:YES];
  790. [self.editButtonItem setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL",@"") : NSLocalizedString(@"BUTTON_EDIT", @"")];
  791. }
  792. if (_libraryMode == VLCLibraryModeCreateFolder) {
  793. _libraryMode = _previousLibraryMode;
  794. _indexPaths = nil;
  795. [self updateViewContents];
  796. }
  797. self.navigationController.toolbarHidden = !editing;
  798. }
  799. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  800. {
  801. return UITableViewCellEditingStyleDelete;
  802. }
  803. - (IBAction)leftButtonAction:(id)sender
  804. {
  805. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  806. if (self.isEditing)
  807. [self setEditing:NO animated:YES];
  808. }
  809. - (IBAction)backToAllItems:(id)sender
  810. {
  811. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  812. if (![self.collectionView.collectionViewLayout isEqual:_folderLayout]) {
  813. //for some reason the Gesturerecognizer block themselves if not removed manually
  814. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  815. if (recognizer == _reorderLayout.panGestureRecognizer || recognizer == _reorderLayout.longPressGestureRecognizer)
  816. [self.collectionView removeGestureRecognizer:recognizer];
  817. }
  818. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  819. [self.collectionView setCollectionViewLayout:_folderLayout animated:NO];
  820. _reorderLayout = nil;
  821. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  822. }
  823. }
  824. inFolder = NO;
  825. UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  826. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  827. toolbarItems[0] = createFolderItem;
  828. self.toolbarItems = toolbarItems;
  829. [self setLibraryMode:_libraryMode];
  830. [self updateViewContents];
  831. }
  832. - (void)_endEditingWithHardReset:(BOOL)hardReset
  833. {
  834. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  835. if (hardReset)
  836. [self updateViewContents];
  837. else
  838. [self reloadViews];
  839. [self setEditing:NO animated:YES];
  840. }
  841. - (void)deleteSelection
  842. {
  843. NSArray *indexPaths;
  844. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  845. indexPaths = [self.collectionView indexPathsForSelectedItems];
  846. else
  847. indexPaths = [self.tableView indexPathsForSelectedRows];
  848. NSUInteger count = indexPaths.count;
  849. NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:count];
  850. for (NSUInteger x = 0; x < count; x++)
  851. [objects addObject:_foundMedia[[indexPaths[x] row]]];
  852. for (NSUInteger x = 0; x < count; x++)
  853. [self removeMediaObject:objects[x] updateDatabase:NO];
  854. [self _endEditingWithHardReset:YES];
  855. }
  856. - (void)renameSelection
  857. {
  858. NSArray *indexPaths;
  859. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  860. indexPaths = [self.collectionView indexPathsForSelectedItems];
  861. else
  862. indexPaths = [self.tableView indexPathsForSelectedRows];
  863. if (indexPaths.count < 1) {
  864. [self _endEditingWithHardReset:NO];
  865. return;
  866. }
  867. NSString *itemName;
  868. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  869. itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
  870. else
  871. itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
  872. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", @""), itemName] message:nil cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_RENAME", @"")]];
  873. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  874. [[alert textFieldAtIndex:0] setText:itemName];
  875. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  876. __weak VLCAlertView *weakAlert = alert;
  877. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  878. if (cancelled)
  879. [self _endEditingWithHardReset:NO];
  880. else
  881. [self renameMediaObjectTo:[weakAlert textFieldAtIndex:0].text];
  882. };
  883. [alert show];
  884. }
  885. - (void)renameMediaObjectTo:(NSString*)newName
  886. {
  887. NSArray *indexPaths;
  888. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  889. indexPaths = [self.collectionView indexPathsForSelectedItems];
  890. else
  891. indexPaths = [self.tableView indexPathsForSelectedRows];
  892. if (indexPaths.count < 1)
  893. return;
  894. id mediaObject = _foundMedia[[indexPaths[0] row]];
  895. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]] || [mediaObject isKindOfClass:[MLLabel class]] )
  896. [mediaObject setName:newName];
  897. else
  898. [mediaObject setTitle:newName];
  899. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  900. [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
  901. else
  902. [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
  903. if (indexPaths.count > 1)
  904. [self renameSelection];
  905. else
  906. [self _endEditingWithHardReset:NO];
  907. }
  908. #pragma mark - coin coin
  909. - (void)setLibraryMode:(VLCLibraryMode)mode
  910. {
  911. _libraryMode = mode;
  912. [self updateViewContents];
  913. }
  914. #pragma mark - autorotation
  915. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  916. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  917. /* introduced in iOS 6 */
  918. - (NSUInteger)supportedInterfaceOrientations
  919. {
  920. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  921. return UIInterfaceOrientationMaskAll;
  922. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  923. UIInterfaceOrientationMaskPortrait;
  924. }
  925. /* introduced in iOS 6 */
  926. - (BOOL)shouldAutorotate
  927. {
  928. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  929. }
  930. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  931. {
  932. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  933. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  934. [self.collectionView.collectionViewLayout invalidateLayout];
  935. }
  936. @end