VLCServerBrowsingTVViewController.m 5.73 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 41 42 43 44 45 46 47
        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
48 49 50 51 52 53 54
                                                                       attribute:NSLayoutAttributeCenterY
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:self.view
                                                                       attribute:NSLayoutAttributeCenterY
                                                                      multiplier:1.0
                                                                        constant:0.0];
        [self.view addConstraint:yConstraint];
55
        NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
56 57 58 59 60 61 62
                                                                       attribute:NSLayoutAttributeCenterX
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:self.view
                                                                       attribute:NSLayoutAttributeCenterX
                                                                      multiplier:1.0
                                                                        constant:0.0];
        [self.view addConstraint:xConstraint];
63 64 65 66
    }
    return self;
}

67 68
- (void)viewDidAppear:(BOOL)animated
{
69 70 71 72 73 74
    [super viewDidAppear:animated];
    [self.serverBrowser update];
}

#pragma mark -

75 76
- (void)reloadData
{
Tobias's avatar
Tobias committed
77 78 79 80 81
    [self.serverBrowser update];
}

#pragma mark -

82 83
- (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
{
84
    self.title = networkBrowser.title;
85
    [self.collectionView reloadData];
86
    _nothingFoundLabel.hidden = [self.serverBrowser items].count > 0;
87 88 89 90
}

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

91 92 93
    [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
                         message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
                     buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
94 95 96
}

#pragma mark -
97 98 99 100

- (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
{
    if (item.isContainer) {
101
        VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:item.containerBrowser];
102 103 104 105 106 107 108 109 110
        [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];
        }
111 112 113 114
    }
}


115 116 117 118
#pragma mark - collection view data source

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
119 120 121
    return [self.serverBrowser items].count;
}

122 123 124 125
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
        id<VLCNetworkServerBrowserItem> item = self.serverBrowser.items[indexPath.row];

126 127
    if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
        [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
128 129 130 131 132
    }
}

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

136 137 138 139
    // would make sence if item came from search which isn't
    // currently the case on the TV
    const BOOL singlePlayback = NO;
    [self didSelectItem:item index:row singlePlayback:singlePlayback];
140 141 142
}

@end