VLCOneDriveTableViewController.m 5.43 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
 * $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"
19
#import "VLCProgressView.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
20
#import "UIDevice+VLC.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
21

Carola's avatar
Carola committed
22
@interface VLCOneDriveTableViewController ()
Felix Paul Kühne's avatar
Felix Paul Kühne committed
23
24
{
    VLCOneDriveController *_oneDriveController;
25
    VLCOneDriveObject *_selectedFile;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
26
27
28
29
30
31
32
33
}
@end

@implementation VLCOneDriveTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

Carola's avatar
Carola committed
34
35
36
    _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
    self.controller = _oneDriveController;
    self.controller.delegate = self;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
37

Pierre SAGASPE's avatar
Pierre SAGASPE committed
38
39
40
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];

    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
41
42
43
44
45

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

Carola's avatar
Carola committed
46
- (void)viewWillAppear:(BOOL)animated
47
{
Carola's avatar
Carola committed
48
49
    [super viewWillAppear:animated];
    [self updateViewAfterSessionChange];
50
    self.authorizationInProgress = NO;
51
52
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
53
54
#pragma mark - generic interface interaction

Carola's avatar
Carola committed
55
- (void)goBack
Felix Paul Kühne's avatar
Felix Paul Kühne committed
56
{
57
    if ((_oneDriveController.rootFolder != _oneDriveController.currentFolder) && [_oneDriveController isAuthorized]) {
58
        if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
59
            _oneDriveController.currentFolder = nil;
60
61
            self.title = _oneDriveController.rootFolder.name;
        } else {
62
            _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
63
64
            self.title = _oneDriveController.currentFolder.name;
        }
Carola's avatar
Carola committed
65
        [self.activityIndicator startAnimating];
66
67
68
        [_oneDriveController loadCurrentFolder];
    } else
        [self.navigationController popViewControllerAnimated:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
69
70
}

71
72
73
74
#pragma mark - table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
75
76
77
78
79
80
81
82
83
84
    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;
85
86
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
87
88
89
90
#pragma mark - table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
91
92
93
94
    VLCOneDriveObject *selectedObject = _oneDriveController.currentFolder.items[indexPath.row];

    if (selectedObject.isFolder) {
        /* dive into sub folder */
Carola's avatar
Carola committed
95
        [self.activityIndicator startAnimating];
96
97
        _oneDriveController.currentFolder = selectedObject;
        [_oneDriveController loadCurrentFolder];
98
        self.title = selectedObject.name;
99
100
101
102
103
    } else {
        /* stream file */
        NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
        VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
        [appDelegate openMovieFromURL:url];
104
105
106
    }

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

109
110
111
112
113
114
115
116
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
        [_oneDriveController downloadObject:_selectedFile];

    _selectedFile = nil;
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
117
#pragma mark - login dialog
118

Felix Paul Kühne's avatar
Felix Paul Kühne committed
119
120
- (void)loginAction:(id)sender
{
121
122
123
124
125
    if (![_oneDriveController isAuthorized]) {
        self.authorizationInProgress = YES;
        [_oneDriveController login];
    } else
        [_oneDriveController logout];
126
127
128
129
130
131
132
133
134
}

#pragma mark - onedrive controller delegation

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

Carola's avatar
Carola committed
135
#pragma mark - cell delegation
136
137
138

- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
{
139
140
141
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];

142
143
144
145
146
147
148
149
    if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
        /* selected item is a proper file, ask the user if s/he wants to download it */
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
        [alert show];
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
150
151
152
}

@end