VLCPlaylistViewController.m 14.1 KB
Newer Older
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1 2 3 4 5 6 7
//
//  VLCMasterViewController.m
//  AspenProject
//
//  Created by Felix Paul Kühne on 27.02.13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//
8 9
//  Refer to the COPYING file of the official project for license.
//
Felix Paul Kühne's avatar
Felix Paul Kühne committed
10 11

#import "VLCPlaylistViewController.h"
12
#import "VLCMovieViewController.h"
13
#import "VLCPlaylistTableViewCell.h"
14
#import "VLCPlaylistGridView.h"
Romain Goyet's avatar
Romain Goyet committed
15
#import "UINavigationController+Theme.h"
16
#import "NSString+SupportedMedia.h"
17
#import "VLCBugreporter.h"
18
#import "VLCAppDelegate.h"
19
#import "UIBarButtonItem+Theme.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
20

21 22 23
@implementation EmptyLibraryView
@end

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

@implementation VLCPlaylistViewController

32 33 34
- (void)loadView {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
35
        _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
36 37 38 39 40 41 42
        _tableView.delegate = self;
        _tableView.dataSource = self;
        self.view = _tableView;
    } else {
        _gridView = [[AQGridView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _gridView.delegate = self;
        _gridView.dataSource = self;
43
        _gridView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"libraryBackground"]];
44 45 46
        self.view = _gridView;
    }

47 48
    _libraryMode = kVLCLibraryModeAllFiles;

49 50 51 52 53 54
    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
55 56 57 58
- (void)viewDidLoad
{
    [super viewDidLoad];

59
    UIBarButtonItem *addButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
60 61 62 63 64 65 66 67 68 69 70 71

    /* 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
72
    self.navigationItem.leftBarButtonItem = addButton;
73

74 75 76 77
    [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
78

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

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
- (void)viewDidAppear:(BOOL)animated
{
104
    [self performSelector:@selector(reloadContents) withObject:nil afterDelay:.0];
105
    [[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 119 120 121 122 123 124 125 126
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
        [[VLCBugreporter sharedInstance] handleBugreportRequest];
}

127 128
- (void)removeMediaObject:(MLFile *)mediaObject
{
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
    NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
    NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
    NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
       return ([obj rangeOfString:fileName].location != NSNotFound);
    }];
    unsigned int count = indexSet.count;
    NSString *additionalFilePath;
    NSUInteger currentIndex = [indexSet firstIndex];
    for (unsigned int x = 0; x < count; x++) {
        additionalFilePath = allfiles[currentIndex];
        if ([additionalFilePath isSupportedSubtitleFormat])
            [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
        currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
    }
    [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
146
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
147
    [self reloadContents];
148 149 150 151 152 153 154
}

- (void)_displayEmptyLibraryViewIfNeeded
{
    if (_foundMedia.count > 0) {
        if (self.emptyLibraryView.superview)
            [self.emptyLibraryView removeFromSuperview];
155

156
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
157
    } else {
158
        self.emptyLibraryView.frame = self.view.frame;
159
        [self.view addSubview:self.emptyLibraryView];
160
        self.navigationItem.rightBarButtonItem = nil;
161
    }
162 163 164 165 166

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

169
- (void)reloadContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
170
{
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    if (_libraryMode == kVLCLibraryModeAllAlbums) {
        NSArray *rawAlbums = [MLAlbum allAlbums];
        _foundMedia = [[NSMutableArray alloc] init];
        NSUInteger count = rawAlbums.count;
        MLAlbum *album;

        for (NSUInteger x = 0; x < count; x++) {
            album = rawAlbums[x];
            if (album.name.length > 0 && album.tracks.count > 0)
                [_foundMedia addObject:album];
        }
        rawAlbums = nil;
    } else
        _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];

    [self updateViewContents];
}
188

189 190
- (void)updateViewContents
{
191
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
192
        [self.tableView reloadData];
193
    else {
194
        [self.gridView reloadData];
195
        [self.gridView setNeedsLayout];
196
    }
197 198

    [self _displayEmptyLibraryViewIfNeeded];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
199 200
}

201 202
#pragma mark - Table View

Felix Paul Kühne's avatar
Felix Paul Kühne committed
203 204 205 206 207 208 209 210 211 212 213 214 215
- (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
{
216
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
217

218
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
219
    if (cell == nil)
220
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
221

222 223
    NSInteger row = indexPath.row;
    cell.mediaObject = _foundMedia[row];
224

Felix Paul Kühne's avatar
Felix Paul Kühne committed
225 226 227
    return cell;
}

228 229 230 231 232
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
233 234 235 236 237 238 239
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

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

244
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
245
{
246 247 248 249 250 251 252 253
    NSManagedObject *currentObject = _foundMedia[indexPath.row];
    if ([currentObject isKindOfClass:[MLAlbum class]]) {
        _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)currentObject tracks] allObjects]];
        NSLog(@"current item is an album with %i kids", _foundMedia.count);
        [self updateViewContents];
    } else {
        if (!self.movieViewController)
            self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
254

255 256 257 258 259 260 261
        if ([currentObject isKindOfClass:[MLFile class]])
            self.movieViewController.mediaItem = (MLFile *)currentObject;
        else
            self.movieViewController.mediaItem = [(MLAlbumTrack*)currentObject files].anyObject;

        [self.navigationController pushViewController:self.movieViewController animated:YES];
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
262 263
}

264 265
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
266
{
267
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
268 269
}

270
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
271
{
272
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
273

274
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
275
    if (cell == nil) {
276
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
277
        cell.selectionStyle = AQGridViewCellSelectionStyleNone;
278
        cell.gridView = gridView;
279 280
    }

281
    cell.mediaObject = _foundMedia[index];
282 283 284 285 286 287

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
288
    return [VLCPlaylistGridView preferredSize];
289 290 291 292
}

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

295
    MLFile *mediaObject = _foundMedia[index];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
296
    if (!self.movieViewController)
297
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
298

299 300
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
301 302
}

303 304 305 306 307 308
- (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
        [self removeMediaObject: _foundMedia[index]];
}

309
#pragma mark - UI implementation
310 311
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
312
    [super setEditing:editing animated:animated];
313

Felix Paul Kühne's avatar
Felix Paul Kühne committed
314
    UIBarButtonItem *editButton = self.editButtonItem;
315 316
    NSString *editImage = editing? @"doneButton": @"button";
    NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
317
    [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
318
                                 barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
319
    [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
320
                                   forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
321 322
    [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];

323
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
324
        [self.gridView setEditing:editing];
325
    else
326
        [self.tableView setEditing:editing animated:YES];
327 328
}

329
- (IBAction)leftButtonAction:(id)sender
330
{
331
    [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
332 333
}

334
/* deprecated in iOS 6 */
335 336
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
337 338 339 340
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return YES;

    return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
341 342
}

343 344
// 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)
345 346
/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
347 348 349 350 351
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return UIInterfaceOrientationMaskAll;

    return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
                                    UIInterfaceOrientationMaskPortrait;
352 353 354 355
}

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

359
#pragma mark - coin coin
360

361 362 363
- (void)openMovieFromURL:(NSURL *)url
{
    if (!self.movieViewController)
364
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
365

366 367 368
    if (self.navigationController.topViewController != self.movieViewController)
        [self.navigationController pushViewController:self.movieViewController animated:YES];

369
    self.movieViewController.url = url;
370 371
}

372 373 374 375 376 377
- (void)setLibraryMode:(NSUInteger)mode
{
    _libraryMode = mode;
    [self reloadContents];
}

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