VLCPlaylistViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. }
  41. - (void)viewWillAppear:(BOOL)animated
  42. {
  43. [self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:animated];
  44. [super viewWillAppear:animated];
  45. }
  46. - (void)viewDidAppear:(BOOL)animated
  47. {
  48. [super viewDidAppear:animated];
  49. [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
  50. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  51. }
  52. - (void)viewDidDisappear:(BOOL)animated
  53. {
  54. [super viewDidDisappear:animated];
  55. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  56. }
  57. - (void)removeMediaObject:(MLFile *)mediaObject
  58. {
  59. [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  60. NSUInteger index = [_foundMedia indexOfObject:mediaObject];
  61. [_foundMedia removeObjectAtIndex:index];
  62. [self.tableView deleteRowsAtIndexPaths:[NSIndexPath indexPathWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
  63. [self.gridView deleteItemsAtIndices:[NSIndexSet indexSetWithIndex:index] withAnimation:AQGridViewItemAnimationFade];
  64. }
  65. #pragma mark - Table View
  66. - (void)updateViewContents
  67. {
  68. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  69. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  70. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  71. [self.tableView reloadData];
  72. else
  73. [self.gridView reloadData];
  74. }
  75. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  76. {
  77. return 1;
  78. }
  79. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  80. {
  81. return _foundMedia.count;
  82. }
  83. // Customize the appearance of table view cells.
  84. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  85. {
  86. static NSString *CellIdentifier = @"PlaylistCell";
  87. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  88. if (cell == nil)
  89. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  90. cell.mediaObject = _foundMedia[indexPath.row];
  91. return cell;
  92. }
  93. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  94. {
  95. return YES;
  96. }
  97. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. if (editingStyle == UITableViewCellEditingStyleDelete)
  100. [self removeMediaObject: _foundMedia[indexPath.row]];
  101. }
  102. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  103. {
  104. MLFile *mediaObject = _foundMedia[indexPath.row];
  105. if (!self.movieViewController) {
  106. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  107. }
  108. self.movieViewController.mediaItem = mediaObject;
  109. [self.navigationController pushViewController:self.movieViewController animated:YES];
  110. }
  111. #pragma mark - AQGridView
  112. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  113. {
  114. return _foundMedia.count;
  115. }
  116. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  117. {
  118. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  119. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  120. if (cell == nil) {
  121. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  122. cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
  123. }
  124. cell.mediaObject = _foundMedia[index];
  125. return cell;
  126. }
  127. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  128. {
  129. return [VLCPlaylistGridView preferredSize];
  130. }
  131. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  132. {
  133. MLFile *mediaObject = _foundMedia[index];
  134. if (!self.movieViewController)
  135. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  136. self.movieViewController.mediaItem = mediaObject;
  137. [self.navigationController pushViewController:self.movieViewController animated:YES];
  138. }
  139. #pragma mark - UI implementation
  140. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  141. {
  142. if (_editMode != editing)
  143. _editMode = editing;
  144. else
  145. _editMode = !editing;
  146. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  147. [self.gridView setEditing:_editMode];
  148. } else {
  149. [self.tableView setEditing:_editMode animated:YES];
  150. }
  151. if (_editMode) {
  152. self.editButtonItem.style = UIBarButtonItemStyleDone;
  153. self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
  154. } else {
  155. self.editButtonItem.style = UIBarButtonItemStylePlain;
  156. self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
  157. }
  158. }
  159. - (void)showAboutView:(id)sender
  160. {
  161. if (!self.aboutViewController)
  162. self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  163. [self.navigationController pushViewController:self.aboutViewController animated:YES];
  164. }
  165. /* deprecated in iOS 6 */
  166. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  167. return YES; // We support all 4 possible orientations
  168. }
  169. /* introduced in iOS 6 */
  170. - (NSUInteger)supportedInterfaceOrientations {
  171. return UIInterfaceOrientationMaskAll;
  172. }
  173. /* introduced in iOS 6 */
  174. - (BOOL)shouldAutorotate {
  175. return YES;
  176. }
  177. #pragma mark - tab bar
  178. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
  179. {
  180. if (item == self.networkStreamsBarItem) {
  181. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.url", @"public.text", nil]]) {
  182. _pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  183. if (!_pasteURL || [[_pasteURL absoluteString] isEqualToString:@""]) {
  184. NSString * pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  185. _pasteURL = [NSURL URLWithString:pasteString];
  186. }
  187. if (_pasteURL && ![[_pasteURL scheme] isEqualToString:@""] && ![[_pasteURL absoluteString] isEqualToString:@""]) {
  188. NSString * messageString = [NSString stringWithFormat:@"Do you want to open %@?", [_pasteURL absoluteString]];
  189. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"OPEN_URL", @"") message:messageString delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_OPEN", @""), nil];
  190. [alert show];
  191. }
  192. }
  193. }
  194. self.tabBar.selectedItem = self.localFilesBarItem;
  195. }
  196. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  197. {
  198. if (buttonIndex == 1)
  199. [self openMovieFromURL:_pasteURL];
  200. }
  201. - (void)openMovieFromURL:(NSURL *)url
  202. {
  203. if (!self.movieViewController)
  204. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  205. self.movieViewController.url = url;
  206. [self.navigationController pushViewController:self.movieViewController animated:YES];
  207. }
  208. @end