VLCPlaylistViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #import "VLCBugreporter.h"
  18. @implementation EmptyLibraryView
  19. @end
  20. @interface VLCPlaylistViewController () <AQGridViewDataSource, AQGridViewDelegate,
  21. UITableViewDataSource, UITableViewDelegate> {
  22. NSMutableArray *_foundMedia;
  23. }
  24. @end
  25. @implementation VLCPlaylistViewController
  26. - (void)loadView {
  27. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  28. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  29. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  30. _tableView.delegate = self;
  31. _tableView.dataSource = self;
  32. self.view = _tableView;
  33. } else {
  34. _gridView = [[AQGridView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  35. _gridView.delegate = self;
  36. _gridView.dataSource = self;
  37. _gridView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"libraryBackground"]];
  38. self.view = _gridView;
  39. }
  40. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  41. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  42. }
  43. #pragma mark -
  44. - (void)viewDidLoad
  45. {
  46. [super viewDidLoad];
  47. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuCone"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
  48. [addButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  49. [addButton setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  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.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title"]];
  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(updateViewContents) 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 updateViewContents];
  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)updateViewContents
  144. {
  145. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  146. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  147. [self.tableView reloadData];
  148. else {
  149. [self.gridView reloadData];
  150. [self.gridView setNeedsLayout];
  151. }
  152. [self _displayEmptyLibraryViewIfNeeded];
  153. }
  154. #pragma mark - Table View
  155. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  156. {
  157. return 1;
  158. }
  159. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  160. {
  161. return _foundMedia.count;
  162. }
  163. // Customize the appearance of table view cells.
  164. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. static NSString *CellIdentifier = @"PlaylistCell";
  167. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  168. if (cell == nil)
  169. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  170. NSInteger row = indexPath.row;
  171. cell.mediaObject = _foundMedia[row];
  172. return cell;
  173. }
  174. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  177. }
  178. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. return YES;
  181. }
  182. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  183. {
  184. if (editingStyle == UITableViewCellEditingStyleDelete)
  185. [self removeMediaObject: _foundMedia[indexPath.row]];
  186. }
  187. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  188. {
  189. MLFile *mediaObject = _foundMedia[indexPath.row];
  190. if (!self.movieViewController)
  191. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  192. self.movieViewController.mediaItem = mediaObject;
  193. [self.navigationController pushViewController:self.movieViewController animated:YES];
  194. }
  195. #pragma mark - AQGridView
  196. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  197. {
  198. return _foundMedia.count;
  199. }
  200. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  201. {
  202. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  203. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  204. if (cell == nil) {
  205. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  206. cell.selectionStyle = AQGridViewCellSelectionStyleNone;
  207. cell.gridView = gridView;
  208. }
  209. cell.mediaObject = _foundMedia[index];
  210. return cell;
  211. }
  212. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  213. {
  214. return [VLCPlaylistGridView preferredSize];
  215. }
  216. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  217. {
  218. [self.gridView deselectItemAtIndex:index animated:YES];
  219. MLFile *mediaObject = _foundMedia[index];
  220. if (!self.movieViewController)
  221. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  222. self.movieViewController.mediaItem = mediaObject;
  223. [self.navigationController pushViewController:self.movieViewController animated:YES];
  224. }
  225. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  226. {
  227. if (editingStyle == UITableViewCellEditingStyleDelete)
  228. [self removeMediaObject: _foundMedia[index]];
  229. }
  230. #pragma mark - UI implementation
  231. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  232. {
  233. [super setEditing:editing animated:animated];
  234. UIBarButtonItem *editButton = self.editButtonItem;
  235. NSString *editImage = editing? @"doneButton": @"button";
  236. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  237. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  238. barMetrics:UIBarMetricsDefault];
  239. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  240. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  241. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  242. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  243. [self.gridView setEditing:editing];
  244. else
  245. [self.tableView setEditing:editing animated:YES];
  246. }
  247. - (IBAction)leftButtonAction:(id)sender
  248. {
  249. if (self.menuViewController == nil) {
  250. VLCMenuViewController *menuViewController = [[VLCMenuViewController alloc] initWithNibName:nil bundle:nil];
  251. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
  252. [navigationController loadTheme];
  253. self.menuViewController = navigationController;
  254. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  255. navigationController.navigationBarHidden = YES;
  256. }
  257. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  258. self.menuViewController.contentSizeForViewInPopover = self.menuViewController.view.frame.size;
  259. if (self.addMediaPopoverController == nil) {
  260. self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.menuViewController];
  261. self.addMediaPopoverController.delegate = self;
  262. }
  263. if (self.addMediaPopoverController.popoverVisible)
  264. [self.addMediaPopoverController dismissPopoverAnimated:YES];
  265. else
  266. [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
  267. permittedArrowDirections:UIPopoverArrowDirectionUp
  268. animated:YES];
  269. } else
  270. [self.navigationController presentViewController:self.menuViewController animated:YES completion:NULL];
  271. }
  272. /* deprecated in iOS 6 */
  273. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  274. {
  275. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  276. return YES;
  277. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  278. }
  279. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  280. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  281. /* introduced in iOS 6 */
  282. - (NSUInteger)supportedInterfaceOrientations {
  283. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  284. return UIInterfaceOrientationMaskAll;
  285. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  286. UIInterfaceOrientationMaskPortrait;
  287. }
  288. /* introduced in iOS 6 */
  289. - (BOOL)shouldAutorotate {
  290. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  291. }
  292. #pragma mark - coin coin
  293. - (void)openMovieFromURL:(NSURL *)url
  294. {
  295. if (!self.movieViewController)
  296. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  297. self.movieViewController.url = url;
  298. [self.navigationController pushViewController:self.movieViewController animated:YES];
  299. }
  300. @end