VLCPlaylistViewController.m 12.5 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 "VLCMenuViewController.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
14

15 16 17 18 19
@implementation EmptyLibraryView
@end

@interface VLCPlaylistViewController () <AQGridViewDataSource, AQGridViewDelegate,
                                         UITableViewDataSource, UITableViewDelegate> {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
20 21 22 23 24 25 26 27 28 29 30 31
    NSMutableArray *_foundMedia;
}
@end

@implementation VLCPlaylistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

32 33 34 35 36 37 38 39 40 41 42 43 44
- (void)loadView {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        self.view = _tableView;
    } else {
        _gridView = [[AQGridView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _gridView.delegate = self;
        _gridView.dataSource = self;
        self.view = _gridView;
    }

45
    self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
46 47 48 49 50 51
    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
}

#pragma mark -

Felix Paul Kühne's avatar
Felix Paul Kühne committed
52 53 54 55
- (void)viewDidLoad
{
    [super viewDidLoad];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
56
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuCone"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
57 58
    [addButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [addButton setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
59 60 61 62 63 64 65 66 67 68 69 70

    /* 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"];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
71
    self.navigationItem.leftBarButtonItem = addButton;
72

73 74 75 76
    [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
                                   forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
                                   forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
77

78
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
79 80 81
        _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
        _gridView.alwaysBounceVertical = YES;
        _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
82 83
    } else {
        _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
84
        _tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
85
    }
86

87 88
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title"]];

89 90 91 92 93
    _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
    _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
    _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
    [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
94 95
}

96 97 98
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
99
    [self _displayEmptyLibraryViewIfNeeded];
100 101
}

102 103 104 105
- (void)viewDidAppear:(BOOL)animated
{
    [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
    [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
106 107

    [super viewDidAppear:animated];
108 109 110 111 112 113 114 115
}

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

116 117 118
- (void)removeMediaObject:(MLFile *)mediaObject
{
    [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
119 120 121 122 123 124 125
}

- (void)_displayEmptyLibraryViewIfNeeded
{
    if (_foundMedia.count > 0) {
        if (self.emptyLibraryView.superview)
            [self.emptyLibraryView removeFromSuperview];
126 127
        
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
128
    } else {
129
        self.emptyLibraryView.frame = self.view.frame;
130
        [self.view addSubview:self.emptyLibraryView];
131
        self.navigationItem.rightBarButtonItem = nil;
132
    }
133 134 135 136 137

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
                                                             UITableViewCellSeparatorStyleNone;
    }
138 139
}

140
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
141
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
142
    _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
143

144
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
145
        [self.tableView reloadData];
146
    else {
147
        [self.gridView reloadData];
148 149
        [self.gridView setNeedsDisplay];
    }
150 151

    [self _displayEmptyLibraryViewIfNeeded];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
152 153
}

154 155 156

#pragma mark - Table View

Felix Paul Kühne's avatar
Felix Paul Kühne committed
157 158 159 160 161 162 163 164 165 166 167 168 169
- (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
{
170
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
171

172
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
173
    if (cell == nil)
174
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
175

176 177 178
    NSInteger row = indexPath.row;
    cell.mediaObject = _foundMedia[row];
    cell.isEven = (row % 2 == 0);
179

Felix Paul Kühne's avatar
Felix Paul Kühne committed
180 181 182 183 184 185 186 187 188 189
    return cell;
}

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

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

194
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
195
{
196
    MLFile *mediaObject = _foundMedia[indexPath.row];
197
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
198
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
199

200 201
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
202 203
}

204 205
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
206
{
207
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
208 209
}

210
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
211
{
212
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
213

214
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
215
    if (cell == nil) {
216
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
217
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
218
        cell.gridView = gridView;
219 220
    }

221
    cell.mediaObject = _foundMedia[index];
222 223 224 225 226 227

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
228
    return [VLCPlaylistGridView preferredSize];
229 230 231 232
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
233 234
    [self.gridView deselectItemAtIndex:index animated:YES];

235
    MLFile *mediaObject = _foundMedia[index];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
236
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
237
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
238

239 240
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
241 242
}

243 244 245 246 247 248
- (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
        [self removeMediaObject: _foundMedia[index]];
}

249
#pragma mark - UI implementation
250 251
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
252
    [super setEditing:editing animated:animated];
253
    
Felix Paul Kühne's avatar
Felix Paul Kühne committed
254
    UIBarButtonItem *editButton = self.editButtonItem;
255 256
    NSString *editImage = editing? @"doneButton": @"button";
    NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
257
    [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
258
                                 barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
259
    [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
260
                                   forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
261 262
    [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];

263
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
264
        [self.gridView setEditing:editing];
265
    else
266
        [self.tableView setEditing:editing animated:YES];
267 268
}

269
- (IBAction)leftButtonAction:(id)sender
270
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
271 272
    if (self.menuViewController == nil)
        self.menuViewController = [[VLCMenuViewController alloc] initWithNibName:@"VLCMenuViewController" bundle:nil];
273 274

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
275
		self.menuViewController.contentSizeForViewInPopover = self.menuViewController.view.frame.size;
276
        if (self.addMediaPopoverController == nil) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
277
            self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.menuViewController];
278 279 280 281 282 283 284 285 286 287
            self.addMediaPopoverController.delegate = self;
        }

        if (self.addMediaPopoverController.popoverVisible)
            [self.addMediaPopoverController dismissPopoverAnimated:YES];
        else
            [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
                                                   permittedArrowDirections:UIPopoverArrowDirectionUp
                                                                   animated:YES];
    } else
Felix Paul Kühne's avatar
Felix Paul Kühne committed
288
        [self.navigationController presentViewController:self.menuViewController animated:YES completion:NULL];
289 290
}

291
/* deprecated in iOS 6 */
292 293
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
294 295 296 297
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return YES;

    return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
298 299
}

300 301
// RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
// so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
302 303
/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
304 305 306 307 308
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return UIInterfaceOrientationMaskAll;

    return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
                                    UIInterfaceOrientationMaskPortrait;
309 310 311 312
}

/* introduced in iOS 6 */
- (BOOL)shouldAutorotate {
313
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
314 315
}

316
#pragma mark - coin coin
317

318 319 320 321 322 323 324
- (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];
325 326
}

327

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