VLCOneDriveTableViewController.m 6.69 KB
Newer Older
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1
2
3
4
/*****************************************************************************
 * VLCOneDriveTableViewController.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
Felix Paul Kühne's avatar
Felix Paul Kühne committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCOneDriveTableViewController.h"
#import "VLCOneDriveController.h"
#import "UIBarButtonItem+Theme.h"
#import "VLCCloudStorageTableViewCell.h"
#import "VLCAppDelegate.h"
#import "VLCOneDriveController.h"

20
@interface VLCOneDriveTableViewController () <UITableViewDataSource, UITableViewDelegate, VLCOneDriveControllerDelegate, VLCCloudStorageTableViewCell>
Felix Paul Kühne's avatar
Felix Paul Kühne committed
21
22
{
    UIBarButtonItem *_backButton;
23
    UIBarButtonItem *_logoutButton;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
24
25
26
27

    UIActivityIndicatorView *_activityIndicator;

    VLCOneDriveController *_oneDriveController;
28
    NSString *_currentPath;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
29
30
31
32
33
34
35
36
37
}
@end

@implementation VLCOneDriveTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _oneDriveController = [VLCOneDriveController sharedInstance];
38
    _oneDriveController.delegate = self;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
39
40
41
42
43
44

    self.modalPresentationStyle = UIModalPresentationFormSheet;

    self.navigationItem.title = @"OneDrive";

    _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
45
    self.navigationItem.leftBarButtonItem = _backButton;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
46

47
48
    _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
49
50
51
52
53
54
55
    self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
    self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
    self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];

    self.cloudStorageLogo = nil;
    if (!SYSTEM_RUNS_IOS7_OR_LATER) {
        self.flatLoginButton.hidden = YES;
56
        [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
57
58
    } else {
        self.loginButton.hidden = YES;
59
        [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    }

    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _activityIndicator.hidesWhenStopped = YES;
    _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_activityIndicator];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

75
76
77
78
79
    if (_oneDriveController.activeSession)
        [_oneDriveController login];

    if (!_oneDriveController.userAuthenticated)
        [self _showLoginDialog];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
80
81
82
83
84
85
86
87
88

    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
}

#pragma mark - generic interface interaction

- (IBAction)goBack:(id)sender
{
89
90
91
92
93
94
    if (_oneDriveController.rootFolder != _oneDriveController.currentFolder) {
        _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
        [_activityIndicator startAnimating];
        [_oneDriveController loadCurrentFolder];
    } else
        [self.navigationController popViewControllerAnimated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
95
96
}

97
98
99
100
#pragma mark - table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
101
    return _oneDriveController.currentFolder.items.count;
102
103
104
105
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
106
107
108
109
110
111
112
113
114
115
    static NSString *CellIdentifier = @"OneDriveCell";

    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];

    cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
    cell.delegate = self;

    return cell;
116
117
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
118
119
#pragma mark - table view delegate

120
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Felix Paul Kühne's avatar
Felix Paul Kühne committed
121
122
123
124
125
126
{
    cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
127
128
129
130
131
132
133
    VLCOneDriveObject *selectedObject = _oneDriveController.currentFolder.items[indexPath.row];

    if (selectedObject.isFolder) {
        /* dive into sub folder */
        [_activityIndicator startAnimating];
        _oneDriveController.currentFolder = selectedObject;
        [_oneDriveController loadCurrentFolder];
134
135
136
137
138
    } else {
        /* stream file */
        NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
        VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
        [appDelegate openMovieFromURL:url];
139
140
141
    }

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
142
143
144
}

#pragma mark - login dialog
145
146
147
148
149
150
151

- (void)logout
{
    [_oneDriveController logout];
    [self updateViewAfterSessionChange];
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
152
153
154
- (void)_showLoginDialog
{
    self.loginToCloudStorageView.frame = self.tableView.frame;
155
    self.navigationItem.rightBarButtonItem = nil;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
156
157
158
159
160
    [self.view addSubview:self.loginToCloudStorageView];
}

- (void)loginAction:(id)sender
{
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    [_oneDriveController login];
}

#pragma mark - onedrive controller delegation

- (void)mediaListUpdated
{
    [_activityIndicator stopAnimating];

    [self.tableView reloadData];
}

- (void)sessionWasUpdated
{
    [self updateViewAfterSessionChange];
}

#pragma mark - app delegate

- (void)updateViewAfterSessionChange
{
    self.navigationItem.rightBarButtonItem = _logoutButton;
    if (![_oneDriveController userAuthenticated]) {
        [self _showLoginDialog];
        return;
    } else if (self.loginToCloudStorageView.superview) {
        [self.loginToCloudStorageView removeFromSuperview];
    }

    if (_oneDriveController.currentFolder != nil)
        [self mediaListUpdated];
    else {
        [_activityIndicator startAnimating];
        [_oneDriveController loadCurrentFolder];
    }
}

#pragma mark - cell delegationx

- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
202
203
204
}

@end