VLCPlaylistViewController.m 9.57 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",@"");
50 51 52 53 54 55

    self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
    self.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
    self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
    [self.emptyLibraryLongDescriptionLabel sizeToFit];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
56 57
}

58 59 60 61
- (void)viewWillAppear:(BOOL)animated
{
    [self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:animated];
    [super viewWillAppear:animated];
62 63

    [self _displayEmptyLibraryViewIfNeeded];
64 65
}

66 67 68
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
69

70 71 72 73 74 75 76 77 78 79
    [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];
}

80 81 82
- (void)removeMediaObject:(MLFile *)mediaObject
{
    [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
83 84

    [self updateViewContents];
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    [self _displayEmptyLibraryViewIfNeeded];
}

- (void)_displayEmptyLibraryViewIfNeeded
{
    if (_foundMedia.count > 0) {
        if (self.emptyLibraryView.superview)
            [self.emptyLibraryView removeFromSuperview];
    } else {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
            self.emptyLibraryView.frame = self.tableView.frame;
        else
            self.emptyLibraryView.frame = self.gridView.frame;
        [self.view addSubview:self.emptyLibraryView];
    }
100 101
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
102 103
#pragma mark - Table View

104
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
105
{
106 107
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

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

110 111 112 113
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        [self.tableView reloadData];
    else
        [self.gridView reloadData];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

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

131
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
132
    if (cell == nil)
133
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
134

135 136
    cell.mediaObject = _foundMedia[indexPath.row];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
137 138 139 140 141 142 143 144 145 146
    return cell;
}

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

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

151
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
152
{
153
    MLFile *mediaObject = _foundMedia[indexPath.row];
154
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
155
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
156

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

161 162
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
163
{
164
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
165 166
}

167
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
168
{
169
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
170

171
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
172
    if (cell == nil) {
173
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
174
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
175 176
    }

177
    cell.mediaObject = _foundMedia[index];
178 179 180 181 182 183

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
184
    return [VLCPlaylistGridView preferredSize];
185 186 187 188 189
}

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

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

197
#pragma mark - UI implementation
198 199
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
200 201 202 203 204
    if (_editMode != editing)
        _editMode = editing;
    else
        _editMode = !editing;

205 206 207
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [self.gridView setEditing:_editMode];
    } else {
208
        [self.tableView setEditing:_editMode animated:YES];
209
    }
210 211

    if (_editMode) {
212
        self.editButtonItem.style = UIBarButtonItemStyleDone;
213
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
214 215
    } else {
        self.editButtonItem.style = UIBarButtonItemStylePlain;
216
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
217 218 219
    }
}

220 221
- (void)showAboutView:(id)sender
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
222 223 224
    if (!self.aboutViewController)
        self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
    [self.navigationController pushViewController:self.aboutViewController animated:YES];
225 226
}

227
/* deprecated in iOS 6 */
228 229 230 231 232 233
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (_foundMedia.count > 0)
        return YES;
    else
        return NO;
234 235 236 237
}

/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
238 239 240 241
    if (_foundMedia.count > 0)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
242 243 244 245
}

/* introduced in iOS 6 */
- (BOOL)shouldAutorotate {
246 247 248 249
    if (_foundMedia.count > 0)
        return YES;
    else
        return NO;
250 251
}

252 253 254 255 256 257 258 259 260 261 262 263 264
#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]];
265
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"OPEN_URL", @"") message:messageString delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_OPEN", @""), nil];
266 267 268 269 270 271 272 273 274 275
                [alert show];
            }
        }
    }

    self.tabBar.selectedItem = self.localFilesBarItem;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
276 277 278
    if (buttonIndex == 1)
        [self openMovieFromURL:_pasteURL];
}
279

280 281 282 283 284 285 286
- (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];
287 288
}

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