VLCPlaylistViewController.m 46 KB

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