VLCPlaylistViewController.m 14 KB

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