VLCServerBrowsingTVViewController.m 7.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
11

12
#import "VLCServerBrowsingTVViewController.h"
13
#import "VLCRemoteBrowsingTVCell.h"
14 15
#import "VLCPlayerDisplayController.h"
#import "VLCPlaybackController.h"
16
#import "VLCServerBrowsingController.h"
17
#import "VLCMaskView.h"
18
#import "GRKArrayDiff+UICollectionView.h"
19

20
@interface VLCServerBrowsingTVViewController ()
21 22 23
{
    UILabel *_nothingFoundLabel;
}
24
@property (nonatomic) VLCServerBrowsingController *browsingController;
25
@property (nonatomic) NSArray<id <VLCNetworkServerBrowserItem>> *items;
26 27
@end

28
@implementation VLCServerBrowsingTVViewController
29
@synthesize subdirectoryBrowserClass = _subdirectoryBrowserClass;
30 31 32

- (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)serverBrowser
{
33
    self = [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
34 35 36
    if (self) {
        _serverBrowser = serverBrowser;
        serverBrowser.delegate = self;
37 38 39

        _browsingController = [[VLCServerBrowsingController alloc] initWithViewController:self serverBrowser:serverBrowser];

40
        self.title = serverBrowser.title;
41 42

        self.downloadArtwork = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingDownloadArtwork];
43 44 45 46
    }
    return self;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.nothingFoundLabel.text = NSLocalizedString(@"FOLDER_EMPTY", nil);
    [self.nothingFoundLabel sizeToFit];
    UIView *nothingFoundView = self.nothingFoundView;
    [nothingFoundView sizeToFit];
    [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:nothingFoundView];

    NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
                                                                   attribute:NSLayoutAttributeCenterY
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeCenterY
                                                                  multiplier:1.0
                                                                    constant:0.0];
    [self.view addConstraint:yConstraint];
    NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
                                                                   attribute:NSLayoutAttributeCenterX
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeCenterX
                                                                  multiplier:1.0
                                                                    constant:0.0];
    [self.view addConstraint:xConstraint];
}

76 77
- (void)viewDidAppear:(BOOL)animated
{
78 79 80 81
    [super viewDidAppear:animated];
    [self.serverBrowser update];
}

82 83 84 85 86 87 88 89 90 91 92
- (void)setSubdirectoryBrowserClass:(Class)subdirectoryBrowserClass
{
    NSParameterAssert([subdirectoryBrowserClass isSubclassOfClass:[VLCServerBrowsingTVViewController class]]);
    _subdirectoryBrowserClass = subdirectoryBrowserClass;
}

- (Class)subdirectoryBrowserClass
{
    return _subdirectoryBrowserClass ?: [self class];
}

93 94
#pragma mark -

95 96
- (void)reloadData
{
Tobias's avatar
Tobias committed
97 98 99
    [self.serverBrowser update];
}

100
#pragma mark - VLCNetworkServerBrowserDelegate
Tobias's avatar
Tobias committed
101

102 103
- (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
{
104
    self.title = networkBrowser.title;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    NSArray *oldItems = self.items;
    NSArray *newItems = networkBrowser.items;
    GRKArrayDiff *diff = [[GRKArrayDiff alloc] initWithPreviousArray:oldItems
                                                        currentArray:newItems
                                                       identityBlock:^NSString * _Nullable(id <VLCNetworkServerBrowserItem> item) {
                                                           return [NSString stringWithFormat:@"%@#%@",item.URL.absoluteString ?: @"", item.name];
                                                       }
                                                       modifiedBlock:nil];

    [diff performBatchUpdatesWithCollectionView:self.collectionView
                                        section:0
                               dataSourceUpdate:^{
                                   self.items = newItems;
                               } completion:nil];

    _nothingFoundLabel.hidden = self.items.count > 0;
122 123 124 125
}

- (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {

126 127 128
    [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
                         message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
                     buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
129 130 131
}

#pragma mark -
132 133 134 135

- (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
{
    if (item.isContainer) {
136 137
        VLCServerBrowsingTVViewController *targetViewController = [[self.subdirectoryBrowserClass alloc] initWithServerBrowser:item.containerBrowser];
        [self showViewController:targetViewController sender:self];
138 139 140 141 142 143 144 145
    } else {
        if (singlePlayback) {
            [self.browsingController streamFileForItem:item];
        } else {
            VLCMediaList *mediaList = self.serverBrowser.mediaList;
            [self.browsingController configureSubtitlesInMediaList:mediaList];
            [self.browsingController streamMediaList:mediaList startingAtIndex:index];
        }
146 147 148 149
    }
}


150 151 152 153
#pragma mark - collection view data source

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
154
    NSInteger count = self.items.count;
155 156
    self.nothingFoundView.hidden = count > 0;
    return count;
157 158
}

159 160
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
161
    NSArray *items = self.items;
162 163 164
    NSInteger row = indexPath.row;
    if (row < items.count) {
        id<VLCNetworkServerBrowserItem> item = items[row];
165

166
        if ([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
167 168 169
            ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
        }

170 171 172
        if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
            [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
        }
173 174 175 176 177
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
178
    NSInteger row = indexPath.row;
179
    id<VLCNetworkServerBrowserItem> item = self.items[row];
180

181 182
    // would make sence if item came from search which isn't
    // currently the case on the TV
183
    const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
184
    [self didSelectItem:item index:row singlePlayback:singlePlayback];
185 186 187
}

@end