VLCPlaylistViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // VLCMasterViewController.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCPlaylistViewController.h"
  11. #import "VLCMovieViewController.h"
  12. #import "VLCPlaylistTableViewCell.h"
  13. #import "VLCPlaylistGridView.h"
  14. #import "UINavigationController+Theme.h"
  15. #import "NSString+SupportedMedia.h"
  16. #import "VLCBugreporter.h"
  17. #import "VLCAppDelegate.h"
  18. #import "UIBarButtonItem+Theme.h"
  19. @implementation EmptyLibraryView
  20. @end
  21. @interface VLCPlaylistViewController () <AQGridViewDataSource, AQGridViewDelegate, UITableViewDataSource, UITableViewDelegate> {
  22. NSMutableArray *_foundMedia;
  23. NSUInteger _libraryMode;
  24. }
  25. @end
  26. @implementation VLCPlaylistViewController
  27. - (void)loadView {
  28. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  29. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  30. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  31. _tableView.delegate = self;
  32. _tableView.dataSource = self;
  33. self.view = _tableView;
  34. } else {
  35. _gridView = [[AQGridView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  36. _gridView.delegate = self;
  37. _gridView.dataSource = self;
  38. _gridView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"libraryBackground"]];
  39. self.view = _gridView;
  40. }
  41. _libraryMode = kVLCLibraryModeAllFiles;
  42. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  43. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  44. }
  45. #pragma mark -
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. UIBarButtonItem *addButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  50. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  51. * wearing a Father Xmas hat.
  52. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  53. * and should not be confused with the idea of religious statements or propagation there off
  54. */
  55. NSCalendar *gregorian =
  56. [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  57. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
  58. if (dayOfYear >= 354)
  59. addButton.image = [UIImage imageNamed:@"vlc-xmas"];
  60. self.navigationItem.leftBarButtonItem = addButton;
  61. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  62. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  63. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  64. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  65. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  66. _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
  67. _gridView.alwaysBounceVertical = YES;
  68. _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  69. } else {
  70. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  71. _tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  72. }
  73. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  74. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  75. _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
  76. _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  77. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  78. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  79. }
  80. - (void)viewWillAppear:(BOOL)animated
  81. {
  82. [super viewWillAppear:animated];
  83. [self _displayEmptyLibraryViewIfNeeded];
  84. }
  85. - (void)viewDidAppear:(BOOL)animated
  86. {
  87. [self performSelector:@selector(reloadContents) withObject:nil afterDelay:.0];
  88. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  89. [super viewDidAppear:animated];
  90. }
  91. - (void)viewDidDisappear:(BOOL)animated
  92. {
  93. [super viewDidDisappear:animated];
  94. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  95. }
  96. - (BOOL)canBecomeFirstResponder
  97. {
  98. return YES;
  99. }
  100. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  101. {
  102. if (motion == UIEventSubtypeMotionShake)
  103. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  104. }
  105. - (void)removeMediaObject:(MLFile *)mediaObject
  106. {
  107. NSFileManager *fileManager = [NSFileManager defaultManager];
  108. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  109. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  110. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  111. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  112. return ([obj rangeOfString:fileName].location != NSNotFound);
  113. }];
  114. unsigned int count = indexSet.count;
  115. NSString *additionalFilePath;
  116. NSUInteger currentIndex = [indexSet firstIndex];
  117. for (unsigned int x = 0; x < count; x++) {
  118. additionalFilePath = allfiles[currentIndex];
  119. if ([additionalFilePath isSupportedSubtitleFormat])
  120. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  121. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  122. }
  123. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  124. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  125. [self reloadContents];
  126. }
  127. - (void)_displayEmptyLibraryViewIfNeeded
  128. {
  129. if (_foundMedia.count > 0) {
  130. if (self.emptyLibraryView.superview)
  131. [self.emptyLibraryView removeFromSuperview];
  132. } else {
  133. self.emptyLibraryView.frame = self.view.frame;
  134. [self.view addSubview:self.emptyLibraryView];
  135. }
  136. if (_libraryMode == kVLCLibraryModeAllFiles && _foundMedia.count > 0)
  137. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  138. else
  139. self.navigationItem.rightBarButtonItem = nil;
  140. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  141. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  142. UITableViewCellSeparatorStyleNone;
  143. }
  144. }
  145. - (void)reloadContents
  146. {
  147. if (_libraryMode == kVLCLibraryModeAllAlbums) {
  148. NSArray *rawAlbums = [MLAlbum allAlbums];
  149. _foundMedia = [[NSMutableArray alloc] init];
  150. NSUInteger count = rawAlbums.count;
  151. MLAlbum *album;
  152. for (NSUInteger x = 0; x < count; x++) {
  153. album = rawAlbums[x];
  154. if (album.name.length > 0 && album.tracks.count > 0)
  155. [_foundMedia addObject:album];
  156. }
  157. rawAlbums = nil;
  158. } else if (_libraryMode == kVLCLibraryModeAllSeries) {
  159. NSArray *rawShows = [MLShow allShows];
  160. _foundMedia = [[NSMutableArray alloc] init];
  161. NSUInteger count = rawShows.count;
  162. MLShow *show;
  163. for (NSUInteger x = 0; x < count; x++) {
  164. show = rawShows[x];
  165. if (show.name.length > 0 && show.episodes.count > 0)
  166. [_foundMedia addObject:show];
  167. }
  168. rawShows = nil;
  169. } else
  170. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  171. [self updateViewContents];
  172. }
  173. - (void)updateViewContents
  174. {
  175. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  176. [self.tableView reloadData];
  177. else {
  178. [self.gridView reloadData];
  179. [self.gridView setNeedsLayout];
  180. }
  181. [self _displayEmptyLibraryViewIfNeeded];
  182. }
  183. #pragma mark - Table View
  184. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  185. {
  186. return 1;
  187. }
  188. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  189. {
  190. return _foundMedia.count;
  191. }
  192. // Customize the appearance of table view cells.
  193. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  194. {
  195. static NSString *CellIdentifier = @"PlaylistCell";
  196. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  197. if (cell == nil)
  198. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  199. NSInteger row = indexPath.row;
  200. cell.mediaObject = _foundMedia[row];
  201. return cell;
  202. }
  203. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  206. }
  207. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  208. {
  209. return YES;
  210. }
  211. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  212. {
  213. if (editingStyle == UITableViewCellEditingStyleDelete)
  214. [self removeMediaObject: _foundMedia[indexPath.row]];
  215. }
  216. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  217. {
  218. NSManagedObject *currentObject = _foundMedia[indexPath.row];
  219. if ([currentObject isKindOfClass:[MLAlbum class]]) {
  220. _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)currentObject tracks] allObjects]];
  221. [self updateViewContents];
  222. } else if ([currentObject isKindOfClass:[MLShow class]]) {
  223. _foundMedia = [NSMutableArray arrayWithArray:[[(MLShow *)currentObject episodes] allObjects]];
  224. [self updateViewContents];
  225. } else {
  226. if (!self.movieViewController)
  227. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  228. if ([currentObject isKindOfClass:[MLFile class]])
  229. self.movieViewController.mediaItem = (MLFile *)currentObject;
  230. else if ([currentObject isKindOfClass:[MLAlbumTrack class]])
  231. self.movieViewController.mediaItem = [(MLAlbumTrack*)currentObject files].anyObject;
  232. else if ([currentObject isKindOfClass:[MLShowEpisode class]])
  233. self.movieViewController.mediaItem = [(MLShowEpisode*)currentObject files].anyObject;
  234. [self.navigationController pushViewController:self.movieViewController animated:YES];
  235. }
  236. }
  237. #pragma mark - AQGridView
  238. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  239. {
  240. return _foundMedia.count;
  241. }
  242. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  243. {
  244. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  245. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  246. if (cell == nil) {
  247. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  248. cell.selectionStyle = AQGridViewCellSelectionStyleNone;
  249. cell.gridView = gridView;
  250. }
  251. cell.mediaObject = _foundMedia[index];
  252. return cell;
  253. }
  254. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  255. {
  256. return [VLCPlaylistGridView preferredSize];
  257. }
  258. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  259. {
  260. [self.gridView deselectItemAtIndex:index animated:YES];
  261. MLFile *mediaObject = _foundMedia[index];
  262. if (!self.movieViewController)
  263. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  264. self.movieViewController.mediaItem = mediaObject;
  265. [self.navigationController pushViewController:self.movieViewController animated:YES];
  266. }
  267. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  268. {
  269. if (editingStyle == UITableViewCellEditingStyleDelete)
  270. [self removeMediaObject: _foundMedia[index]];
  271. }
  272. #pragma mark - UI implementation
  273. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  274. {
  275. if (_libraryMode != kVLCLibraryModeAllFiles)
  276. return;
  277. [super setEditing:editing animated:animated];
  278. UIBarButtonItem *editButton = self.editButtonItem;
  279. NSString *editImage = editing? @"doneButton": @"button";
  280. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  281. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  282. barMetrics:UIBarMetricsDefault];
  283. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  284. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  285. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  286. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  287. [self.gridView setEditing:editing];
  288. else
  289. [self.tableView setEditing:editing animated:YES];
  290. }
  291. - (IBAction)leftButtonAction:(id)sender
  292. {
  293. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  294. }
  295. /* deprecated in iOS 6 */
  296. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  297. {
  298. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  299. return YES;
  300. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  301. }
  302. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  303. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  304. /* introduced in iOS 6 */
  305. - (NSUInteger)supportedInterfaceOrientations {
  306. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  307. return UIInterfaceOrientationMaskAll;
  308. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  309. UIInterfaceOrientationMaskPortrait;
  310. }
  311. /* introduced in iOS 6 */
  312. - (BOOL)shouldAutorotate {
  313. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  314. }
  315. #pragma mark - coin coin
  316. - (void)openMovieFromURL:(NSURL *)url
  317. {
  318. if (!self.movieViewController)
  319. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  320. if (self.navigationController.topViewController != self.movieViewController)
  321. [self.navigationController pushViewController:self.movieViewController animated:YES];
  322. self.movieViewController.url = url;
  323. }
  324. - (void)setLibraryMode:(NSUInteger)mode
  325. {
  326. _libraryMode = mode;
  327. [self reloadContents];
  328. }
  329. @end