VLCPlaylistViewController.m 9.63 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 "VLCPlaylistGridView.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

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

@implementation VLCPlaylistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
25
    if (self)
26
        self.title = @"Aspen";
27

Felix Paul Kühne's avatar
Felix Paul Kühne committed
28 29 30 31 32
    return self;
}

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

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

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

42 43 44 45
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
        _gridView.alwaysBounceVertical = YES;
        _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
46
    } else {
47
        self.tabBar.selectedItem = self.localFilesBarItem;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
48 49
        self.networkStreamsBarItem.title = NSLocalizedString(@"Network",@"");
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
50 51
}

52 53 54 55 56 57
- (void)viewWillAppear:(BOOL)animated
{
    [self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:animated];
    [super viewWillAppear:animated];
}

58 59 60
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
61

62 63 64 65 66 67 68 69 70 71
    [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
    [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
72 73
#pragma mark - Table View

74
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
75
{
76 77
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

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

80 81 82 83
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        [self.tableView reloadData];
    else
        [self.gridView reloadData];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

- (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";

101
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
102
    if (cell == nil)
103
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
104 105

    MLFile *object = _foundMedia[indexPath.row];
106 107 108
    cell.titleLabel.text = object.title;
    cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
    cell.thumbnailView.image = object.computedThumbnail;
109 110 111
    cell.progressIndicator.progress = object.lastPosition.floatValue;
    if (cell.progressIndicator.progress < 0.1f)
        cell.progressIndicator.hidden = YES;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
112 113 114 115 116 117 118 119 120 121 122
    return cell;
}

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
123 124 125 126
        NSUInteger row = indexPath.row;
        MLFile *mediaObject = _foundMedia[row];
        [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
        [_foundMedia removeObjectAtIndex:row];
127
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
128
        [self.gridView deleteItemsAtIndices:[NSIndexSet indexSetWithIndex:row] withAnimation:AQGridViewItemAnimationFade];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
129 130 131 132 133
    } 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.
    }
}

134
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
135
{
136 137
    MLFile *mediaObject = _foundMedia[indexPath.row];
    if (!self.movieViewController) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
138
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
139 140 141
    }
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
142 143
}

144 145
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
146
{
147
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
148 149
}

150
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
151
{
152 153
    static NSString *AQCellIdentifier = @"AQCell";

154
    AQGridViewCell *cell = (AQGridViewCell *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
155
    if (cell == nil) {
156 157 158 159 160 161 162
        VLCPlaylistGridView *cellViewClass = [[VLCPlaylistGridView alloc] init];

        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:cellViewClass options:nil];
        cellViewClass = [array objectAtIndex:0];
        cell = [[AQGridViewCell alloc] initWithFrame:cellViewClass.frame reuseIdentifier:AQCellIdentifier];
        [cell.contentView addSubview:cellViewClass];
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
163 164 165
    }

    MLFile *object = _foundMedia[index];
166 167 168 169 170
    VLCPlaylistGridView *cellView = (VLCPlaylistGridView *)[[cell contentView] viewWithTag:1];
    cellView.title = object.title;
    cellView.subtitle = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
    cellView.thumbnail = object.computedThumbnail;
    cellView.progressView.progress = object.lastPosition.floatValue;
171 172 173 174 175 176

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
177 178 179 180 181
    VLCPlaylistGridView *cellViewClass = [[VLCPlaylistGridView alloc] init];
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:cellViewClass options:nil];
    cellViewClass = [array objectAtIndex:0];

    CGSize cellSize = CGSizeMake(cellViewClass.frame.size.width, cellViewClass.frame.size.height);
182 183 184 185 186 187
    return cellSize;
}

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

191 192
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
193 194
}

195
#pragma mark - UI implementation
196 197 198 199
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    if (self.tableView.editing) {
        self.editButtonItem.style = UIBarButtonItemStylePlain;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
200
        self.editButtonItem.title = NSLocalizedString(@"Edit",@"");
201 202 203
        [self.tableView setEditing:NO animated:YES];
    } else {
        self.editButtonItem.style = UIBarButtonItemStyleDone;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
204
        self.editButtonItem.title = NSLocalizedString(@"Done",@"");
205 206 207 208
        [self.tableView setEditing:YES animated:YES];
    }
}

209 210
- (void)showAboutView:(id)sender
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
211 212 213
    if (!self.aboutViewController)
        self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
    [self.navigationController pushViewController:self.aboutViewController animated:YES];
214 215
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
#pragma mark - tab bar
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item == self.networkStreamsBarItem) {
        if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.url", @"public.text", nil]]) {
            _pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
            if (!_pasteURL || [[_pasteURL absoluteString] isEqualToString:@""]) {
                NSString * pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
                _pasteURL = [NSURL URLWithString:pasteString];
            }

            if (_pasteURL && ![[_pasteURL scheme] isEqualToString:@""] && ![[_pasteURL absoluteString] isEqualToString:@""]) {
                NSString * messageString = [NSString stringWithFormat:@"Do you want to open %@?", [_pasteURL absoluteString]];
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Open URL?" message:messageString delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
                [alert show];
            }
        }
    }

    self.tabBar.selectedItem = self.localFilesBarItem;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
240 241 242
    if (buttonIndex == 1)
        [self openMovieFromURL:_pasteURL];
}
243

244 245 246 247 248 249 250
- (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];
251 252
}

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