VLCPlaylistViewController.m 14.4 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"
15
#import "VLCMenuViewController.h"
Romain Goyet's avatar
Romain Goyet committed
16
#import "UINavigationController+Theme.h"
17
#import "NSString+SupportedMedia.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
18

19 20 21 22 23
@implementation EmptyLibraryView
@end

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

@implementation VLCPlaylistViewController

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

36 37 38 39 40 41 42 43 44 45
- (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;
46 47
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"padLibBg"]];
        _gridView.backgroundView = imageView;
48 49 50 51 52 53 54 55 56
        self.view = _gridView;
    }

    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
57 58 59 60
- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

78 79 80 81
    [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
82

83
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
84 85 86
        _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
        _gridView.alwaysBounceVertical = YES;
        _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
87 88
    } else {
        _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
89
        _tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
90
    }
91
    self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
92 93
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title"]];

94 95 96 97 98
    _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
99 100
}

101 102 103
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
104
    [self _displayEmptyLibraryViewIfNeeded];
105 106
}

107 108
- (void)viewDidAppear:(BOOL)animated
{
109
    [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.0];
110
    [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
111 112

    [super viewDidAppear:animated];
113 114 115 116 117 118 119 120
}

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

121 122
- (void)removeMediaObject:(MLFile *)mediaObject
{
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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];
140 141
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
    [self updateViewContents];
142 143 144 145 146 147 148
}

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

150
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
151
    } else {
152
        self.emptyLibraryView.frame = self.view.frame;
153
        [self.view addSubview:self.emptyLibraryView];
154
        self.navigationItem.rightBarButtonItem = nil;
155
    }
156 157 158 159 160

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

163
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
164
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
165
    _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
166

167
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
168
        [self.tableView reloadData];
169
    else {
170
        [self.gridView reloadData];
171
        [self.gridView setNeedsLayout];
172
    }
173 174

    [self _displayEmptyLibraryViewIfNeeded];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
175 176
}

177 178 179

#pragma mark - Table View

Felix Paul Kühne's avatar
Felix Paul Kühne committed
180 181 182 183 184 185 186 187 188 189 190 191 192
- (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
{
193
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
194

195
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
196
    if (cell == nil)
197
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
198

199 200
    NSInteger row = indexPath.row;
    cell.mediaObject = _foundMedia[row];
201

Felix Paul Kühne's avatar
Felix Paul Kühne committed
202 203 204
    return cell;
}

205 206 207 208 209
- (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
210 211 212 213 214 215 216
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

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

221
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
222
{
223
    MLFile *mediaObject = _foundMedia[indexPath.row];
224
    if (!self.movieViewController)
225
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
226

227 228
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
229 230
}

231 232
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
233
{
234
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
235 236
}

237
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
238
{
239
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
240

241
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
242
    if (cell == nil) {
243
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
244
        cell.selectionStyle = AQGridViewCellSelectionStyleNone;
245
        cell.gridView = gridView;
246 247
    }

248
    cell.mediaObject = _foundMedia[index];
249 250 251 252 253 254

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
255
    return [VLCPlaylistGridView preferredSize];
256 257 258 259
}

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

262
    MLFile *mediaObject = _foundMedia[index];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
263
    if (!self.movieViewController)
264
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
265

266 267
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
268 269
}

270 271 272 273 274 275
- (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
        [self removeMediaObject: _foundMedia[index]];
}

276
#pragma mark - UI implementation
277 278
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
279
    [super setEditing:editing animated:animated];
280

Felix Paul Kühne's avatar
Felix Paul Kühne committed
281
    UIBarButtonItem *editButton = self.editButtonItem;
282 283
    NSString *editImage = editing? @"doneButton": @"button";
    NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
284
    [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
285
                                 barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
286
    [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
287
                                   forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
288 289
    [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];

290
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
291
        [self.gridView setEditing:editing];
292
    else
293
        [self.tableView setEditing:editing animated:YES];
294 295
}

296
- (IBAction)leftButtonAction:(id)sender
297
{
Romain Goyet's avatar
Romain Goyet committed
298 299 300
    if (self.menuViewController == nil) {
        VLCMenuViewController *menuViewController = [[VLCMenuViewController alloc] initWithNibName:nil bundle:nil];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
Romain Goyet's avatar
Romain Goyet committed
301
        [navigationController loadTheme],
Romain Goyet's avatar
Romain Goyet committed
302
        self.menuViewController = navigationController;
303 304
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            navigationController.navigationBarHidden = YES;
Romain Goyet's avatar
Romain Goyet committed
305
    }
306 307

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
308
        self.menuViewController.contentSizeForViewInPopover = self.menuViewController.view.frame.size;
309
        if (self.addMediaPopoverController == nil) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
310
            self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.menuViewController];
311 312 313 314 315 316 317 318 319 320
            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
321
        [self.navigationController presentViewController:self.menuViewController animated:YES completion:NULL];
322 323
}

324
/* deprecated in iOS 6 */
325 326
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
327 328 329 330
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return YES;

    return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
331 332
}

333 334
// 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)
335 336
/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
337 338 339 340 341
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return UIInterfaceOrientationMaskAll;

    return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
                                    UIInterfaceOrientationMaskPortrait;
342 343 344 345
}

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

349
#pragma mark - coin coin
350

351 352 353
- (void)openMovieFromURL:(NSURL *)url
{
    if (!self.movieViewController)
354
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
355 356 357

    self.movieViewController.url = url;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
358 359
}

360

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