VLCPlaylistViewController.m 5.52 KB
Newer Older
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1 2 3 4 5 6 7 8 9
//
//  VLCMasterViewController.m
//  AspenProject
//
//  Created by Felix Paul Kühne on 27.02.13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//

#import "VLCPlaylistViewController.h"
10
#import "VLCMovieViewController.h"
11
#import "VLCPlaylistTableViewCell.h"
12
#import "VLCPlaylistGridViewCell.h"
13
#import "VLCAboutViewController.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
14 15 16 17 18 19 20 21 22 23 24 25

@interface VLCPlaylistViewController () {
    NSMutableArray *_foundMedia;
}
@end

@implementation VLCPlaylistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
26
        self.title = @"Aspen";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
27 28 29 30 31 32 33
    }
    return self;
}


- (void)viewDidLoad
{
34 35
    self.tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
    self.tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
36 37
    [super viewDidLoad];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
38
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAboutView:)];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
39 40 41 42
    self.navigationItem.leftBarButtonItem = addButton;

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

43 44
    [self updateViewContents];
    [[MLMediaLibrary sharedMediaLibrary] libraryDidAppear];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
45 46 47 48 49 50 51 52 53 54
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View

55
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
56
{
57 58
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
59

Felix Paul Kühne's avatar
Felix Paul Kühne committed
60
    _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
61

62 63 64 65
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        [self.tableView reloadData];
    else
        [self.gridView reloadData];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

- (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 = @"Cell";

83
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
84
    if (cell == nil)
85
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
86 87

    MLFile *object = _foundMedia[indexPath.row];
88 89 90
    cell.titleLabel.text = object.title;
    cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
    cell.thumbnailView.image = object.computedThumbnail;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
91 92 93 94 95 96 97 98 99 100 101 102
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_foundMedia removeObjectAtIndex:indexPath.row];
103
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
104 105 106 107 108
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

109
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
110
{
111 112
    MLFile *mediaObject = _foundMedia[indexPath.row];
    if (!self.movieViewController) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
113
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
114 115 116
    }
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
117 118
}

119 120
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
121
{
122
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
123 124
}

125
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
126
{
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    static NSString *AQCellIdentifier = @"AQCell";

    VLCPlaylistGridViewCell *cell = (VLCPlaylistGridViewCell *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
    if (cell == nil) {
        cell = [[VLCPlaylistGridViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 384.,216.) reuseIdentifier:AQCellIdentifier];
        cell.selectionStyle = AQGridViewCellSelectionStyleBlueGray;
    }

    MLFile *object = _foundMedia[index];
    cell.title = object.title;
    cell.subtitle = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
    cell.thumbnail = object.computedThumbnail;

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
    static CGSize cellSize = { 384., 216. };
    return cellSize;
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    MLFile *mediaObject = _foundMedia[index];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
152
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
153
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
154

155 156
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
157 158
}

159 160 161
#pragma mark - UI implementation
- (void)showAboutView:(id)sender
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
162 163 164
    if (!self.aboutViewController)
        self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
    [self.navigationController pushViewController:self.aboutViewController animated:YES];
165 166
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
167
@end