VLCPlaylistViewController.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 createFolderWithName:nil];
  525. }
  526. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  527. return;
  528. }
  529. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  530. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  531. VLCMediaList *list;
  532. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  533. NSUInteger count = tracks.count;
  534. list = [[VLCMediaList alloc] init];
  535. MLFile *file;
  536. for (NSInteger x = count - 1; x > -1; x--) {
  537. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  538. [list addMedia:[VLCMedia mediaWithURL: [NSURL URLWithString:file.url]]];
  539. }
  540. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  541. } else if ([selectedObject isKindOfClass:[MLFile class]] && [((MLFile *)selectedObject).labels count] > 0) {
  542. VLCMediaList *list;
  543. MLLabel *folder = [((MLFile *)selectedObject).labels anyObject];
  544. NSArray *folderTracks = [folder.files allObjects];
  545. NSUInteger count = folderTracks.count;
  546. list = [[VLCMediaList alloc] init];
  547. MLFile *file;
  548. for (NSInteger x = count - 1; x > -1; x--) {
  549. file = (MLFile *)folderTracks[x];
  550. [list addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:file.url]]];
  551. }
  552. [(VLCAppDelegate *)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[folderTracks indexOfObject:selectedObject]];
  553. } else
  554. [self openMediaObject:selectedObject];
  555. }
  556. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  557. {
  558. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  559. }
  560. - (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
  561. {
  562. MLFile *mediaObject = (MLFile *)_foundMedia[indexPath.item];
  563. mediaObject.labels = nil;
  564. mediaObject.folderTrackNumber = nil;
  565. [self backToAllItems:nil];
  566. }
  567. - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
  568. {
  569. MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
  570. [_foundMedia removeObjectAtIndex:fromIndexPath.item];
  571. [_foundMedia insertObject:object atIndex:toIndexPath.item];
  572. object.folderTrackNumber = @(toIndexPath.item - 1);
  573. object = [_foundMedia objectAtIndex:fromIndexPath.item];
  574. object.folderTrackNumber = @(fromIndexPath.item - 1);
  575. }
  576. - (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
  577. {
  578. BOOL validFileTypeAtFolderPath = ([_foundMedia[folderPath.item] isKindOfClass:[MLFile class]] || [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]]) && [_foundMedia[itemPath.item] isKindOfClass:[MLFile class]];
  579. if (!validFileTypeAtFolderPath) {
  580. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_INVALID_TYPE_TITLE", @"") message:NSLocalizedString(@"FOLDER_INVALID_TYPE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  581. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  582. [self updateViewContents];
  583. };
  584. [alert show];
  585. return;
  586. }
  587. BOOL isFolder = [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
  588. if (isFolder){
  589. MLLabel *folder = _foundMedia[folderPath.item];
  590. MLFile *file = _foundMedia[itemPath.item];
  591. [file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
  592. file.folderTrackNumber = @([folder.files count] - 1);
  593. [_foundMedia removeObjectAtIndex:itemPath.item];
  594. [self updateViewContents];
  595. } else {
  596. _folderObject = _foundMedia[folderPath.item];
  597. _indexPaths = [NSMutableArray arrayWithArray:@[itemPath]];
  598. [self showCreateFolderAlert];
  599. }
  600. }
  601. #pragma mark - Folder implementation
  602. - (void)showCreateFolderAlert
  603. {
  604. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_CHOOSE_NAME_TITLE", @"") message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", @"") cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", @"")]];
  605. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  606. [[alert textFieldAtIndex:0] setText:NSLocalizedString(@"FOLDER_NAME_PLACEHOLDER", @"")];
  607. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  608. __weak VLCAlertView *weakAlert = alert;
  609. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  610. if (cancelled)
  611. [self updateViewContents];
  612. else
  613. [self createFolderWithName:[weakAlert textFieldAtIndex:0].text];
  614. };
  615. [alert show];
  616. }
  617. - (void)createFolder
  618. {
  619. if (_libraryMode == VLCLibraryModeCreateFolder) {
  620. _libraryMode = _previousLibraryMode;
  621. [self updateViewContents];
  622. [self showCreateFolderAlert];
  623. return;
  624. }
  625. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  626. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  627. else
  628. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  629. for (NSIndexPath *path in _indexPaths) {
  630. id mediaObject;
  631. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  632. mediaObject = _foundMedia[path.item];
  633. else
  634. mediaObject = _foundMedia[path.row];
  635. if ([mediaObject isKindOfClass:[MLLabel class]])
  636. [_indexPaths removeObject:path];
  637. }
  638. if ([_indexPaths count] != 0) {
  639. NSArray *folder = [MLLabel allLabels];
  640. //if we already have folders display them
  641. if ([folder count] > 0) {
  642. _foundMedia = [NSMutableArray arrayWithArray:folder];
  643. self.title = NSLocalizedString(@"SELECT_FOLDER", @"");
  644. _previousLibraryMode = _libraryMode;
  645. _libraryMode = VLCLibraryModeCreateFolder;
  646. [self reloadViews];
  647. return;
  648. }
  649. }
  650. //no selected items or no existing folder ask for foldername
  651. [self showCreateFolderAlert];
  652. }
  653. - (void)removeFromFolder
  654. {
  655. BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
  656. if (isPad)
  657. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  658. else
  659. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  660. [_indexPaths sortUsingSelector:@selector(compare:)];
  661. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  662. NSIndexPath *path = _indexPaths[i];
  663. MLFile *file = (MLFile *)_foundMedia[isPad ? path.item : path.row];
  664. MLLabel *folder = [file.labels anyObject];
  665. file.labels = nil;
  666. file.folderTrackNumber = nil;
  667. [_foundMedia removeObject:file];
  668. if ([folder.files count] == 0) {
  669. [self removeMediaObject:folder updateDatabase:YES];
  670. [self backToAllItems:nil];
  671. }
  672. }
  673. [self reloadViews];
  674. }
  675. - (void)createFolderWithName:(NSString *)folderName
  676. {
  677. NSArray *labels = [MLLabel allLabels];
  678. for (MLLabel *label in labels){
  679. if ([label.name isEqualToString:folderName]) {
  680. _folderObject = nil;
  681. _indexPaths = nil;
  682. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_TITLE", @"") message:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  683. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  684. [self updateViewContents];
  685. };
  686. [alert show];
  687. return;
  688. }
  689. }
  690. if (_folderObject != nil) {
  691. NSUInteger folderIndex = [_foundMedia indexOfObject:_folderObject];
  692. //item got dragged onto item
  693. if ([_foundMedia[folderIndex] isKindOfClass:[MLFile class]]) {
  694. MLFile *file = _foundMedia[folderIndex];
  695. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  696. label.name = folderName;
  697. file.labels = [NSSet setWithObjects:label,nil];
  698. NSNumber *folderTrackNumber = [NSNumber numberWithInt:(int)[label files].count - 1];
  699. file.folderTrackNumber = folderTrackNumber;
  700. [_foundMedia removeObjectAtIndex:folderIndex];
  701. [_foundMedia insertObject:label atIndex:folderIndex];
  702. MLFile *itemFile = _foundMedia[((NSIndexPath *)_indexPaths[0]).item];
  703. itemFile.labels = file.labels;
  704. [_foundMedia removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
  705. itemFile.folderTrackNumber = @([label files].count - 1);
  706. } else {
  707. //item got dragged onto folder or items should be added to folder
  708. MLLabel *label = _foundMedia[folderIndex];
  709. [_indexPaths sortUsingSelector:@selector(compare:)];
  710. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  711. NSIndexPath *path = _indexPaths[i];
  712. if (![_foundMedia[path.row] isKindOfClass:[MLFile class]])
  713. continue;
  714. MLFile *file = _foundMedia[path.row];
  715. file.labels = [NSSet setWithObjects:label, nil];
  716. [_foundMedia removeObjectAtIndex:path.row];
  717. file.folderTrackNumber = @([label files].count - 1);
  718. }
  719. }
  720. _folderObject = nil;
  721. } else {
  722. //create new folder
  723. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  724. label.name = folderName;
  725. //if items were selected
  726. if ([_indexPaths count] != 0) {
  727. [_indexPaths sortUsingSelector:@selector(compare:)];
  728. for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
  729. NSIndexPath *path = _indexPaths[i];
  730. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  731. MLFile *file = _foundMedia[path.item];
  732. file.labels = [NSSet setWithObjects:label, nil];
  733. file.folderTrackNumber = @([label files].count - 1);
  734. [_foundMedia removeObjectAtIndex:path.item];
  735. } else {
  736. MLFile *file = _foundMedia[path.row];
  737. file.labels = [NSSet setWithObjects:label, nil];
  738. file.folderTrackNumber = @([label files].count - 1);
  739. [_foundMedia removeObjectAtIndex:path.row];
  740. }
  741. }
  742. }
  743. }
  744. _indexPaths = nil;
  745. [self setEditing:NO];
  746. [self updateViewContents];
  747. }
  748. - (void)_collectionViewHandleLongPressGesture:(UIGestureRecognizer *) sender
  749. {
  750. [self setEditing:YES animated:YES];
  751. }
  752. #pragma mark - UI implementation
  753. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  754. {
  755. [super setEditing:editing animated:animated];
  756. UIBarButtonItem *editButton = self.editButtonItem;
  757. NSString *editImage = editing? @"doneButton": @"button";
  758. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  759. if (SYSTEM_RUNS_IOS7_OR_LATER)
  760. editButton.tintColor = [UIColor whiteColor];
  761. else {
  762. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  763. barMetrics:UIBarMetricsDefault];
  764. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  765. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  766. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  767. }
  768. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  769. NSArray *visibleCells = self.collectionView.visibleCells;
  770. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  771. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  772. [aCell setEditing:editing animated:animated];
  773. }];
  774. self.collectionView.allowsMultipleSelection = editing;
  775. /* UIKit doesn't clear the selection automagically if we leave the editing mode
  776. * so we need to do so manually */
  777. if (!editing) {
  778. [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
  779. NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
  780. NSUInteger count = selectedItems.count;
  781. for (NSUInteger x = 0; x < count; x++)
  782. [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
  783. } else
  784. [self.collectionView removeGestureRecognizer:_longPressGestureRecognizer];
  785. } else {
  786. self.tableView.allowsMultipleSelectionDuringEditing = editing;
  787. [self.tableView setEditing:editing animated:YES];
  788. [self.editButtonItem setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL",@"") : NSLocalizedString(@"BUTTON_EDIT", @"")];
  789. }
  790. if (_libraryMode == VLCLibraryModeCreateFolder) {
  791. _libraryMode = _previousLibraryMode;
  792. _indexPaths = nil;
  793. [self updateViewContents];
  794. }
  795. self.navigationController.toolbarHidden = !editing;
  796. }
  797. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  798. {
  799. return UITableViewCellEditingStyleDelete;
  800. }
  801. - (IBAction)leftButtonAction:(id)sender
  802. {
  803. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  804. if (self.isEditing)
  805. [self setEditing:NO animated:YES];
  806. }
  807. - (IBAction)backToAllItems:(id)sender
  808. {
  809. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  810. //for some reason the Gesturerecognizer block themselves if not removed manually
  811. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  812. if (recognizer == _reorderLayout.panGestureRecognizer || recognizer == _reorderLayout.longPressGestureRecognizer)
  813. [self.collectionView removeGestureRecognizer:recognizer];
  814. }
  815. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  816. [self.collectionView setCollectionViewLayout:_folderLayout animated:NO];
  817. _reorderLayout = nil;
  818. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  819. }
  820. inFolder = NO;
  821. UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  822. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  823. toolbarItems[0] = createFolderItem;
  824. self.toolbarItems = toolbarItems;
  825. [self setLibraryMode:_libraryMode];
  826. [self updateViewContents];
  827. }
  828. - (void)_endEditingWithHardReset:(BOOL)hardReset
  829. {
  830. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  831. if (hardReset)
  832. [self updateViewContents];
  833. else
  834. [self reloadViews];
  835. [self setEditing:NO animated:YES];
  836. }
  837. - (void)deleteSelection
  838. {
  839. NSArray *indexPaths;
  840. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  841. indexPaths = [self.collectionView indexPathsForSelectedItems];
  842. else
  843. indexPaths = [self.tableView indexPathsForSelectedRows];
  844. NSUInteger count = indexPaths.count;
  845. NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:count];
  846. for (NSUInteger x = 0; x < count; x++)
  847. [objects addObject:_foundMedia[[indexPaths[x] row]]];
  848. for (NSUInteger x = 0; x < count; x++)
  849. [self removeMediaObject:objects[x] updateDatabase:NO];
  850. [self _endEditingWithHardReset:YES];
  851. }
  852. - (void)renameSelection
  853. {
  854. NSArray *indexPaths;
  855. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  856. indexPaths = [self.collectionView indexPathsForSelectedItems];
  857. else
  858. indexPaths = [self.tableView indexPathsForSelectedRows];
  859. if (indexPaths.count < 1) {
  860. [self _endEditingWithHardReset:NO];
  861. return;
  862. }
  863. NSString *itemName;
  864. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  865. itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
  866. else
  867. itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
  868. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", @""), itemName] message:nil cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_RENAME", @"")]];
  869. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  870. [[alert textFieldAtIndex:0] setText:itemName];
  871. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  872. __weak VLCAlertView *weakAlert = alert;
  873. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  874. if (cancelled)
  875. [self _endEditingWithHardReset:NO];
  876. else
  877. [self renameMediaObjectTo:[weakAlert textFieldAtIndex:0].text];
  878. };
  879. [alert show];
  880. }
  881. - (void)renameMediaObjectTo:(NSString*)newName
  882. {
  883. NSArray *indexPaths;
  884. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  885. indexPaths = [self.collectionView indexPathsForSelectedItems];
  886. else
  887. indexPaths = [self.tableView indexPathsForSelectedRows];
  888. if (indexPaths.count < 1)
  889. return;
  890. id mediaObject = _foundMedia[[indexPaths[0] row]];
  891. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]] || [mediaObject isKindOfClass:[MLLabel class]] )
  892. [mediaObject setName:newName];
  893. else
  894. [mediaObject setTitle:newName];
  895. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  896. [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
  897. else
  898. [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
  899. if (indexPaths.count > 1)
  900. [self renameSelection];
  901. else
  902. [self _endEditingWithHardReset:NO];
  903. }
  904. #pragma mark - coin coin
  905. - (void)setLibraryMode:(VLCLibraryMode)mode
  906. {
  907. _libraryMode = mode;
  908. [self updateViewContents];
  909. }
  910. #pragma mark - autorotation
  911. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  912. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  913. /* introduced in iOS 6 */
  914. - (NSUInteger)supportedInterfaceOrientations
  915. {
  916. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  917. return UIInterfaceOrientationMaskAll;
  918. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  919. UIInterfaceOrientationMaskPortrait;
  920. }
  921. /* introduced in iOS 6 */
  922. - (BOOL)shouldAutorotate
  923. {
  924. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  925. }
  926. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  927. {
  928. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  929. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  930. [self.collectionView.collectionViewLayout invalidateLayout];
  931. }
  932. @end