VLCServerBrowsingTVViewController.m 5.91 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"
Tobias's avatar
Tobias committed
13
#import "VLCServerBrowsingTVCell.h"
14 15
#import "VLCPlayerDisplayController.h"
#import "VLCPlaybackController.h"
16
#import "VLCServerBrowsingController.h"
17
#import "VLCMaskView.h"
18

19
@interface VLCServerBrowsingTVViewController ()
20
@property (nonatomic, readonly) id<VLCNetworkServerBrowser>serverBrowser;
21
@property (nonatomic) VLCServerBrowsingController *browsingController;
22 23
@end

24
@implementation VLCServerBrowsingTVViewController
25 26 27

- (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)serverBrowser
{
28
    self = [super initWithNibName:@"VLCServerBrowsingTVViewController" bundle:nil];
29 30 31
    if (self) {
        _serverBrowser = serverBrowser;
        serverBrowser.delegate = self;
32 33 34

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

35 36 37 38 39
        self.title = serverBrowser.title;
    }
    return self;
}

40

41 42
- (void)viewDidLoad {
    [super viewDidLoad];
43 44 45 46 47

    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    const CGFloat inset = 50;
    flowLayout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
    [self.collectionView registerNib:[UINib nibWithNibName:@"VLCServerBrowsingTVCell" bundle:nil] forCellWithReuseIdentifier:VLCServerBrowsingTVCellIdentifier];
Tobias's avatar
Tobias committed
48
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadData)];
49 50

    self.collectionView.maskView = [[VLCMaskView alloc] initWithFrame:self.collectionView.bounds];
51 52 53 54 55 56 57
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.serverBrowser update];
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    UICollectionView *collectionView = self.collectionView;
    VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
    maskView.maskEnd = self.topLayoutGuide.length * 0.8;

    /*
     Update the position from where the collection view's content should
     start to fade out. The size of the fade increases as the collection
     view scrolls to a maximum of half the navigation bar's height.
     */
    CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
    CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
    maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);

    /*
     Position the mask view so that it is always fills the visible area of
     the collection view.
     */
    CGSize collectionViewSize = self.collectionView.bounds.size;
    maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
    
}

85 86
#pragma mark -

Tobias's avatar
Tobias committed
87 88 89 90 91 92
- (void)reloadData {
    [self.serverBrowser update];
}

#pragma mark -

93 94
- (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
    self.title = networkBrowser.title;
95
    [self.collectionView reloadData];
96 97 98 99
}

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

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

#pragma mark -
106 107 108 109

- (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
{
    if (item.isContainer) {
110
        VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:item.containerBrowser];
111 112 113 114 115 116 117 118 119
        [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];
        }
120 121 122 123
    }
}


124 125 126 127
#pragma mark - collection view data source

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
128 129 130
    return [self.serverBrowser items].count;
}

131
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
132
{
133
    VLCServerBrowsingTVCell *cell = (VLCServerBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCServerBrowsingTVCellIdentifier forIndexPath:indexPath];
134 135 136
    return cell;
}

137 138 139 140 141 142 143 144 145 146 147
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
        id<VLCNetworkServerBrowserItem> item = self.serverBrowser.items[indexPath.row];

    if ([cell conformsToProtocol:@protocol(VLCServerBrowsingCell)]) {
        [self.browsingController configureCell:(id<VLCServerBrowsingCell>)cell withItem:item];
    }
}

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

151 152 153 154
    // 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];
155 156 157
}

@end