VLCTrackSelectorView.m 9.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*****************************************************************************
 * VLCTrackSelectorView.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2017 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <caro # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCTrackSelectorView.h"

#import "VLCPlaybackController.h"
#import "VLCTrackSelectorHeaderView.h"
#import "VLCTrackSelectorTableViewCell.h"

#import "UIDevice+VLC.h"

#define TRACK_SELECTOR_TABLEVIEW_CELL @"track selector table view cell"
#define TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER @"track selector table view section header"

@interface VLCTrackSelectorView() <UITableViewDataSource, UITableViewDelegate>
{
    UITableView *_trackSelectorTableView;
    NSLayoutConstraint *_heightConstraint;
}
@end

@implementation VLCTrackSelectorView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
36
    if (self) {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        _trackSelectorTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _trackSelectorTableView.delegate = self;
        _trackSelectorTableView.dataSource = self;
        _trackSelectorTableView.separatorColor = [UIColor clearColor];
        _trackSelectorTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        _trackSelectorTableView.rowHeight = 44.;
        _trackSelectorTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _trackSelectorTableView.sectionHeaderHeight = 28.;
        [_trackSelectorTableView registerClass:[VLCTrackSelectorTableViewCell class] forCellReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL];
        [_trackSelectorTableView registerClass:[VLCTrackSelectorHeaderView class] forHeaderFooterViewReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
        _trackSelectorTableView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_trackSelectorTableView];
        [self setupConstraints];
        [self configureForDeviceCategory];
    }
    return self;
}

55 56
- (void)configureForDeviceCategory
{
57 58
    _trackSelectorTableView.opaque = NO;
    _trackSelectorTableView.backgroundColor = [UIColor clearColor];
59 60
    _trackSelectorTableView.allowsMultipleSelection = YES;
}
61

62 63 64 65 66 67 68 69 70
- (void)layoutSubviews
{
    CGFloat height = _trackSelectorTableView.contentSize.height;
    _heightConstraint.constant = height;
    [super layoutSubviews];
}

- (void)setupConstraints
{
71
    _heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44];
72 73 74 75 76 77 78 79 80 81 82 83 84 85
    _heightConstraint.priority = UILayoutPriorityDefaultHigh;
    NSArray *constraints = @[
                             [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
                             [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
                             [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
                             [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0],
                             _heightConstraint,
                             ];
    [NSLayoutConstraint activateConstraints:constraints];
}
#pragma mark - track selector table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
86 87
    NSInteger sections = 0;
    VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
88 89

    if (_switchingTracksNotChapters) {
90 91
        if([playbackController numberOfAudioTracks] > 2)
            sections++;
92

93 94
        if ([playbackController numberOfVideoSubtitlesIndexes] > 1)
            sections++;
95
    } else {
96 97
        if ([playbackController numberOfTitles] > 1)
            sections++;
98

99 100
        if ([playbackController numberOfChaptersForCurrentTitle] > 1)
            sections++;
101 102
    }

103
    return sections;
104 105 106 107 108
}

- (void)updateView
{
    [_trackSelectorTableView reloadData];
109 110
    [self setNeedsLayout];
    [self layoutIfNeeded];
111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];

    if (!view) {
        view = [[VLCTrackSelectorHeaderView alloc] initWithReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
    }
    return view;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
125
    VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
126

127 128
    if (_switchingTracksNotChapters) {
        if ([playbackController numberOfAudioTracks] > 2 && section == 0)
129 130
            return NSLocalizedString(@"CHOOSE_AUDIO_TRACK", nil);

131
        if ([playbackController numberOfVideoSubtitlesIndexes] > 1)
132 133
            return NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", nil);
    } else {
134
        if ([playbackController numberOfTitles] > 1 && section == 0)
135 136
            return NSLocalizedString(@"CHOOSE_TITLE", nil);

137
        if ([playbackController numberOfChaptersForCurrentTitle] > 1)
138 139 140 141 142 143 144 145
            return NSLocalizedString(@"CHOOSE_CHAPTER", nil);
    }

    return @"unknown track type";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
146
    VLCTrackSelectorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL forIndexPath:indexPath];
147 148 149

    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
150
    VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
151 152 153

    if (_switchingTracksNotChapters) {
        NSString *trackName;
154 155
        if ([playbackController numberOfAudioTracks] > 2 && section == 0) {
            if ([playbackController indexOfCurrentAudioTrack] == row) {
156
                [cell setShowsCurrentTrack];
157 158
            }

159 160 161
            trackName = [playbackController audioTrackNameAtIndex:row];
        } else {
            if ([playbackController indexOfCurrentSubtitleTrack] == row) {
162
                [cell setShowsCurrentTrack];
163
            }
164 165

            trackName = [playbackController videoSubtitleNameAtIndex:row];
166 167
        }

168 169 170 171
        if ([trackName isEqualToString:@"Disable"]) {
            cell.textLabel.text = NSLocalizedString(@"DISABLE_LABEL", nil);
        } else {
            cell.textLabel.text = trackName;
172 173
        }
    } else {
174 175 176 177
        if ([playbackController numberOfTitles] > 1 && section == 0) {

            NSDictionary *description = [playbackController titleDescriptionsDictAtIndex:row];
            if(description != nil) {
178 179 180
                cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCTitleDescriptionName], [[VLCTime timeWithNumber:description[VLCTitleDescriptionDuration]] stringValue]];
            }

181
            if (row == [playbackController indexOfCurrentTitle]) {
182
                [cell setShowsCurrentTrack];
183
            }
184
        } else {
185 186
            NSDictionary *description = [playbackController chapterDescriptionsDictAtIndex:row];
            if (description != nil)
187 188
                cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCChapterDescriptionName], [[VLCTime timeWithNumber:description[VLCChapterDescriptionDuration]] stringValue]];
        }
189 190 191

        if (row == [playbackController indexOfCurrentChapter])
            [cell setShowsCurrentTrack];
192 193 194 195 196 197 198
    }

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
199
    VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
200

201 202 203
    if (_switchingTracksNotChapters) {
        if ([playbackController numberOfAudioTracks] > 2 && section == 0)
            return [playbackController numberOfAudioTracks];
204

205
        return [playbackController numberOfVideoSubtitlesIndexes];
206
    } else {
207 208
        if ([playbackController numberOfTitles] > 1 && section == 0)
            return [playbackController numberOfTitles];
209
        else
210
            return [playbackController numberOfChaptersForCurrentTitle];
211 212 213 214 215 216 217
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger index = indexPath.row;
218
    VLCPlaybackController *playbackController = [VLCPlaybackController sharedInstance];
219 220

    if (_switchingTracksNotChapters) {
221 222
        if ([playbackController numberOfAudioTracks] > 2 && indexPath.section == 0) {
            [playbackController selectAudioTrackAtIndex:index];
223

224 225
        } else if (index <= [playbackController numberOfVideoSubtitlesIndexes]) {
            [playbackController selectVideoSubtitleAtIndex:index];
226 227
        }
    } else {
228 229
        if ([playbackController numberOfTitles] > 1 && indexPath.section == 0)
            [playbackController selectTitleAtIndex:index];
230
        else
231
            [playbackController selectChapterAtIndex:index];
232 233 234
    }

    self.alpha = 1.0f;
235
    void (^animationBlock)(void) = ^() {
236 237 238 239 240 241 242
        self.alpha =  0.0f;;
    };

    NSTimeInterval animationDuration = .3;
    [UIView animateWithDuration:animationDuration animations:animationBlock completion:_completionHandler];
}
@end