VLCPlaylistViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  133. } else {
  134. self.emptyLibraryView.frame = self.view.frame;
  135. [self.view addSubview:self.emptyLibraryView];
  136. self.navigationItem.rightBarButtonItem = nil;
  137. }
  138. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  139. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  140. UITableViewCellSeparatorStyleNone;
  141. }
  142. }
  143. - (void)reloadContents
  144. {
  145. if (_libraryMode == kVLCLibraryModeAllAlbums) {
  146. NSArray *rawAlbums = [MLAlbum allAlbums];
  147. _foundMedia = [[NSMutableArray alloc] init];
  148. NSUInteger count = rawAlbums.count;
  149. MLAlbum *album;
  150. for (NSUInteger x = 0; x < count; x++) {
  151. album = rawAlbums[x];
  152. if (album.name.length > 0 && album.tracks.count > 0)
  153. [_foundMedia addObject:album];
  154. }
  155. rawAlbums = nil;
  156. } else if (_libraryMode == kVLCLibraryModeAllSeries) {
  157. NSArray *rawShows = [MLShow allShows];
  158. _foundMedia = [[NSMutableArray alloc] init];
  159. NSUInteger count = rawShows.count;
  160. MLShow *show;
  161. for (NSUInteger x = 0; x < count; x++) {
  162. show = rawShows[x];
  163. if (show.name.length > 0 && show.episodes.count > 0)
  164. [_foundMedia addObject:show];
  165. }
  166. rawShows = nil;
  167. } else
  168. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  169. [self updateViewContents];
  170. }
  171. - (void)updateViewContents
  172. {
  173. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  174. [self.tableView reloadData];
  175. else {
  176. [self.gridView reloadData];
  177. [self.gridView setNeedsLayout];
  178. }
  179. [self _displayEmptyLibraryViewIfNeeded];
  180. }
  181. #pragma mark - Table View
  182. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  183. {
  184. return 1;
  185. }
  186. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  187. {
  188. return _foundMedia.count;
  189. }
  190. // Customize the appearance of table view cells.
  191. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  192. {
  193. static NSString *CellIdentifier = @"PlaylistCell";
  194. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  195. if (cell == nil)
  196. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  197. NSInteger row = indexPath.row;
  198. cell.mediaObject = _foundMedia[row];
  199. return cell;
  200. }
  201. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  202. {
  203. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  204. }
  205. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  206. {
  207. return YES;
  208. }
  209. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  210. {
  211. if (editingStyle == UITableViewCellEditingStyleDelete)
  212. [self removeMediaObject: _foundMedia[indexPath.row]];
  213. }
  214. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  215. {
  216. NSManagedObject *currentObject = _foundMedia[indexPath.row];
  217. if ([currentObject isKindOfClass:[MLAlbum class]]) {
  218. _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)currentObject tracks] allObjects]];
  219. [self updateViewContents];
  220. } else if ([currentObject isKindOfClass:[MLShow class]]) {
  221. _foundMedia = [NSMutableArray arrayWithArray:[[(MLShow *)currentObject episodes] allObjects]];
  222. [self updateViewContents];
  223. } else {
  224. if (!self.movieViewController)
  225. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  226. if ([currentObject isKindOfClass:[MLFile class]])
  227. self.movieViewController.mediaItem = (MLFile *)currentObject;
  228. else if ([currentObject isKindOfClass:[MLAlbumTrack class]])
  229. self.movieViewController.mediaItem = [(MLAlbumTrack*)currentObject files].anyObject;
  230. else if ([currentObject isKindOfClass:[MLShowEpisode class]])
  231. self.movieViewController.mediaItem = [(MLShowEpisode*)currentObject files].anyObject;
  232. [self.navigationController pushViewController:self.movieViewController animated:YES];
  233. }
  234. }
  235. #pragma mark - AQGridView
  236. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  237. {
  238. return _foundMedia.count;
  239. }
  240. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  241. {
  242. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  243. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  244. if (cell == nil) {
  245. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  246. cell.selectionStyle = AQGridViewCellSelectionStyleNone;
  247. cell.gridView = gridView;
  248. }
  249. cell.mediaObject = _foundMedia[index];
  250. return cell;
  251. }
  252. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  253. {
  254. return [VLCPlaylistGridView preferredSize];
  255. }
  256. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  257. {
  258. [self.gridView deselectItemAtIndex:index animated:YES];
  259. MLFile *mediaObject = _foundMedia[index];
  260. if (!self.movieViewController)
  261. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  262. self.movieViewController.mediaItem = mediaObject;
  263. [self.navigationController pushViewController:self.movieViewController animated:YES];
  264. }
  265. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  266. {
  267. if (editingStyle == UITableViewCellEditingStyleDelete)
  268. [self removeMediaObject: _foundMedia[index]];
  269. }
  270. #pragma mark - UI implementation
  271. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  272. {
  273. [super setEditing:editing animated:animated];
  274. UIBarButtonItem *editButton = self.editButtonItem;
  275. NSString *editImage = editing? @"doneButton": @"button";
  276. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  277. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  278. barMetrics:UIBarMetricsDefault];
  279. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  280. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  281. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  282. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  283. [self.gridView setEditing:editing];
  284. else
  285. [self.tableView setEditing:editing animated:YES];
  286. }
  287. - (IBAction)leftButtonAction:(id)sender
  288. {
  289. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  290. }
  291. /* deprecated in iOS 6 */
  292. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  293. {
  294. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  295. return YES;
  296. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  297. }
  298. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  299. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  300. /* introduced in iOS 6 */
  301. - (NSUInteger)supportedInterfaceOrientations {
  302. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  303. return UIInterfaceOrientationMaskAll;
  304. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  305. UIInterfaceOrientationMaskPortrait;
  306. }
  307. /* introduced in iOS 6 */
  308. - (BOOL)shouldAutorotate {
  309. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  310. }
  311. #pragma mark - coin coin
  312. - (void)openMovieFromURL:(NSURL *)url
  313. {
  314. if (!self.movieViewController)
  315. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  316. if (self.navigationController.topViewController != self.movieViewController)
  317. [self.navigationController pushViewController:self.movieViewController animated:YES];
  318. self.movieViewController.url = url;
  319. }
  320. - (void)setLibraryMode:(NSUInteger)mode
  321. {
  322. _libraryMode = mode;
  323. [self reloadContents];
  324. }
  325. @end