VLCPlaylistViewController.m 10.2 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 "VLCAboutViewController.h"
14
#import "VLCPasscodeLockViewController.h"
15
#import "VLCAddMediaViewController.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
16 17 18 19 20 21 22 23 24 25 26

@interface VLCPlaylistViewController () {
    NSMutableArray *_foundMedia;
}
@end

@implementation VLCPlaylistViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
27
    if (self)
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
28
        self.title = @"VLC";
29

Felix Paul Kühne's avatar
Felix Paul Kühne committed
30 31 32 33 34
    return self;
}

- (void)viewDidLoad
{
35 36
    self.tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
    self.tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
37 38
    [super viewDidLoad];

39
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"vlc"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
40 41 42
    self.navigationItem.leftBarButtonItem = addButton;

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

45 46 47 48
    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
49
    }
50

51 52 53 54 55
    self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
    self.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
    self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
    [self.emptyLibraryLongDescriptionLabel sizeToFit];
56 57

    self.passcodeLockViewController = [[VLCPasscodeLockViewController alloc] initWithNibName:@"VLCPasscodeLockViewController" bundle:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
58 59
}

60 61 62 63
- (void)viewWillAppear:(BOOL)animated
{
    [self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:animated];
    [super viewWillAppear:animated];
64

65 66 67 68 69
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        self.tableView.hidden = YES;
    else
        self.gridView.hidden = YES;

70
    [self _displayEmptyLibraryViewIfNeeded];
71 72
}

73 74
- (void)viewDidAppear:(BOOL)animated
{
75 76 77 78 79
    [self validatePasscode];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        self.tableView.hidden = NO;
    else
        self.gridView.hidden = NO;
80

81 82
    [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
    [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
83 84

    [super viewDidAppear:animated];
85 86 87 88 89 90 91 92
}

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

93 94 95
- (void)removeMediaObject:(MLFile *)mediaObject
{
    [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
96 97

    [self updateViewContents];
98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

- (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];
    }
112 113
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
- (void)validatePasscode
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([[defaults objectForKey:@"Passcode"] isEqualToString:@""]) {
        self.passcodeValidated = YES;
        return;
    }

    if (!self.passcodeValidated) {
        if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate)
            [self.navigationController pushViewController:self.passcodeLockViewController animated:YES];
        else
            self.passcodeValidated = YES;
    }
}

130
- (void)updateViewContents
Felix Paul Kühne's avatar
Felix Paul Kühne committed
131
{
132 133
    [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];

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

136 137
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        [self.tableView reloadData];
138
    else {
139
        [self.gridView reloadData];
140 141
        [self.gridView setNeedsDisplay];
    }
142 143

    [self _displayEmptyLibraryViewIfNeeded];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
144 145
}

146 147 148

#pragma mark - Table View

Felix Paul Kühne's avatar
Felix Paul Kühne committed
149 150 151 152 153 154 155 156 157 158 159 160 161
- (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
{
162
    static NSString *CellIdentifier = @"PlaylistCell";
Felix Paul Kühne's avatar
Felix Paul Kühne committed
163

164
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
165
    if (cell == nil)
166
        cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
167

168 169
    cell.mediaObject = _foundMedia[indexPath.row];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
170 171 172 173 174 175 176 177 178 179
    return cell;
}

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

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

184
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
185
{
186
    MLFile *mediaObject = _foundMedia[indexPath.row];
187
    if (!self.movieViewController)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
188
        self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
189

190 191
    self.movieViewController.mediaItem = mediaObject;
    [self.navigationController pushViewController:self.movieViewController animated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
192 193
}

194 195
#pragma mark - AQGridView
- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
196
{
197
    return _foundMedia.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
198 199
}

200
- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
Felix Paul Kühne's avatar
Felix Paul Kühne committed
201
{
202
    static NSString *AQCellIdentifier = @"AQPlaylistCell";
203

204
    VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
205
    if (cell == nil) {
206
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
207
        cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
208 209
    }

210
    cell.mediaObject = _foundMedia[index];
211 212 213 214 215 216

    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
217
    return [VLCPlaylistGridView preferredSize];
218 219 220 221 222
}

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

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

230
#pragma mark - UI implementation
231 232
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
233 234 235 236 237
    if (_editMode != editing)
        _editMode = editing;
    else
        _editMode = !editing;

238
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
239
        [self.gridView setEditing:_editMode];
240
    else
241 242 243
        [self.tableView setEditing:_editMode animated:YES];

    if (_editMode) {
244
        self.editButtonItem.style = UIBarButtonItemStyleDone;
245
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
246 247
    } else {
        self.editButtonItem.style = UIBarButtonItemStylePlain;
248
        self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
249 250 251
    }
}

252
- (IBAction)leftButtonAction:(id)sender
253
{
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    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];
272 273
}

274
/* deprecated in iOS 6 */
275 276
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
277 278 279
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
        return YES;

280 281 282 283
    if (_foundMedia.count > 0)
        return YES;
    else
        return NO;
284 285 286 287
}

/* introduced in iOS 6 */
- (NSUInteger)supportedInterfaceOrientations {
288 289 290 291
    if (_foundMedia.count > 0)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
292 293 294 295
}

/* introduced in iOS 6 */
- (BOOL)shouldAutorotate {
296 297 298 299
    if (_foundMedia.count > 0)
        return YES;
    else
        return NO;
300 301
}

302
#pragma mark - coin coin
303

304 305 306 307 308 309 310
- (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];
311 312
}

313

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