VLCPlaylistViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #import "VLCPlaylistViewController.h"
  9. #import "VLCMovieViewController.h"
  10. #import "VLCPlaylistTableViewCell.h"
  11. #import "VLCPlaylistGridView.h"
  12. #import "VLCAboutViewController.h"
  13. @interface VLCPlaylistViewController () {
  14. NSMutableArray *_foundMedia;
  15. }
  16. @end
  17. @implementation VLCPlaylistViewController
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self)
  22. self.title = @"Aspen";
  23. return self;
  24. }
  25. - (void)viewDidLoad
  26. {
  27. self.tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  28. self.tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
  29. [super viewDidLoad];
  30. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ABOUT",@"") style:UIBarButtonItemStyleBordered target:self action:@selector(showAboutView:)];
  31. self.navigationItem.leftBarButtonItem = addButton;
  32. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  33. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  34. _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
  35. _gridView.alwaysBounceVertical = YES;
  36. _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  37. }
  38. self.tabBar.selectedItem = self.localFilesBarItem;
  39. self.networkStreamsBarItem.title = NSLocalizedString(@"TABBAR_NETWORK",@"");
  40. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  41. self.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
  42. self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  43. self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  44. [self.emptyLibraryLongDescriptionLabel sizeToFit];
  45. }
  46. - (void)viewWillAppear:(BOOL)animated
  47. {
  48. [self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:animated];
  49. [super viewWillAppear:animated];
  50. [self _displayEmptyLibraryViewIfNeeded];
  51. }
  52. - (void)viewDidAppear:(BOOL)animated
  53. {
  54. [super viewDidAppear:animated];
  55. [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
  56. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  57. }
  58. - (void)viewDidDisappear:(BOOL)animated
  59. {
  60. [super viewDidDisappear:animated];
  61. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  62. }
  63. - (void)removeMediaObject:(MLFile *)mediaObject
  64. {
  65. [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  66. [self updateViewContents];
  67. [self _displayEmptyLibraryViewIfNeeded];
  68. }
  69. - (void)_displayEmptyLibraryViewIfNeeded
  70. {
  71. if (_foundMedia.count > 0) {
  72. if (self.emptyLibraryView.superview)
  73. [self.emptyLibraryView removeFromSuperview];
  74. } else {
  75. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  76. self.emptyLibraryView.frame = self.tableView.frame;
  77. else
  78. self.emptyLibraryView.frame = self.gridView.frame;
  79. [self.view addSubview:self.emptyLibraryView];
  80. }
  81. }
  82. #pragma mark - Table View
  83. - (void)updateViewContents
  84. {
  85. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  86. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  87. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  88. [self.tableView reloadData];
  89. else
  90. [self.gridView reloadData];
  91. }
  92. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  93. {
  94. return 1;
  95. }
  96. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  97. {
  98. return _foundMedia.count;
  99. }
  100. // Customize the appearance of table view cells.
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. static NSString *CellIdentifier = @"PlaylistCell";
  104. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  105. if (cell == nil)
  106. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  107. cell.mediaObject = _foundMedia[indexPath.row];
  108. return cell;
  109. }
  110. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  111. {
  112. return YES;
  113. }
  114. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  115. {
  116. if (editingStyle == UITableViewCellEditingStyleDelete)
  117. [self removeMediaObject: _foundMedia[indexPath.row]];
  118. }
  119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. MLFile *mediaObject = _foundMedia[indexPath.row];
  122. if (!self.movieViewController)
  123. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  124. self.movieViewController.mediaItem = mediaObject;
  125. [self.navigationController pushViewController:self.movieViewController animated:YES];
  126. }
  127. #pragma mark - AQGridView
  128. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  129. {
  130. return _foundMedia.count;
  131. }
  132. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  133. {
  134. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  135. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  136. if (cell == nil) {
  137. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  138. cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
  139. }
  140. cell.mediaObject = _foundMedia[index];
  141. return cell;
  142. }
  143. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  144. {
  145. return [VLCPlaylistGridView preferredSize];
  146. }
  147. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  148. {
  149. MLFile *mediaObject = _foundMedia[index];
  150. if (!self.movieViewController)
  151. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  152. self.movieViewController.mediaItem = mediaObject;
  153. [self.navigationController pushViewController:self.movieViewController animated:YES];
  154. }
  155. #pragma mark - UI implementation
  156. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  157. {
  158. if (_editMode != editing)
  159. _editMode = editing;
  160. else
  161. _editMode = !editing;
  162. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  163. [self.gridView setEditing:_editMode];
  164. } else {
  165. [self.tableView setEditing:_editMode animated:YES];
  166. }
  167. if (_editMode) {
  168. self.editButtonItem.style = UIBarButtonItemStyleDone;
  169. self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
  170. } else {
  171. self.editButtonItem.style = UIBarButtonItemStylePlain;
  172. self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
  173. }
  174. }
  175. - (void)showAboutView:(id)sender
  176. {
  177. if (!self.aboutViewController)
  178. self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  179. [self.navigationController pushViewController:self.aboutViewController animated:YES];
  180. }
  181. /* deprecated in iOS 6 */
  182. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  183. {
  184. if (_foundMedia.count > 0)
  185. return YES;
  186. else
  187. return NO;
  188. }
  189. /* introduced in iOS 6 */
  190. - (NSUInteger)supportedInterfaceOrientations {
  191. if (_foundMedia.count > 0)
  192. return UIInterfaceOrientationMaskAll;
  193. else
  194. return UIInterfaceOrientationMaskPortrait;
  195. }
  196. /* introduced in iOS 6 */
  197. - (BOOL)shouldAutorotate {
  198. if (_foundMedia.count > 0)
  199. return YES;
  200. else
  201. return NO;
  202. }
  203. #pragma mark - tab bar
  204. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
  205. {
  206. if (item == self.networkStreamsBarItem) {
  207. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.url", @"public.text", nil]]) {
  208. _pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  209. if (!_pasteURL || [[_pasteURL absoluteString] isEqualToString:@""]) {
  210. NSString * pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  211. _pasteURL = [NSURL URLWithString:pasteString];
  212. }
  213. if (_pasteURL && ![[_pasteURL scheme] isEqualToString:@""] && ![[_pasteURL absoluteString] isEqualToString:@""]) {
  214. NSString * messageString = [NSString stringWithFormat:@"Do you want to open %@?", [_pasteURL absoluteString]];
  215. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"OPEN_URL", @"") message:messageString delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_OPEN", @""), nil];
  216. [alert show];
  217. }
  218. }
  219. }
  220. self.tabBar.selectedItem = self.localFilesBarItem;
  221. }
  222. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  223. {
  224. if (buttonIndex == 1)
  225. [self openMovieFromURL:_pasteURL];
  226. }
  227. - (void)openMovieFromURL:(NSURL *)url
  228. {
  229. if (!self.movieViewController)
  230. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  231. self.movieViewController.url = url;
  232. [self.navigationController pushViewController:self.movieViewController animated:YES];
  233. }
  234. @end