VLCPlaylistViewController.m 14 KB

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