VLCPlaylistViewController.m 45 KB

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