VLCPlaylistViewController.m 10.1 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 "VLCAddMediaViewController.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)
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
26
        self.title = @"VLC";
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] initWithImage:[UIImage imageNamed:@"vlc"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
38 39 40 41 42 43 44 45 46 47 48 49

    /* 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
50 51 52
    self.navigationItem.leftBarButtonItem = addButton;

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
53
    self.navigationItem.rightBarButtonItem.title = NSLocalizedString(@"BUTTON_EDIT", @"");
Felix Paul Kühne's avatar
Felix Paul Kühne committed
54

55 56 57 58
    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
59
    }
60

61 62 63 64 65
    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
66 67
}

68 69 70 71
- (void)viewWillAppear:(BOOL)animated
{
    [self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:animated];
    [super viewWillAppear:animated];
72

73 74 75 76 77
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        self.tableView.hidden = YES;
    else
        self.gridView.hidden = YES;

78
    [self _displayEmptyLibraryViewIfNeeded];
79 80
}

81 82
- (void)viewDidAppear:(BOOL)animated
{
83 84 85 86
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        self.tableView.hidden = NO;
    else
        self.gridView.hidden = NO;
87

88 89
    [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
    [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
90 91

    [super viewDidAppear:animated];
92 93 94 95 96 97 98 99
}

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

100 101 102
- (void)removeMediaObject:(MLFile *)mediaObject
{
    [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
103 104

    [self updateViewContents];
105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

- (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];
    }
119 120
}

121

122
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
123
{
124 125
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

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

128 129
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        [self.tableView reloadData];
130
    else {
131
        [self.gridView reloadData];
132 133
        [self.gridView setNeedsDisplay];
    }
134 135

    [self _displayEmptyLibraryViewIfNeeded];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
136 137
}

138 139 140

#pragma mark - Table View

Felix Paul Kühne's avatar
Felix Paul Kühne committed
141 142 143 144 145 146 147 148 149 150 151 152 153
- (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
{
154
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
155

156
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
157
    if (cell == nil)
158
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
159

160 161
    cell.mediaObject = _foundMedia[indexPath.row];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
162 163 164 165 166 167 168 169 170 171
    return cell;
}

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

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

176
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
177
{
178
    MLFile *mediaObject = _foundMedia[indexPath.row];
179
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
180
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
181

182 183
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
184 185
}

186 187
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
188
{
189
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
190 191
}

192
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
193
{
194
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
195

196
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
197
    if (cell == nil) {
198
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
199
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
200 201
    }

202
    cell.mediaObject = _foundMedia[index];
203 204 205 206 207 208

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
209
    return [VLCPlaylistGridView preferredSize];
210 211 212 213 214
}

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

218 219
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
220 221
}

222
#pragma mark - UI implementation
223 224
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
225 226 227 228 229
    if (_editMode != editing)
        _editMode = editing;
    else
        _editMode = !editing;

230
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
231
        [self.gridView setEditing:_editMode];
232
    else
233 234 235
        [self.tableView setEditing:_editMode animated:YES];

    if (_editMode) {
236
        self.editButtonItem.style = UIBarButtonItemStyleDone;
237
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
238 239
    } else {
        self.editButtonItem.style = UIBarButtonItemStylePlain;
240
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
241 242 243
    }
}

244
- (IBAction)leftButtonAction:(id)sender
245
{
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    if (self.addMediaViewController == nil)
        self.addMediaViewController = [[VLCAddMediaViewController alloc] initWithNibName:@"VLCAddMediaViewController" bundle:nil];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
		self.addMediaViewController.contentSizeForViewInPopover = self.addMediaViewController.view.frame.size;
        if (self.addMediaPopoverController == nil) {
            self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.addMediaViewController];
            self.addMediaPopoverController.delegate = self;
        }

        if (self.addMediaPopoverController.popoverVisible)
            [self.addMediaPopoverController dismissPopoverAnimated:YES];
        else
            [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
                                                   permittedArrowDirections:UIPopoverArrowDirectionUp
                                                                   animated:YES];
    } else
        [self.navigationController presentViewController:self.addMediaViewController animated:YES completion:NULL];
264 265
}

266
/* deprecated in iOS 6 */
267 268
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
269 270 271
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
        return YES;

272 273 274 275
    if (_foundMedia.count > 0)
        return YES;
    else
        return NO;
276 277 278 279
}

/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
280 281 282 283
    if (_foundMedia.count > 0)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
284 285 286 287
}

/* introduced in iOS 6 */
- (BOOL)shouldAutorotate {
288 289 290 291
    if (_foundMedia.count > 0)
        return YES;
    else
        return NO;
292 293
}

294
#pragma mark - coin coin
295

296 297 298 299 300 301 302
- (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];
303 304
}

305

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