123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- //
- // VLCMasterViewController.m
- // AspenProject
- //
- // Created by Felix Paul Kühne on 27.02.13.
- // Copyright (c) 2013 VideoLAN. All rights reserved.
- //
- #import "VLCPlaylistViewController.h"
- #import "VLCMovieViewController.h"
- #import "VLCPlaylistTableViewCell.h"
- #import "VLCPlaylistGridView.h"
- #import "VLCMenuViewController.h"
- @interface VLCPlaylistViewController () {
- NSMutableArray *_foundMedia;
- }
- @end
- @implementation VLCPlaylistViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self)
- self.title = @"VLC";
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"vlc"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
- /* After day 354 of the year, the usual VLC cone is replaced by another cone
- * wearing a Father Xmas hat.
- * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
- * and should not be confused with the idea of religious statements or propagation there off
- */
- NSCalendar *gregorian =
- [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
- NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
- if (dayOfYear >= 354)
- addButton.image = [UIImage imageNamed:@"vlc-xmas"];
- self.navigationItem.leftBarButtonItem = addButton;
- self.navigationItem.rightBarButtonItem = self.editButtonItem;
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
- _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
- _gridView.alwaysBounceVertical = YES;
- _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
- } else {
- _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
- _tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
- }
- self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
- self.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
- self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
- self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
- [self.emptyLibraryLongDescriptionLabel sizeToFit];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self _displayEmptyLibraryViewIfNeeded];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
- [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
- [super viewDidAppear:animated];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
- }
- - (void)removeMediaObject:(MLFile *)mediaObject
- {
- [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
- [self updateViewContents];
- }
- - (void)_displayEmptyLibraryViewIfNeeded
- {
- if (_foundMedia.count > 0) {
- if (self.emptyLibraryView.superview)
- [self.emptyLibraryView removeFromSuperview];
- } else {
- self.emptyLibraryView.frame = self.view.frame;
- [self.view addSubview:self.emptyLibraryView];
- }
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
- _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
- UITableViewCellSeparatorStyleNone;
- }
- }
- - (void)updateViewContents
- {
- [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
- _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
- [self.tableView reloadData];
- else {
- [self.gridView reloadData];
- [self.gridView setNeedsDisplay];
- }
- [self _displayEmptyLibraryViewIfNeeded];
- }
- #pragma mark - Table View
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _foundMedia.count;
- }
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"PlaylistCell";
- VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
- cell.mediaObject = _foundMedia[indexPath.row];
- return cell;
- }
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle == UITableViewCellEditingStyleDelete)
- [self removeMediaObject: _foundMedia[indexPath.row]];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- MLFile *mediaObject = _foundMedia[indexPath.row];
- if (!self.movieViewController)
- self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
- self.movieViewController.mediaItem = mediaObject;
- [self.navigationController pushViewController:self.movieViewController animated:YES];
- }
- #pragma mark - AQGridView
- - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
- {
- return _foundMedia.count;
- }
- - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
- {
- static NSString *AQCellIdentifier = @"AQPlaylistCell";
- VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
- if (cell == nil) {
- cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
- cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
- cell.gridView = gridView;
- }
- cell.mediaObject = _foundMedia[index];
- return cell;
- }
- - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
- {
- return [VLCPlaylistGridView preferredSize];
- }
- - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
- {
- [self.gridView deselectItemAtIndex:index animated:YES];
- MLFile *mediaObject = _foundMedia[index];
- if (!self.movieViewController)
- self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
- self.movieViewController.mediaItem = mediaObject;
- [self.navigationController pushViewController:self.movieViewController animated:YES];
- }
- - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
- {
- if (editingStyle == UITableViewCellEditingStyleDelete)
- [self removeMediaObject: _foundMedia[index]];
- }
- #pragma mark - UI implementation
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated
- {
- [super setEditing:editing animated:animated];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- [self.gridView setEditing:editing];
- else
- [self.tableView setEditing:editing animated:YES];
- }
- - (IBAction)leftButtonAction:(id)sender
- {
- if (self.addMediaViewController == nil)
- self.addMediaViewController = [[VLCMenuViewController alloc] initWithNibName:@"VLCAddMediaViewController" bundle:nil];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
- self.addMediaViewController.contentSizeForViewInPopover = self.addMediaViewController.view.frame.size;
- if (self.addMediaPopoverController == nil) {
- self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.addMediaViewController];
- self.addMediaPopoverController.delegate = self;
- }
- if (self.addMediaPopoverController.popoverVisible)
- [self.addMediaPopoverController dismissPopoverAnimated:YES];
- else
- [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
- permittedArrowDirections:UIPopoverArrowDirectionUp
- animated:YES];
- } else
- [self.navigationController presentViewController:self.addMediaViewController animated:YES completion:NULL];
- }
- /* deprecated in iOS 6 */
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- {
- return (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (_foundMedia.count > 0);
- }
- /* introduced in iOS 6 */
- - (NSUInteger)supportedInterfaceOrientations {
- return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAll: UIInterfaceOrientationMaskPortrait;
- }
- /* introduced in iOS 6 */
- - (BOOL)shouldAutorotate {
- return (_foundMedia.count > 0);
- }
- #pragma mark - coin coin
- - (void)openMovieFromURL:(NSURL *)url
- {
- if (!self.movieViewController)
- self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
- self.movieViewController.url = url;
- [self.navigationController pushViewController:self.movieViewController animated:YES];
- }
- @end
|