VLCPlaylistInterfaceController.m 5.43 KB
Newer Older
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1
/*****************************************************************************
2
 * VLCPlaylistInterfaceController.m
Felix Paul Kühne's avatar
Felix Paul Kühne committed
3
4
5
6
7
8
9
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *          Carola Nitz <caro # videolan.org>
10
 *          Felix Paul Kühne <fkuehne # videolan.org>
Felix Paul Kühne's avatar
Felix Paul Kühne committed
11
12
13
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
Carola's avatar
Carola committed
14

15
#import "VLCPlaylistInterfaceController.h"
Tobias's avatar
Tobias committed
16
17
18
#import <MediaLibraryKit/MediaLibraryKit.h>
#import "VLCRowController.h"
#import <MobileVLCKit/VLCTime.h>
19
20

#import "VLCNotificationRelay.h"
21
#import "VLCWatchTableController.h"
22
#import "NSManagedObjectContext+refreshAll.h"
23
#import "MLMediaLibrary+playlist.h"
Carola's avatar
Carola committed
24

Tobias's avatar
Tobias committed
25
static NSString *const rowType = @"mediaRow";
26
27
static NSString *const VLCDBUpdateNotification = @"VLCUpdateDataBase";
static NSString *const VLCDBUpdateNotificationRemote = @"org.videolan.ios-app.dbupdate";
Carola's avatar
Carola committed
28

29
@interface VLCPlaylistInterfaceController()
30
31
32
33
34
{
    CGRect _thumbnailSize;
    CGFloat _rowWidth;
}

35
@property (nonatomic, strong) VLCWatchTableController *tableController;
36
@property (nonatomic) VLCLibraryMode libraryMode;
37
38
@property (nonatomic) id groupObject;

Carola's avatar
Carola committed
39
40
@end

41
@implementation VLCPlaylistInterfaceController
Carola's avatar
Carola committed
42
43
44

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
Tobias's avatar
Tobias committed
45
46

    MLMediaLibrary *mediaLibrary = [MLMediaLibrary sharedMediaLibrary];
47
    mediaLibrary.additionalPersitentStoreOptions = @{NSReadOnlyPersistentStoreOption : @YES};
Tobias's avatar
Tobias committed
48

49
50
51
52
53
54
55
56
    if (context == nil) {
        self.libraryMode = VLCLibraryModeAllFiles;
        [self setupMenuButtons];
        self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
        self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", nil);
    } else {
        self.groupObject = context;
        self.title = [self.groupObject name];
57
        self.libraryMode = VLCLibraryModeNone;
58
    }
Tobias's avatar
Tobias committed
59
    [self addNowPlayingMenu];
Tobias's avatar
Tobias committed
60

61
    [[VLCNotificationRelay sharedRelay] addRelayRemoteName:VLCDBUpdateNotificationRemote toLocalName:VLCDBUpdateNotification];
62

63
    /* setup table view controller */
64
65
66
67
    VLCWatchTableController *tableController = [[VLCWatchTableController alloc] init];
    tableController.table = self.table;
    tableController.previousPageButton = self.previousButton;
    tableController.nextPageButton = self.nextButton;
Tobias's avatar
Tobias committed
68
    tableController.emptyLibraryInterfaceObjects = self.emptyLibraryGroup;
Tobias's avatar
Tobias committed
69
    tableController.pageSize = 20;
70
71
72
    tableController.rowType = rowType;

    tableController.configureRowControllerWithObjectBlock = ^(id controller, id object) {
73
74
75
        if ([controller respondsToSelector:@selector(configureWithMediaLibraryObject:)]) {
            [controller configureWithMediaLibraryObject:object];
        }
76
    };
77
    self.tableController = tableController;
78
79

    [self updateData];
Carola's avatar
Carola committed
80
81
}

82
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
83
    id object = self.tableController.displayedObjects[rowIndex];
84
    if ([object isKindOfClass:[MLAlbum class]] || [object isKindOfClass:[MLLabel class]] || [object isKindOfClass:[MLShow class]]) {
Carola's avatar
Carola committed
85

86
        [self pushControllerWithName:@"tableViewController" context:object];
Carola's avatar
Carola committed
87
        [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(self.libraryMode), @"folder":((NSManagedObject *)object).objectID.URIRepresentation} webpageURL:nil];
88
    } else {
89
90
        [self pushControllerWithName:@"detailInfo" context:object];
    }
91
92
}

93
94
95
96
97
98
99
100
- (IBAction)nextPagePressed {
    [self.tableController nextPageButtonPressed];
}

- (IBAction)previousPagePressed {
    [self.tableController previousPageButtonPressed];
}

101
102
103
104
- (void)setupMenuButtons {

    [self addMenuItemWithImageNamed:@"AllFiles" title: NSLocalizedString(@"LIBRARY_ALL_FILES", nil) action:@selector(switchToAllFiles)];
    [self addMenuItemWithImageNamed:@"MusicAlbums" title: NSLocalizedString(@"LIBRARY_MUSIC", nil) action:@selector(switchToMusic)];
105
    [self addMenuItemWithImageNamed:@"TVShows" title: NSLocalizedString(@"LIBRARY_SERIES", nil) action:@selector(switchToSeries)];
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}

- (void)switchToAllFiles{
    self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
    self.libraryMode = VLCLibraryModeAllFiles;
    [self updateData];
}

- (void)switchToMusic{
    self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
    self.libraryMode = VLCLibraryModeAllAlbums;
    [self updateData];
}

- (void)switchToSeries{
    self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
    self.libraryMode = VLCLibraryModeAllSeries;
    [self updateData];
}

126
127
#pragma mark - data handling

128
- (void)updateData {
129
130
131
132
    [super updateData];
    NSManagedObjectContext *moc = [(NSManagedObject *)self.tableController.objects.firstObject managedObjectContext];
    [moc vlc_refreshAllObjectsMerge:NO];
    self.tableController.objects = [self mediaArray];
Tobias's avatar
Tobias committed
133
134
}

Carola's avatar
Carola committed
135

Carola's avatar
Carola committed
136
137
138
- (void)setLibraryMode:(VLCLibraryMode)libraryMode
{
    //should also handle diving into a folder
Carola's avatar
Carola committed
139
    [self updateUserActivity:@"org.videolan.vlc-ios.librarymode" userInfo:@{@"state" : @(libraryMode)} webpageURL:nil];
Carola's avatar
Carola committed
140
141
142
    _libraryMode = libraryMode;
}

143
144
145
146
147
148
149
150
151
152
- (NSArray *)mediaArray
{
    id groupObject = self.groupObject;
    if (groupObject) {
        return [[MLMediaLibrary sharedMediaLibrary] playlistArrayForGroupObject:groupObject];
    } else {
        return [[MLMediaLibrary sharedMediaLibrary] playlistArrayForLibraryMode:self.libraryMode];
    }
}

Carola's avatar
Carola committed
153
@end