VLCPlaylistViewController.m 9.64 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 33 34
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

35
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"vlc"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
36 37 38 39 40 41 42 43 44 45 46 47

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

48
    self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT", @"");
Felix Paul Kühne's avatar
Felix Paul Kühne committed
49 50 51
    self.navigationItem.leftBarButtonItem = addButton;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

52
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
53 54 55
        _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
        _gridView.alwaysBounceVertical = YES;
        _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
56 57 58
    } else {
        _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
        _tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
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
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
71
    [self _displayEmptyLibraryViewIfNeeded];
72 73
}

74 75 76 77
- (void)viewDidAppear:(BOOL)animated
{
    [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
    [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
78 79

    [super viewDidAppear:animated];
80 81 82 83 84 85 86 87
}

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

88 89 90
- (void)removeMediaObject:(MLFile *)mediaObject
{
    [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
91 92

    [self updateViewContents];
93 94 95 96 97 98 99 100
}

- (void)_displayEmptyLibraryViewIfNeeded
{
    if (_foundMedia.count > 0) {
        if (self.emptyLibraryView.superview)
            [self.emptyLibraryView removeFromSuperview];
    } else {
101
        self.emptyLibraryView.frame = self.view.frame;
102 103
        [self.view addSubview:self.emptyLibraryView];
    }
104 105 106 107 108

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

111

112
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
113
{
114 115
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

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

118
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
119
        [self.tableView reloadData];
120
    else {
121
        [self.gridView reloadData];
122 123
        [self.gridView setNeedsDisplay];
    }
124 125

    [self _displayEmptyLibraryViewIfNeeded];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
126 127
}

128 129 130

#pragma mark - Table View

Felix Paul Kühne's avatar
Felix Paul Kühne committed
131 132 133 134 135 136 137 138 139 140 141 142 143
- (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
{
144
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
145

146
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
147
    if (cell == nil)
148
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
149

150 151
    cell.mediaObject = _foundMedia[indexPath.row];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
152 153 154 155 156 157 158 159 160 161
    return cell;
}

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

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

166
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
167
{
168
    MLFile *mediaObject = _foundMedia[indexPath.row];
169
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
170
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
171

172 173
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
174 175
}

176 177
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
178
{
179
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
180 181
}

182
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
183
{
184
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
185

186
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
187
    if (cell == nil) {
188
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
189
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
190 191
    }

192
    cell.mediaObject = _foundMedia[index];
193 194 195 196 197 198

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
199
    return [VLCPlaylistGridView preferredSize];
200 201 202 203
}

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

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

210 211
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
212 213
}

214
#pragma mark - UI implementation
215 216
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
217 218 219 220 221
    if (_editMode != editing)
        _editMode = editing;
    else
        _editMode = !editing;

222
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
223
        [self.gridView setEditing:_editMode];
224
    else
225 226 227
        [self.tableView setEditing:_editMode animated:YES];

    if (_editMode) {
228
        self.editButtonItem.style = UIBarButtonItemStyleDone;
229
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
230 231
    } else {
        self.editButtonItem.style = UIBarButtonItemStylePlain;
232
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
233 234 235
    }
}

236
- (IBAction)leftButtonAction:(id)sender
237
{
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    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];
256 257
}

258
/* deprecated in iOS 6 */
259 260
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
261
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (_foundMedia.count > 0);
262 263 264 265
}

/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
266
    return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAll: UIInterfaceOrientationMaskPortrait;
267 268 269 270
}

/* introduced in iOS 6 */
- (BOOL)shouldAutorotate {
271
    return (_foundMedia.count > 0);
272 273
}

274
#pragma mark - coin coin
275

276 277 278 279 280 281 282
- (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];
283 284
}

285

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