VLCPlaylistViewController.m 46 KB

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