VLCPlaylistViewController.m 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  74. UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewLongTouchGestureAction:)];
  75. [self.view addGestureRecognizer:gestureRecognizer];
  76. }
  77. } else {
  78. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  79. _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_folderLayout];
  80. _collectionView.alwaysBounceVertical = YES;
  81. _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  82. _collectionView.delegate = self;
  83. _collectionView.dataSource = self;
  84. _collectionView.opaque = YES;
  85. _collectionView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  86. self.view = _collectionView;
  87. _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_collectionViewHandleLongPressGesture:)];
  88. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  89. if (SYSTEM_RUNS_IOS7_OR_LATER)
  90. [_collectionView registerNib:[UINib nibWithNibName:@"VLCFuturePlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  91. else
  92. [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  93. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  94. }
  95. _libraryMode = VLCLibraryModeAllFiles;
  96. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  97. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  98. _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
  99. _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  100. }
  101. #pragma mark -
  102. - (void)viewDidLoad
  103. {
  104. [super viewDidLoad];
  105. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  106. _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  107. self.navigationItem.leftBarButtonItem = _menuButton;
  108. if (SYSTEM_RUNS_IOS7_OR_LATER)
  109. self.editButtonItem.tintColor = [UIColor whiteColor];
  110. else {
  111. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  112. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  113. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  114. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  115. }
  116. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  117. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  118. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  119. [_emptyLibraryView.learnMoreButton setTitle:NSLocalizedString(@"BUTTON_LEARN_MORE", @"") forState:UIControlStateNormal];
  120. UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  121. [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)]]];
  122. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  123. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  124. self.navigationController.toolbar.tintColor = [UIColor whiteColor];
  125. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  126. } else
  127. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  128. }
  129. - (void)viewWillAppear:(BOOL)animated
  130. {
  131. [super viewWillAppear:animated];
  132. [self.collectionView.collectionViewLayout invalidateLayout];
  133. [self _displayEmptyLibraryViewIfNeeded];
  134. }
  135. - (void)viewDidAppear:(BOOL)animated
  136. {
  137. [super viewDidAppear:animated];
  138. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  139. if (![[defaults objectForKey:kDisplayedFirstSteps] boolValue]) {
  140. [self.emptyLibraryView performSelector:@selector(learnMore:) withObject:nil afterDelay:1.];
  141. [defaults setObject:[NSNumber numberWithBool:YES] forKey:kDisplayedFirstSteps];
  142. [defaults synchronize];
  143. }
  144. if ([[MLMediaLibrary sharedMediaLibrary] libraryNeedsUpgrade]) {
  145. self.navigationItem.rightBarButtonItem = nil;
  146. self.navigationItem.leftBarButtonItem = nil;
  147. self.title = @"";
  148. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"UPGRADING_LIBRARY", @"");
  149. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = YES;
  150. [self.emptyLibraryView.activityIndicator startAnimating];
  151. self.emptyLibraryView.frame = self.view.bounds;
  152. [self.view addSubview:self.emptyLibraryView];
  153. [[MLMediaLibrary sharedMediaLibrary] setDelegate: self];
  154. [[MLMediaLibrary sharedMediaLibrary] performSelectorInBackground:@selector(upgradeLibrary) withObject:nil];
  155. return;
  156. }
  157. if (_foundMedia.count < 1)
  158. [self updateViewContents];
  159. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  160. }
  161. - (void)viewDidDisappear:(BOOL)animated
  162. {
  163. [super viewDidDisappear:animated];
  164. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  165. }
  166. - (BOOL)canBecomeFirstResponder
  167. {
  168. return YES;
  169. }
  170. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  171. {
  172. if (motion == UIEventSubtypeMotionShake)
  173. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  174. }
  175. - (void)openMediaObject:(NSManagedObject *)mediaObject
  176. {
  177. if ([mediaObject isKindOfClass:[MLAlbum class]]) {
  178. _foundMedia = [NSMutableArray arrayWithArray:[(MLAlbum *)mediaObject sortedTracks]];
  179. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  180. if (_libraryMode == VLCLibraryModeAllFiles)
  181. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  182. else
  183. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  184. self.title = [(MLAlbum*)mediaObject name];
  185. [self reloadViews];
  186. } else if ([mediaObject isKindOfClass:[MLShow class]]) {
  187. _foundMedia = [NSMutableArray arrayWithArray:[(MLShow *)mediaObject sortedEpisodes]];
  188. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  189. if (_libraryMode == VLCLibraryModeAllFiles)
  190. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  191. else
  192. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  193. self.title = [(MLShow*)mediaObject name];
  194. [self reloadViews];
  195. } else if ([mediaObject isKindOfClass:[MLLabel class]]) {
  196. MLLabel *folder = (MLLabel*) mediaObject;
  197. inFolder = YES;
  198. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  199. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  200. if (recognizer == _folderLayout.panGestureRecognizer || recognizer == _folderLayout.longPressGestureRecognizer || recognizer == _longPressGestureRecognizer)
  201. [self.collectionView removeGestureRecognizer:recognizer];
  202. }
  203. _reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
  204. [self.collectionView setCollectionViewLayout:_reorderLayout animated:NO];
  205. _folderLayout = nil;
  206. }
  207. _foundMedia = [NSMutableArray arrayWithArray:[folder sortedFolderItems]];
  208. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  209. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  210. self.title = [folder name];
  211. UIBarButtonItem *removeFromFolder = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(removeFromFolder)];
  212. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  213. toolbarItems[0] = removeFromFolder;
  214. self.toolbarItems = toolbarItems;
  215. [self reloadViews];
  216. return;
  217. } else
  218. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaFromManagedObject:mediaObject];
  219. }
  220. - (void)removeMediaObject:(id)managedObject updateDatabase:(BOOL)updateDB
  221. {
  222. // delete all tracks from an album
  223. if ([managedObject isKindOfClass:[MLAlbum class]]) {
  224. MLAlbum *album = managedObject;
  225. NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
  226. for (MLAlbumTrack *track in iterAlbumTrack) {
  227. NSSet *iterFiles = [NSSet setWithSet:track.files];
  228. for (MLFile *file in iterFiles)
  229. [self _deleteMediaObject:file];
  230. }
  231. // delete all episodes from a show
  232. } else if ([managedObject isKindOfClass:[MLShow class]]) {
  233. MLShow *show = managedObject;
  234. NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
  235. for (MLShowEpisode *episode in iterShowEpisodes) {
  236. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  237. for (MLFile *file in iterFiles)
  238. [self _deleteMediaObject:file];
  239. }
  240. // delete all files from an episode
  241. } else if ([managedObject isKindOfClass:[MLShowEpisode class]]) {
  242. MLShowEpisode *episode = managedObject;
  243. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  244. for (MLFile *file in iterFiles)
  245. [self _deleteMediaObject:file];
  246. // delete all files from a track
  247. } else if ([managedObject isKindOfClass:[MLAlbumTrack class]]) {
  248. MLAlbumTrack *track = managedObject;
  249. NSSet *iterFiles = [NSSet setWithSet:track.files];
  250. for (MLFile *file in iterFiles)
  251. [self _deleteMediaObject:file];
  252. } else if ([managedObject isKindOfClass:[MLLabel class]]) {
  253. MLLabel *folder = managedObject;
  254. NSSet *iterFiles = [NSSet setWithSet:folder.files];
  255. [folder removeFiles:folder.files];
  256. for (MLFile *file in iterFiles)
  257. [self _deleteMediaObject:file];
  258. [[MLMediaLibrary sharedMediaLibrary] removeObject:folder];
  259. }
  260. else
  261. [self _deleteMediaObject:managedObject];
  262. if (updateDB) {
  263. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  264. [self updateViewContents];
  265. }
  266. }
  267. - (void)_deleteMediaObject:(MLFile *)mediaObject
  268. {
  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 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  412. {
  413. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  414. cell.multipleSelectionBackgroundView.backgroundColor = cell.backgroundColor;
  415. }
  416. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  417. {
  418. return YES;
  419. }
  420. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  421. {
  422. if (editingStyle == UITableViewCellEditingStyleDelete)
  423. [self removeMediaObject: _foundMedia[indexPath.row] updateDatabase:YES];
  424. }
  425. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  426. {
  427. if (tableView.isEditing) {
  428. if (_libraryMode == VLCLibraryModeCreateFolder) {
  429. _folderObject = _foundMedia[indexPath.row];
  430. _libraryMode = _previousLibraryMode;
  431. [self updateViewContents];
  432. [self createFolderWithName:nil];
  433. }
  434. return;
  435. }
  436. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  437. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  438. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  439. VLCMediaList *list;
  440. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  441. NSUInteger count = tracks.count;
  442. list = [[VLCMediaList alloc] init];
  443. MLFile *file;
  444. VLCMedia *media;
  445. for (NSInteger x = count - 1; x > -1; x--) {
  446. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  447. media = [VLCMedia mediaWithURL: [NSURL URLWithString:file.url]];
  448. [media parse];
  449. [list addMedia:media];
  450. }
  451. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  452. } else
  453. [self openMediaObject:selectedObject];
  454. }
  455. #pragma mark - table view gestures
  456. - (void)tableViewLongTouchGestureAction:(UIGestureRecognizer *)recognizer
  457. {
  458. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  459. UITableViewCell *cell = [(UITableView *)self.view cellForRowAtIndexPath:path];
  460. CGRect frame = cell.frame;
  461. if (frame.size.height > 90.)
  462. frame.size.height = 90.;
  463. else if (recognizer.state == UIGestureRecognizerStateBegan)
  464. frame.size.height = 180;
  465. void (^animationBlock)() = ^() {
  466. cell.frame = frame;
  467. };
  468. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  469. cell.frame = frame;
  470. [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
  471. };
  472. NSTimeInterval animationDuration = .2;
  473. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  474. }
  475. - (void)swipeRightGestureAction:(UIGestureRecognizer *)recognizer
  476. {
  477. if ([[self.editButtonItem title] isEqualToString:NSLocalizedString(@"BUTTON_CANCEL",@"")])
  478. [self setEditing:NO animated:YES];
  479. else {
  480. [self setEditing:YES animated:YES];
  481. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  482. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:path.section]
  483. animated:YES
  484. scrollPosition:UITableViewScrollPositionNone];
  485. }
  486. }
  487. #pragma mark - Collection View
  488. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  489. {
  490. return _foundMedia.count;
  491. }
  492. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  493. {
  494. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  495. cell.mediaObject = _foundMedia[indexPath.row];
  496. cell.collectionView = _collectionView;
  497. [cell setEditing:self.editing animated:NO];
  498. return cell;
  499. }
  500. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  501. {
  502. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  503. if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
  504. return CGSizeMake(341., 190.);
  505. else
  506. return CGSizeMake(384., 216.);
  507. }
  508. return CGSizeMake(298.0, 220.0);
  509. }
  510. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  511. {
  512. if (SYSTEM_RUNS_IOS7_OR_LATER)
  513. return UIEdgeInsetsMake(0., 0., 0., 0.);
  514. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  515. }
  516. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  517. {
  518. if (SYSTEM_RUNS_IOS7_OR_LATER)
  519. return 0.;
  520. return 10.0;
  521. }
  522. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  523. {
  524. if (SYSTEM_RUNS_IOS7_OR_LATER)
  525. return 0.;
  526. return 10.0;
  527. }
  528. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  529. {
  530. if (self.editing) {
  531. if (_libraryMode == VLCLibraryModeCreateFolder) {
  532. _folderObject = _foundMedia[indexPath.item];
  533. _libraryMode = _previousLibraryMode;
  534. [self updateViewContents];
  535. [self createFolderWithName:nil];
  536. }
  537. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  538. return;
  539. }
  540. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  541. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  542. VLCMediaList *list;
  543. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  544. NSUInteger count = tracks.count;
  545. list = [[VLCMediaList alloc] init];
  546. MLFile *file;
  547. for (NSInteger x = count - 1; x > -1; x--) {
  548. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  549. [list addMedia:[VLCMedia mediaWithURL: [NSURL URLWithString:file.url]]];
  550. }
  551. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  552. } else if ([selectedObject isKindOfClass:[MLFile class]] && [((MLFile *)selectedObject).labels count] > 0) {
  553. VLCMediaList *list;
  554. MLLabel *folder = [((MLFile *)selectedObject).labels anyObject];
  555. NSArray *folderTracks = [folder.files allObjects];
  556. NSUInteger count = folderTracks.count;
  557. list = [[VLCMediaList alloc] init];
  558. MLFile *file;
  559. for (NSInteger x = count - 1; x > -1; x--) {
  560. file = (MLFile *)folderTracks[x];
  561. [list addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:file.url]]];
  562. }
  563. [(VLCAppDelegate *)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[folderTracks indexOfObject:selectedObject]];
  564. } else
  565. [self openMediaObject:selectedObject];
  566. }
  567. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  568. {
  569. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  570. }
  571. - (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
  572. {
  573. MLFile *mediaObject = (MLFile *)_foundMedia[indexPath.item];
  574. mediaObject.labels = nil;
  575. mediaObject.folderTrackNumber = nil;
  576. [self backToAllItems:nil];
  577. }
  578. - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
  579. {
  580. MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
  581. [_foundMedia removeObjectAtIndex:fromIndexPath.item];
  582. [_foundMedia insertObject:object atIndex:toIndexPath.item];
  583. object.folderTrackNumber = @(toIndexPath.item - 1);
  584. object = [_foundMedia objectAtIndex:fromIndexPath.item];
  585. object.folderTrackNumber = @(fromIndexPath.item - 1);
  586. }
  587. - (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
  588. {
  589. BOOL validFileTypeAtFolderPath = [_foundMedia[folderPath.item] isKindOfClass:[MLFile class]] || [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
  590. if (!(validFileTypeAtFolderPath && [_foundMedia[itemPath.item] isKindOfClass:[MLFile class]])) {
  591. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_INVALID_TYPE_TITLE", @"") message:NSLocalizedString(@"FOLDER_INVALID_TYPE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  592. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  593. [self updateViewContents];
  594. };
  595. [alert show];
  596. return;
  597. }
  598. BOOL isFolder = [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
  599. if (isFolder){
  600. MLLabel *folder = _foundMedia[folderPath.item];
  601. MLFile *file = _foundMedia[itemPath.item];
  602. [file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
  603. file.folderTrackNumber = @([folder.files count] - 1);
  604. [_foundMedia removeObjectAtIndex:itemPath.item];
  605. [self updateViewContents];
  606. } else {
  607. _folderObject = _foundMedia[folderPath.item];
  608. _indexPaths = [NSMutableArray arrayWithArray:@[itemPath]];
  609. [self showCreateFolderAlert];
  610. }
  611. }
  612. #pragma mark - Folder implementation
  613. - (void)showCreateFolderAlert
  614. {
  615. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_CHOOSE_NAME_TITLE", @"") message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", @"") cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", @"")]];
  616. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  617. [[alert textFieldAtIndex:0] setText:NSLocalizedString(@"FOLDER_NAME_PLACEHOLDER", @"")];
  618. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  619. __weak VLCAlertView *weakAlert = alert;
  620. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  621. if (cancelled)
  622. [self updateViewContents];
  623. else
  624. [self createFolderWithName:[weakAlert textFieldAtIndex:0].text];
  625. };
  626. [alert show];
  627. }
  628. - (void)createFolder
  629. {
  630. if (_libraryMode == VLCLibraryModeCreateFolder) {
  631. _libraryMode = _previousLibraryMode;
  632. [self updateViewContents];
  633. [self showCreateFolderAlert];
  634. return;
  635. }
  636. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  637. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  638. else
  639. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  640. for (NSIndexPath *path in _indexPaths) {
  641. id mediaObject;
  642. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  643. mediaObject = _foundMedia[path.item];
  644. else
  645. mediaObject = _foundMedia[path.row];
  646. if ([mediaObject isKindOfClass:[MLLabel class]])
  647. [_indexPaths removeObject:mediaObject];
  648. }
  649. if ([_indexPaths count] != 0) {
  650. NSArray *folder = [MLLabel allLabels];
  651. //if we already have folders display them
  652. if ([folder count] > 0) {
  653. _foundMedia = [NSMutableArray arrayWithArray:folder];
  654. self.title = NSLocalizedString(@"SELECT_FOLDER", @"");
  655. _previousLibraryMode = _libraryMode;
  656. _libraryMode = VLCLibraryModeCreateFolder;
  657. [self reloadViews];
  658. return;
  659. }
  660. }
  661. //no selected items or no existing folder ask for foldername
  662. [self showCreateFolderAlert];
  663. }
  664. - (void)removeFromFolder
  665. {
  666. BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
  667. if (isPad)
  668. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  669. else
  670. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  671. [_indexPaths sortUsingSelector:@selector(compare:)];
  672. for (int i = [_indexPaths count] - 1; i >= 0; i--) {
  673. NSIndexPath *path = _indexPaths[i];
  674. MLFile *file = (MLFile *)_foundMedia[isPad ? path.item : path.row];
  675. MLLabel *folder = [file.labels anyObject];
  676. file.labels = nil;
  677. file.folderTrackNumber = nil;
  678. [_foundMedia removeObject:file];
  679. if ([folder.files count] == 0) {
  680. [self removeMediaObject:folder updateDatabase:YES];
  681. [self backToAllItems:nil];
  682. }
  683. }
  684. [self reloadViews];
  685. }
  686. - (void)createFolderWithName:(NSString *)folderName
  687. {
  688. NSArray *labels = [MLLabel allLabels];
  689. for (MLLabel *label in labels){
  690. if ([label.name isEqualToString:folderName]) {
  691. _folderObject = nil;
  692. _indexPaths = nil;
  693. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_TITLE", @"") message:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  694. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  695. [self updateViewContents];
  696. };
  697. [alert show];
  698. return;
  699. }
  700. }
  701. if (_folderObject != nil) {
  702. int folderIndex = [_foundMedia indexOfObject:_folderObject];
  703. //item got dragged onto item
  704. if ([_foundMedia[folderIndex] isKindOfClass:[MLFile class]]) {
  705. MLFile *file = _foundMedia[folderIndex];
  706. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  707. label.name = folderName;
  708. file.labels = [NSSet setWithObjects:label,nil];
  709. NSNumber *folderTrackNumber = [NSNumber numberWithInt:[label files].count - 1];
  710. file.folderTrackNumber = folderTrackNumber;
  711. [_foundMedia removeObjectAtIndex:folderIndex];
  712. [_foundMedia insertObject:label atIndex:folderIndex];
  713. MLFile *itemFile = _foundMedia[((NSIndexPath *)_indexPaths[0]).item];
  714. itemFile.labels = file.labels;
  715. [_foundMedia removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
  716. itemFile.folderTrackNumber = @([label files].count - 1);
  717. } else {
  718. //item got dragged onto folder or items should be added to folder
  719. MLLabel *label = _foundMedia[folderIndex];
  720. [_indexPaths sortUsingSelector:@selector(compare:)];
  721. for (int i = [_indexPaths count] - 1; i >= 0; i--) {
  722. NSIndexPath *path = _indexPaths[i];
  723. MLFile *file = _foundMedia[path.row];
  724. file.labels = [NSSet setWithObjects:label, nil];
  725. [_foundMedia removeObjectAtIndex:path.row];
  726. file.folderTrackNumber = @([label files].count - 1);
  727. }
  728. }
  729. _folderObject = nil;
  730. } else {
  731. //create new folder
  732. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  733. label.name = folderName;
  734. //if items were selected
  735. if ([_indexPaths count] != 0) {
  736. [_indexPaths sortUsingSelector:@selector(compare:)];
  737. for (int i = [_indexPaths count] - 1; i >= 0; i--) {
  738. NSIndexPath *path = _indexPaths[i];
  739. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  740. MLFile *file = _foundMedia[path.item];
  741. file.labels = [NSSet setWithObjects:label, nil];
  742. file.folderTrackNumber = @([label files].count - 1);
  743. [_foundMedia removeObjectAtIndex:path.item];
  744. } else {
  745. MLFile *file = _foundMedia[path.row];
  746. file.labels = [NSSet setWithObjects:label, nil];
  747. file.folderTrackNumber = @([label files].count - 1);
  748. [_foundMedia removeObjectAtIndex:path.row];
  749. }
  750. }
  751. }
  752. }
  753. _indexPaths = nil;
  754. [self setEditing:NO];
  755. [self updateViewContents];
  756. }
  757. - (void)_collectionViewHandleLongPressGesture:(UIGestureRecognizer *) sender
  758. {
  759. [self setEditing:YES animated:YES];
  760. }
  761. #pragma mark - UI implementation
  762. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  763. {
  764. [super setEditing:editing animated:animated];
  765. UIBarButtonItem *editButton = self.editButtonItem;
  766. NSString *editImage = editing? @"doneButton": @"button";
  767. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  768. if (SYSTEM_RUNS_IOS7_OR_LATER)
  769. editButton.tintColor = [UIColor whiteColor];
  770. else {
  771. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  772. barMetrics:UIBarMetricsDefault];
  773. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  774. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  775. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  776. }
  777. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  778. NSArray *visibleCells = self.collectionView.visibleCells;
  779. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  780. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  781. [aCell setEditing:editing animated:animated];
  782. }];
  783. self.collectionView.allowsMultipleSelection = editing;
  784. /* UIKit doesn't clear the selection automagically if we leave the editing mode
  785. * so we need to do so manually */
  786. if (!editing) {
  787. [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
  788. NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
  789. NSUInteger count = selectedItems.count;
  790. for (NSUInteger x = 0; x < count; x++)
  791. [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
  792. } else
  793. [self.collectionView removeGestureRecognizer:_longPressGestureRecognizer];
  794. } else {
  795. self.tableView.allowsMultipleSelectionDuringEditing = editing;
  796. [self.tableView setEditing:editing animated:YES];
  797. [self.editButtonItem setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL",@"") : NSLocalizedString(@"BUTTON_EDIT", @"")];
  798. }
  799. if (_libraryMode == VLCLibraryModeCreateFolder) {
  800. _libraryMode = _previousLibraryMode;
  801. _indexPaths = nil;
  802. [self updateViewContents];
  803. }
  804. self.navigationController.toolbarHidden = !editing;
  805. }
  806. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  807. {
  808. return UITableViewCellEditingStyleDelete;
  809. }
  810. - (IBAction)leftButtonAction:(id)sender
  811. {
  812. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  813. if (self.isEditing)
  814. [self setEditing:NO animated:YES];
  815. }
  816. - (IBAction)backToAllItems:(id)sender
  817. {
  818. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  819. //for some reason the Gesturerecognizer block themselves if not removed manually
  820. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  821. if (recognizer == _reorderLayout.panGestureRecognizer || recognizer == _reorderLayout.longPressGestureRecognizer)
  822. [self.collectionView removeGestureRecognizer:recognizer];
  823. }
  824. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  825. [self.collectionView setCollectionViewLayout:_folderLayout animated:NO];
  826. _reorderLayout = nil;
  827. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  828. }
  829. inFolder = NO;
  830. UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  831. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  832. toolbarItems[0] = createFolderItem;
  833. self.toolbarItems = toolbarItems;
  834. [self setLibraryMode:_libraryMode];
  835. [self updateViewContents];
  836. }
  837. - (void)_endEditingWithHardReset:(BOOL)hardReset
  838. {
  839. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  840. if (hardReset)
  841. [self updateViewContents];
  842. else
  843. [self reloadViews];
  844. [self setEditing:NO animated:YES];
  845. }
  846. - (void)deleteSelection
  847. {
  848. NSArray *indexPaths;
  849. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  850. indexPaths = [self.collectionView indexPathsForSelectedItems];
  851. else
  852. indexPaths = [self.tableView indexPathsForSelectedRows];
  853. NSUInteger count = indexPaths.count;
  854. NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:count];
  855. for (NSUInteger x = 0; x < count; x++)
  856. [objects addObject:_foundMedia[[indexPaths[x] row]]];
  857. for (NSUInteger x = 0; x < count; x++)
  858. [self removeMediaObject:objects[x] updateDatabase:NO];
  859. [self _endEditingWithHardReset:YES];
  860. }
  861. - (void)renameSelection
  862. {
  863. NSArray *indexPaths;
  864. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  865. indexPaths = [self.collectionView indexPathsForSelectedItems];
  866. else
  867. indexPaths = [self.tableView indexPathsForSelectedRows];
  868. if (indexPaths.count < 1) {
  869. [self _endEditingWithHardReset:NO];
  870. return;
  871. }
  872. NSString *itemName;
  873. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  874. itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
  875. else
  876. itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
  877. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", @""), itemName] message:nil cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_RENAME", @"")]];
  878. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  879. [[alert textFieldAtIndex:0] setText:itemName];
  880. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  881. __weak VLCAlertView *weakAlert = alert;
  882. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  883. if (cancelled)
  884. [self _endEditingWithHardReset:NO];
  885. else
  886. [self renameMediaObjectTo:[weakAlert textFieldAtIndex:0].text];
  887. };
  888. [alert show];
  889. }
  890. - (void)renameMediaObjectTo:(NSString*)newName
  891. {
  892. NSArray *indexPaths;
  893. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  894. indexPaths = [self.collectionView indexPathsForSelectedItems];
  895. else
  896. indexPaths = [self.tableView indexPathsForSelectedRows];
  897. if (indexPaths.count < 1)
  898. return;
  899. id mediaObject = _foundMedia[[indexPaths[0] row]];
  900. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]] || [mediaObject isKindOfClass:[MLLabel class]] )
  901. [mediaObject setName:newName];
  902. else
  903. [mediaObject setTitle:newName];
  904. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  905. [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
  906. else
  907. [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
  908. if (indexPaths.count > 1)
  909. [self renameSelection];
  910. else
  911. [self _endEditingWithHardReset:NO];
  912. }
  913. #pragma mark - coin coin
  914. - (void)setLibraryMode:(VLCLibraryMode)mode
  915. {
  916. _libraryMode = mode;
  917. [self updateViewContents];
  918. }
  919. #pragma mark - autorotation
  920. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  921. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  922. /* introduced in iOS 6 */
  923. - (NSUInteger)supportedInterfaceOrientations
  924. {
  925. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  926. return UIInterfaceOrientationMaskAll;
  927. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  928. UIInterfaceOrientationMaskPortrait;
  929. }
  930. /* introduced in iOS 6 */
  931. - (BOOL)shouldAutorotate
  932. {
  933. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  934. }
  935. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  936. {
  937. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  938. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  939. [self.collectionView.collectionViewLayout invalidateLayout];
  940. }
  941. @end