VLCPlaylistViewController.m 8.48 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];

37
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_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
    }
47 48

    self.tabBar.selectedItem = self.localFilesBarItem;
49
    self.networkStreamsBarItem.title = NSLocalizedString(@"TABBAR_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];
}

72 73 74
- (void)removeMediaObject:(MLFile *)mediaObject
{
    [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
75 76

    [self updateViewContents];
77 78
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
79 80
#pragma mark - Table View

81
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
82
{
83 84
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

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

87 88 89 90
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        [self.tableView reloadData];
    else
        [self.gridView reloadData];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
}

- (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
{
106
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
107

108
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
109
    if (cell == nil)
110
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
111

112 113
    cell.mediaObject = _foundMedia[indexPath.row];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
114 115 116 117 118 119 120 121 122 123
    return cell;
}

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
124 125
    if (editingStyle == UITableViewCellEditingStyleDelete)
        [self removeMediaObject: _foundMedia[indexPath.row]];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
126 127
}

128
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
129
{
130
    MLFile *mediaObject = _foundMedia[indexPath.row];
131
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
132
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
133

134 135
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
136 137
}

138 139
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
140
{
141
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
142 143
}

144
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
145
{
146
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
147

148
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
149
    if (cell == nil) {
150
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
151
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
152 153
    }

154
    cell.mediaObject = _foundMedia[index];
155 156 157 158 159 160

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
161
    return [VLCPlaylistGridView preferredSize];
162 163 164 165 166
}

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

170 171
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
172 173
}

174
#pragma mark - UI implementation
175 176
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
177 178 179 180 181
    if (_editMode != editing)
        _editMode = editing;
    else
        _editMode = !editing;

182 183 184
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [self.gridView setEditing:_editMode];
    } else {
185
        [self.tableView setEditing:_editMode animated:YES];
186
    }
187 188

    if (_editMode) {
189
        self.editButtonItem.style = UIBarButtonItemStyleDone;
190
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
191 192
    } else {
        self.editButtonItem.style = UIBarButtonItemStylePlain;
193
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
194 195 196
    }
}

197 198
- (void)showAboutView:(id)sender
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
199 200 201
    if (!self.aboutViewController)
        self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
    [self.navigationController pushViewController:self.aboutViewController animated:YES];
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/* deprecated in iOS 6 */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES; // We support all 4 possible orientations
}

/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

/* introduced in iOS 6 */
- (BOOL)shouldAutorotate {
    return YES;
}

219 220 221 222 223 224 225 226 227 228 229 230 231
#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]];
232
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"OPEN_URL", @"") message:messageString delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_OPEN", @""), nil];
233 234 235 236 237 238 239 240 241 242
                [alert show];
            }
        }
    }

    self.tabBar.selectedItem = self.localFilesBarItem;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
243 244 245
    if (buttonIndex == 1)
        [self openMovieFromURL:_pasteURL];
}
246

247 248 249 250 251 252 253
- (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];
254 255
}

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