VLCPlaylistViewController.m 14 KB

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