VLCServerBrowsingTVViewController.m 6.21 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

19
@interface VLCServerBrowsingTVViewController ()
20 21 22
{
    UILabel *_nothingFoundLabel;
}
23
@property (nonatomic, readonly) id<VLCNetworkServerBrowser>serverBrowser;
24
@property (nonatomic) VLCServerBrowsingController *browsingController;
25 26
@end

27
@implementation VLCServerBrowsingTVViewController
28 29 30

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

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

38
        self.title = serverBrowser.title;
39 40

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

45 46 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
- (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];
}

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

#pragma mark -

82 83
- (void)reloadData
{
Tobias's avatar
Tobias committed
84 85 86 87 88
    [self.serverBrowser update];
}

#pragma mark -

89 90
- (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
{
91
    self.title = networkBrowser.title;
92
    [self.collectionView reloadData];
93
    _nothingFoundLabel.hidden = [self.serverBrowser items].count > 0;
94 95 96 97
}

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

98 99 100
    [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
                         message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
                     buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
101 102 103
}

#pragma mark -
104 105 106 107

- (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
{
    if (item.isContainer) {
108
        VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:item.containerBrowser];
109 110 111 112 113 114 115 116 117
        [self.navigationController pushViewController:targetViewController animated:YES];
    } else {
        if (singlePlayback) {
            [self.browsingController streamFileForItem:item];
        } else {
            VLCMediaList *mediaList = self.serverBrowser.mediaList;
            [self.browsingController configureSubtitlesInMediaList:mediaList];
            [self.browsingController streamMediaList:mediaList startingAtIndex:index];
        }
118 119 120 121
    }
}


122 123 124 125
#pragma mark - collection view data source

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
126 127 128
    NSInteger count = [self.serverBrowser items].count;
    self.nothingFoundView.hidden = count > 0;
    return count;
129 130
}

131 132
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
133 134 135 136
    NSArray *items = self.serverBrowser.items;
    NSInteger row = indexPath.row;
    if (row < items.count) {
        id<VLCNetworkServerBrowserItem> item = items[row];
137

138 139 140 141
        if([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
            ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
        }

142 143 144
        if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
            [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
        }
145 146 147 148 149
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
150 151
    NSInteger row = indexPath.row;
    id<VLCNetworkServerBrowserItem> item = self.serverBrowser.items[row];
152

153 154
    // would make sence if item came from search which isn't
    // currently the case on the TV
155
    const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
156
    [self didSelectItem:item index:row singlePlayback:singlePlayback];
157 158 159
}

@end