VLCPlaylistViewController.m 14 KB

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