VLCPlaylistViewController.m 15 KB

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