VLCPlaylistViewController.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  412. {
  413. MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
  414. [_foundMedia removeObjectAtIndex:fromIndexPath.item];
  415. [_foundMedia insertObject:object atIndex:toIndexPath.item];
  416. object.folderTrackNumber = @(toIndexPath.item - 1);
  417. object = [_foundMedia objectAtIndex:fromIndexPath.item];
  418. object.folderTrackNumber = @(fromIndexPath.item - 1);
  419. }
  420. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  421. {
  422. return inFolder;
  423. }
  424. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  425. {
  426. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  427. cell.multipleSelectionBackgroundView.backgroundColor = cell.backgroundColor;
  428. }
  429. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  430. {
  431. return YES;
  432. }
  433. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  434. {
  435. if (editingStyle == UITableViewCellEditingStyleDelete)
  436. [self removeMediaObject: _foundMedia[indexPath.row] updateDatabase:YES];
  437. }
  438. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  439. {
  440. if (tableView.isEditing) {
  441. if (_libraryMode == VLCLibraryModeCreateFolder) {
  442. _folderObject = _foundMedia[indexPath.row];
  443. _libraryMode = _previousLibraryMode;
  444. [self updateViewContents];
  445. [self createFolderWithName:nil];
  446. }
  447. return;
  448. }
  449. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  450. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  451. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  452. VLCMediaList *list;
  453. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  454. NSUInteger count = tracks.count;
  455. list = [[VLCMediaList alloc] init];
  456. MLFile *file;
  457. VLCMedia *media;
  458. for (NSInteger x = count - 1; x > -1; x--) {
  459. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  460. media = [VLCMedia mediaWithURL: [NSURL URLWithString:file.url]];
  461. [media parse];
  462. [list addMedia:media];
  463. }
  464. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  465. } else
  466. [self openMediaObject:selectedObject];
  467. }
  468. #pragma mark - table view gestures
  469. - (void)tableViewLongTouchGestureAction:(UIGestureRecognizer *)recognizer
  470. {
  471. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  472. UITableViewCell *cell = [(UITableView *)self.view cellForRowAtIndexPath:path];
  473. CGRect frame = cell.frame;
  474. if (frame.size.height > 90.)
  475. frame.size.height = 90.;
  476. else if (recognizer.state == UIGestureRecognizerStateBegan)
  477. frame.size.height = 180;
  478. void (^animationBlock)() = ^() {
  479. cell.frame = frame;
  480. };
  481. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  482. cell.frame = frame;
  483. [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
  484. };
  485. NSTimeInterval animationDuration = .2;
  486. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  487. }
  488. - (void)swipeRightGestureAction:(UIGestureRecognizer *)recognizer
  489. {
  490. if ([[self.editButtonItem title] isEqualToString:NSLocalizedString(@"BUTTON_CANCEL",@"")])
  491. [self setEditing:NO animated:YES];
  492. else {
  493. [self setEditing:YES animated:YES];
  494. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  495. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:path.section]
  496. animated:YES
  497. scrollPosition:UITableViewScrollPositionNone];
  498. }
  499. }
  500. #pragma mark - Collection View
  501. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  502. {
  503. return _foundMedia.count;
  504. }
  505. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  506. {
  507. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  508. cell.mediaObject = _foundMedia[indexPath.row];
  509. cell.collectionView = _collectionView;
  510. [cell setEditing:self.editing animated:NO];
  511. return cell;
  512. }
  513. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  514. {
  515. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  516. if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
  517. return CGSizeMake(341., 190.);
  518. else
  519. return CGSizeMake(384., 216.);
  520. }
  521. return CGSizeMake(298.0, 220.0);
  522. }
  523. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  524. {
  525. if (SYSTEM_RUNS_IOS7_OR_LATER)
  526. return UIEdgeInsetsMake(0., 0., 0., 0.);
  527. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  528. }
  529. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  530. {
  531. if (SYSTEM_RUNS_IOS7_OR_LATER)
  532. return 0.;
  533. return 10.0;
  534. }
  535. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  536. {
  537. if (SYSTEM_RUNS_IOS7_OR_LATER)
  538. return 0.;
  539. return 10.0;
  540. }
  541. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  542. {
  543. if (self.editing) {
  544. if (_libraryMode == VLCLibraryModeCreateFolder) {
  545. _folderObject = _foundMedia[indexPath.item];
  546. _libraryMode = _previousLibraryMode;
  547. [self updateViewContents];
  548. [self createFolderWithName:nil];
  549. }
  550. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  551. return;
  552. }
  553. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  554. if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
  555. VLCMediaList *list;
  556. NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
  557. NSUInteger count = tracks.count;
  558. list = [[VLCMediaList alloc] init];
  559. MLFile *file;
  560. for (NSInteger x = count - 1; x > -1; x--) {
  561. file = [(MLAlbumTrack*)tracks[x] files].anyObject;
  562. [list addMedia:[VLCMedia mediaWithURL: [NSURL URLWithString:file.url]]];
  563. }
  564. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
  565. } else if ([selectedObject isKindOfClass:[MLFile class]] && [((MLFile *)selectedObject).labels count] > 0) {
  566. VLCMediaList *list;
  567. MLLabel *folder = [((MLFile *)selectedObject).labels anyObject];
  568. NSArray *folderTracks = [folder.files allObjects];
  569. NSUInteger count = folderTracks.count;
  570. list = [[VLCMediaList alloc] init];
  571. MLFile *file;
  572. for (NSInteger x = count - 1; x > -1; x--) {
  573. file = (MLFile *)folderTracks[x];
  574. [list addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:file.url]]];
  575. }
  576. [(VLCAppDelegate *)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[folderTracks indexOfObject:selectedObject]];
  577. } else
  578. [self openMediaObject:selectedObject];
  579. }
  580. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  581. {
  582. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  583. }
  584. - (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
  585. {
  586. MLFile *mediaObject = (MLFile *)_foundMedia[indexPath.item];
  587. mediaObject.labels = nil;
  588. mediaObject.folderTrackNumber = nil;
  589. [self backToAllItems:nil];
  590. }
  591. - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
  592. {
  593. MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
  594. [_foundMedia removeObjectAtIndex:fromIndexPath.item];
  595. [_foundMedia insertObject:object atIndex:toIndexPath.item];
  596. object.folderTrackNumber = @(toIndexPath.item - 1);
  597. object = [_foundMedia objectAtIndex:fromIndexPath.item];
  598. object.folderTrackNumber = @(fromIndexPath.item - 1);
  599. }
  600. - (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
  601. {
  602. BOOL validFileTypeAtFolderPath = [_foundMedia[folderPath.item] isKindOfClass:[MLFile class]] || [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
  603. if (!(validFileTypeAtFolderPath && [_foundMedia[itemPath.item] isKindOfClass:[MLFile class]])) {
  604. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_INVALID_TYPE_TITLE", @"") message:NSLocalizedString(@"FOLDER_INVALID_TYPE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  605. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  606. [self updateViewContents];
  607. };
  608. [alert show];
  609. return;
  610. }
  611. BOOL isFolder = [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
  612. if (isFolder){
  613. MLLabel *folder = _foundMedia[folderPath.item];
  614. MLFile *file = _foundMedia[itemPath.item];
  615. [file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
  616. file.folderTrackNumber = @([folder.files count] - 1);
  617. [_foundMedia removeObjectAtIndex:itemPath.item];
  618. [self updateViewContents];
  619. } else {
  620. _folderObject = _foundMedia[folderPath.item];
  621. _indexPaths = [NSMutableArray arrayWithArray:@[itemPath]];
  622. [self showCreateFolderAlert];
  623. }
  624. }
  625. #pragma mark - Folder implementation
  626. - (void)showCreateFolderAlert
  627. {
  628. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_CHOOSE_NAME_TITLE", @"") message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", @"") cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", @"")]];
  629. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  630. [[alert textFieldAtIndex:0] setText:NSLocalizedString(@"FOLDER_NAME_PLACEHOLDER", @"")];
  631. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  632. __weak VLCAlertView *weakAlert = alert;
  633. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  634. if (cancelled)
  635. [self updateViewContents];
  636. else
  637. [self createFolderWithName:[weakAlert textFieldAtIndex:0].text];
  638. };
  639. [alert show];
  640. }
  641. - (void)createFolder
  642. {
  643. if (_libraryMode == VLCLibraryModeCreateFolder) {
  644. _libraryMode = _previousLibraryMode;
  645. [self updateViewContents];
  646. [self showCreateFolderAlert];
  647. return;
  648. }
  649. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  650. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  651. else
  652. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  653. for (NSIndexPath *path in _indexPaths) {
  654. id mediaObject;
  655. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  656. mediaObject = _foundMedia[path.item];
  657. else
  658. mediaObject = _foundMedia[path.row];
  659. if ([mediaObject isKindOfClass:[MLLabel class]])
  660. [_indexPaths removeObject:path];
  661. }
  662. if ([_indexPaths count] != 0) {
  663. NSArray *folder = [MLLabel allLabels];
  664. //if we already have folders display them
  665. if ([folder count] > 0) {
  666. _foundMedia = [NSMutableArray arrayWithArray:folder];
  667. self.title = NSLocalizedString(@"SELECT_FOLDER", @"");
  668. _previousLibraryMode = _libraryMode;
  669. _libraryMode = VLCLibraryModeCreateFolder;
  670. [self reloadViews];
  671. return;
  672. }
  673. }
  674. //no selected items or no existing folder ask for foldername
  675. [self showCreateFolderAlert];
  676. }
  677. - (void)removeFromFolder
  678. {
  679. BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
  680. if (isPad)
  681. _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
  682. else
  683. _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
  684. [_indexPaths sortUsingSelector:@selector(compare:)];
  685. for (int i = [_indexPaths count] - 1; i >= 0; i--) {
  686. NSIndexPath *path = _indexPaths[i];
  687. MLFile *file = (MLFile *)_foundMedia[isPad ? path.item : path.row];
  688. MLLabel *folder = [file.labels anyObject];
  689. file.labels = nil;
  690. file.folderTrackNumber = nil;
  691. [_foundMedia removeObject:file];
  692. if ([folder.files count] == 0) {
  693. [self removeMediaObject:folder updateDatabase:YES];
  694. [self backToAllItems:nil];
  695. }
  696. }
  697. [self reloadViews];
  698. }
  699. - (void)createFolderWithName:(NSString *)folderName
  700. {
  701. NSArray *labels = [MLLabel allLabels];
  702. for (MLLabel *label in labels){
  703. if ([label.name isEqualToString:folderName]) {
  704. _folderObject = nil;
  705. _indexPaths = nil;
  706. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_TITLE", @"") message:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
  707. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  708. [self updateViewContents];
  709. };
  710. [alert show];
  711. return;
  712. }
  713. }
  714. if (_folderObject != nil) {
  715. int folderIndex = [_foundMedia indexOfObject:_folderObject];
  716. //item got dragged onto item
  717. if ([_foundMedia[folderIndex] isKindOfClass:[MLFile class]]) {
  718. MLFile *file = _foundMedia[folderIndex];
  719. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  720. label.name = folderName;
  721. file.labels = [NSSet setWithObjects:label,nil];
  722. NSNumber *folderTrackNumber = [NSNumber numberWithInt:[label files].count - 1];
  723. file.folderTrackNumber = folderTrackNumber;
  724. [_foundMedia removeObjectAtIndex:folderIndex];
  725. [_foundMedia insertObject:label atIndex:folderIndex];
  726. MLFile *itemFile = _foundMedia[((NSIndexPath *)_indexPaths[0]).item];
  727. itemFile.labels = file.labels;
  728. [_foundMedia removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
  729. itemFile.folderTrackNumber = @([label files].count - 1);
  730. } else {
  731. //item got dragged onto folder or items should be added to folder
  732. MLLabel *label = _foundMedia[folderIndex];
  733. [_indexPaths sortUsingSelector:@selector(compare:)];
  734. for (int i = [_indexPaths count] - 1; i >= 0; i--) {
  735. NSIndexPath *path = _indexPaths[i];
  736. MLFile *file = _foundMedia[path.row];
  737. file.labels = [NSSet setWithObjects:label, nil];
  738. [_foundMedia removeObjectAtIndex:path.row];
  739. file.folderTrackNumber = @([label files].count - 1);
  740. }
  741. }
  742. _folderObject = nil;
  743. } else {
  744. //create new folder
  745. MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
  746. label.name = folderName;
  747. //if items were selected
  748. if ([_indexPaths count] != 0) {
  749. [_indexPaths sortUsingSelector:@selector(compare:)];
  750. for (int i = [_indexPaths count] - 1; i >= 0; i--) {
  751. NSIndexPath *path = _indexPaths[i];
  752. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  753. MLFile *file = _foundMedia[path.item];
  754. file.labels = [NSSet setWithObjects:label, nil];
  755. file.folderTrackNumber = @([label files].count - 1);
  756. [_foundMedia removeObjectAtIndex:path.item];
  757. } else {
  758. MLFile *file = _foundMedia[path.row];
  759. file.labels = [NSSet setWithObjects:label, nil];
  760. file.folderTrackNumber = @([label files].count - 1);
  761. [_foundMedia removeObjectAtIndex:path.row];
  762. }
  763. }
  764. }
  765. }
  766. _indexPaths = nil;
  767. [self setEditing:NO];
  768. [self updateViewContents];
  769. }
  770. - (void)_collectionViewHandleLongPressGesture:(UIGestureRecognizer *) sender
  771. {
  772. [self setEditing:YES animated:YES];
  773. }
  774. #pragma mark - UI implementation
  775. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  776. {
  777. [super setEditing:editing animated:animated];
  778. UIBarButtonItem *editButton = self.editButtonItem;
  779. NSString *editImage = editing? @"doneButton": @"button";
  780. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  781. if (SYSTEM_RUNS_IOS7_OR_LATER)
  782. editButton.tintColor = [UIColor whiteColor];
  783. else {
  784. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  785. barMetrics:UIBarMetricsDefault];
  786. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  787. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  788. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  789. }
  790. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  791. NSArray *visibleCells = self.collectionView.visibleCells;
  792. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  793. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  794. [aCell setEditing:editing animated:animated];
  795. }];
  796. self.collectionView.allowsMultipleSelection = editing;
  797. /* UIKit doesn't clear the selection automagically if we leave the editing mode
  798. * so we need to do so manually */
  799. if (!editing) {
  800. [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
  801. NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
  802. NSUInteger count = selectedItems.count;
  803. for (NSUInteger x = 0; x < count; x++)
  804. [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
  805. } else
  806. [self.collectionView removeGestureRecognizer:_longPressGestureRecognizer];
  807. } else {
  808. self.tableView.allowsMultipleSelectionDuringEditing = editing;
  809. [self.tableView setEditing:editing animated:YES];
  810. [self.editButtonItem setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL",@"") : NSLocalizedString(@"BUTTON_EDIT", @"")];
  811. }
  812. if (_libraryMode == VLCLibraryModeCreateFolder) {
  813. _libraryMode = _previousLibraryMode;
  814. _indexPaths = nil;
  815. [self updateViewContents];
  816. }
  817. self.navigationController.toolbarHidden = !editing;
  818. }
  819. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  820. {
  821. return UITableViewCellEditingStyleDelete;
  822. }
  823. - (IBAction)leftButtonAction:(id)sender
  824. {
  825. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  826. if (self.isEditing)
  827. [self setEditing:NO animated:YES];
  828. }
  829. - (IBAction)backToAllItems:(id)sender
  830. {
  831. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  832. //for some reason the Gesturerecognizer block themselves if not removed manually
  833. for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
  834. if (recognizer == _reorderLayout.panGestureRecognizer || recognizer == _reorderLayout.longPressGestureRecognizer)
  835. [self.collectionView removeGestureRecognizer:recognizer];
  836. }
  837. _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
  838. [self.collectionView setCollectionViewLayout:_folderLayout animated:NO];
  839. _reorderLayout = nil;
  840. [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
  841. }
  842. inFolder = NO;
  843. UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
  844. NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
  845. toolbarItems[0] = createFolderItem;
  846. self.toolbarItems = toolbarItems;
  847. [self setLibraryMode:_libraryMode];
  848. [self updateViewContents];
  849. }
  850. - (void)_endEditingWithHardReset:(BOOL)hardReset
  851. {
  852. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  853. if (hardReset)
  854. [self updateViewContents];
  855. else
  856. [self reloadViews];
  857. [self setEditing:NO animated:YES];
  858. }
  859. - (void)deleteSelection
  860. {
  861. NSArray *indexPaths;
  862. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  863. indexPaths = [self.collectionView indexPathsForSelectedItems];
  864. else
  865. indexPaths = [self.tableView indexPathsForSelectedRows];
  866. NSUInteger count = indexPaths.count;
  867. NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:count];
  868. for (NSUInteger x = 0; x < count; x++)
  869. [objects addObject:_foundMedia[[indexPaths[x] row]]];
  870. for (NSUInteger x = 0; x < count; x++)
  871. [self removeMediaObject:objects[x] updateDatabase:NO];
  872. [self _endEditingWithHardReset:YES];
  873. }
  874. - (void)renameSelection
  875. {
  876. NSArray *indexPaths;
  877. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  878. indexPaths = [self.collectionView indexPathsForSelectedItems];
  879. else
  880. indexPaths = [self.tableView indexPathsForSelectedRows];
  881. if (indexPaths.count < 1) {
  882. [self _endEditingWithHardReset:NO];
  883. return;
  884. }
  885. NSString *itemName;
  886. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  887. itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
  888. else
  889. itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
  890. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", @""), itemName] message:nil cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_RENAME", @"")]];
  891. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  892. [[alert textFieldAtIndex:0] setText:itemName];
  893. [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
  894. __weak VLCAlertView *weakAlert = alert;
  895. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  896. if (cancelled)
  897. [self _endEditingWithHardReset:NO];
  898. else
  899. [self renameMediaObjectTo:[weakAlert textFieldAtIndex:0].text];
  900. };
  901. [alert show];
  902. }
  903. - (void)renameMediaObjectTo:(NSString*)newName
  904. {
  905. NSArray *indexPaths;
  906. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  907. indexPaths = [self.collectionView indexPathsForSelectedItems];
  908. else
  909. indexPaths = [self.tableView indexPathsForSelectedRows];
  910. if (indexPaths.count < 1)
  911. return;
  912. id mediaObject = _foundMedia[[indexPaths[0] row]];
  913. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]] || [mediaObject isKindOfClass:[MLLabel class]] )
  914. [mediaObject setName:newName];
  915. else
  916. [mediaObject setTitle:newName];
  917. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  918. [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
  919. else
  920. [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
  921. if (indexPaths.count > 1)
  922. [self renameSelection];
  923. else
  924. [self _endEditingWithHardReset:NO];
  925. }
  926. #pragma mark - coin coin
  927. - (void)setLibraryMode:(VLCLibraryMode)mode
  928. {
  929. _libraryMode = mode;
  930. [self updateViewContents];
  931. }
  932. #pragma mark - autorotation
  933. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  934. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  935. /* introduced in iOS 6 */
  936. - (NSUInteger)supportedInterfaceOrientations
  937. {
  938. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  939. return UIInterfaceOrientationMaskAll;
  940. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  941. UIInterfaceOrientationMaskPortrait;
  942. }
  943. /* introduced in iOS 6 */
  944. - (BOOL)shouldAutorotate
  945. {
  946. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  947. }
  948. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  949. {
  950. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  951. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  952. [self.collectionView.collectionViewLayout invalidateLayout];
  953. }
  954. @end