VLCPlaylistViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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,
  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 themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  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.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  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. - (BOOL)canBecomeFirstResponder
  96. {
  97. return YES;
  98. }
  99. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  100. {
  101. if (motion == UIEventSubtypeMotionShake)
  102. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  103. }
  104. - (void)removeMediaObject:(MLFile *)mediaObject
  105. {
  106. NSFileManager *fileManager = [NSFileManager defaultManager];
  107. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  108. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  109. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  110. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  111. return ([obj rangeOfString:fileName].location != NSNotFound);
  112. }];
  113. unsigned int count = indexSet.count;
  114. NSString *additionalFilePath;
  115. NSUInteger currentIndex = [indexSet firstIndex];
  116. for (unsigned int x = 0; x < count; x++) {
  117. additionalFilePath = allfiles[currentIndex];
  118. if ([additionalFilePath isSupportedSubtitleFormat])
  119. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  120. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  121. }
  122. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  123. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  124. [self updateViewContents];
  125. }
  126. - (void)_displayEmptyLibraryViewIfNeeded
  127. {
  128. if (_foundMedia.count > 0) {
  129. if (self.emptyLibraryView.superview)
  130. [self.emptyLibraryView removeFromSuperview];
  131. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  132. } else {
  133. self.emptyLibraryView.frame = self.view.frame;
  134. [self.view addSubview:self.emptyLibraryView];
  135. self.navigationItem.rightBarButtonItem = nil;
  136. }
  137. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  138. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  139. UITableViewCellSeparatorStyleNone;
  140. }
  141. }
  142. - (void)updateViewContents
  143. {
  144. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  145. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  146. [self.tableView reloadData];
  147. else {
  148. [self.gridView reloadData];
  149. [self.gridView setNeedsLayout];
  150. }
  151. [self _displayEmptyLibraryViewIfNeeded];
  152. }
  153. #pragma mark - Table View
  154. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  155. {
  156. return 1;
  157. }
  158. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  159. {
  160. return _foundMedia.count;
  161. }
  162. // Customize the appearance of table view cells.
  163. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. static NSString *CellIdentifier = @"PlaylistCell";
  166. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  167. if (cell == nil)
  168. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  169. NSInteger row = indexPath.row;
  170. cell.mediaObject = _foundMedia[row];
  171. return cell;
  172. }
  173. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  174. {
  175. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  176. }
  177. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  178. {
  179. return YES;
  180. }
  181. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  182. {
  183. if (editingStyle == UITableViewCellEditingStyleDelete)
  184. [self removeMediaObject: _foundMedia[indexPath.row]];
  185. }
  186. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  187. {
  188. MLFile *mediaObject = _foundMedia[indexPath.row];
  189. if (!self.movieViewController)
  190. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  191. self.movieViewController.mediaItem = mediaObject;
  192. [self.navigationController pushViewController:self.movieViewController animated:YES];
  193. }
  194. #pragma mark - AQGridView
  195. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  196. {
  197. return _foundMedia.count;
  198. }
  199. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  200. {
  201. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  202. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  203. if (cell == nil) {
  204. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  205. cell.selectionStyle = AQGridViewCellSelectionStyleNone;
  206. cell.gridView = gridView;
  207. }
  208. cell.mediaObject = _foundMedia[index];
  209. return cell;
  210. }
  211. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  212. {
  213. return [VLCPlaylistGridView preferredSize];
  214. }
  215. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  216. {
  217. [self.gridView deselectItemAtIndex:index animated:YES];
  218. MLFile *mediaObject = _foundMedia[index];
  219. if (!self.movieViewController)
  220. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  221. self.movieViewController.mediaItem = mediaObject;
  222. [self.navigationController pushViewController:self.movieViewController animated:YES];
  223. }
  224. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  225. {
  226. if (editingStyle == UITableViewCellEditingStyleDelete)
  227. [self removeMediaObject: _foundMedia[index]];
  228. }
  229. #pragma mark - UI implementation
  230. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  231. {
  232. [super setEditing:editing animated:animated];
  233. UIBarButtonItem *editButton = self.editButtonItem;
  234. NSString *editImage = editing? @"doneButton": @"button";
  235. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  236. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  237. barMetrics:UIBarMetricsDefault];
  238. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  239. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  240. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  241. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  242. [self.gridView setEditing:editing];
  243. else
  244. [self.tableView setEditing:editing animated:YES];
  245. }
  246. - (IBAction)leftButtonAction:(id)sender
  247. {
  248. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  249. }
  250. /* deprecated in iOS 6 */
  251. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  252. {
  253. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  254. return YES;
  255. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  256. }
  257. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  258. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  259. /* introduced in iOS 6 */
  260. - (NSUInteger)supportedInterfaceOrientations {
  261. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  262. return UIInterfaceOrientationMaskAll;
  263. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  264. UIInterfaceOrientationMaskPortrait;
  265. }
  266. /* introduced in iOS 6 */
  267. - (BOOL)shouldAutorotate {
  268. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  269. }
  270. #pragma mark - coin coin
  271. - (void)openMovieFromURL:(NSURL *)url
  272. {
  273. if (!self.movieViewController)
  274. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  275. if (self.navigationController.topViewController != self.movieViewController)
  276. [self.navigationController pushViewController:self.movieViewController animated:YES];
  277. self.movieViewController.url = url;
  278. }
  279. @end