Commit 90b3225c authored by Carola Nitz's avatar Carola Nitz

GDrive: show folder structure

parent 92515f7c
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
- (void)startSession; - (void)startSession;
- (void)stopSession; - (void)stopSession;
- (void)logout; - (void)logout;
- (void)requestFileListing; - (void)requestDirectoryListingWithFolderId:(NSString *)folderId;
- (BOOL)hasMoreFiles; - (BOOL)hasMoreFiles;
- (void)downloadFileToDocumentFolder:(GTLDriveFile *)file; - (void)downloadFileToDocumentFolder:(GTLDriveFile *)file;
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
BOOL _downloadInProgress; BOOL _downloadInProgress;
NSString *_nextPageToken; NSString *_nextPageToken;
NSString *_folderId;
CGFloat _averageSpeed; CGFloat _averageSpeed;
NSTimeInterval _startDL; NSTimeInterval _startDL;
...@@ -90,10 +91,14 @@ ...@@ -90,10 +91,14 @@
} }
#pragma mark - file management #pragma mark - file management
- (void)requestFileListing - (void)requestDirectoryListingWithFolderId:(NSString *)folderId
{ {
if (self.isAuthorized) if (self.isAuthorized) {
[self listFiles]; //we entered a different folder so discard all current files
if (![folderId isEqualToString:_folderId])
_currentFileList = nil;
[self listFilesWithID:folderId];
}
} }
- (BOOL)hasMoreFiles - (BOOL)hasMoreFiles
...@@ -103,6 +108,8 @@ ...@@ -103,6 +108,8 @@
- (void)downloadFileToDocumentFolder:(GTLDriveFile *)file - (void)downloadFileToDocumentFolder:(GTLDriveFile *)file
{ {
if ([file.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) return;
if (!_listOfGoogleDriveFilesToDownload) if (!_listOfGoogleDriveFilesToDownload)
_listOfGoogleDriveFilesToDownload = [[NSMutableArray alloc] init]; _listOfGoogleDriveFilesToDownload = [[NSMutableArray alloc] init];
...@@ -114,18 +121,18 @@ ...@@ -114,18 +121,18 @@
[self _triggerNextDownload]; [self _triggerNextDownload];
} }
- (void)listFiles - (void)listFilesWithID:(NSString *)folderId
{ {
_fileList = nil; _fileList = nil;
_folderId = folderId;
GTLQueryDrive *query; GTLQueryDrive *query;
query = [GTLQueryDrive queryForFilesList]; query = [GTLQueryDrive queryForFilesList];
query.fields = @"items(originalFilename,title,mimeType,fileExtension,fileSize,iconLink,downloadUrl,webContentLink,thumbnailLink),nextPageToken";
query.pageToken = _nextPageToken; query.pageToken = _nextPageToken;
query.maxResults = 100; query.maxResults = 100;
if (![_folderId isEqualToString:@""]) {
APLog(@"fetching files with following queryfields:%@", query.fields); query.q = [NSString stringWithFormat:@"'%@' in parents", [_folderId lastPathComponent]];
}
_fileListTicket = [self.driveService executeQuery:query _fileListTicket = [self.driveService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket, completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *fileList, GTLDriveFileList *fileList,
...@@ -134,7 +141,7 @@ ...@@ -134,7 +141,7 @@
_fileList = fileList; _fileList = fileList;
_nextPageToken = fileList.nextPageToken; _nextPageToken = fileList.nextPageToken;
_fileListTicket = nil; _fileListTicket = nil;
[self _listOfGoodFiles]; [self _listOfGoodFilesAndFolders];
} else { } else {
[self showAlert:NSLocalizedString(@"GDRIVE_ERROR_FETCHING_FILES",nil) message:error.localizedDescription]; [self showAlert:NSLocalizedString(@"GDRIVE_ERROR_FETCHING_FILES",nil) message:error.localizedDescription];
} }
...@@ -173,25 +180,42 @@ ...@@ -173,25 +180,42 @@
return NO; return NO;
} }
- (void)_listOfGoodFiles - (void)_listOfGoodFilesAndFolders
{ {
NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init]; NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
for (GTLDriveFile *driveFile in _fileList.items) for (GTLDriveFile *driveFile in _fileList.items)
{ {
BOOL isDirectory = [driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]; BOOL isDirectory = [driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
if (!isDirectory && [self _supportedFileExtension:[NSString stringWithFormat:@".%@",driveFile.fileExtension ]]) { BOOL inDirectory = NO;
[listOfGoodFilesAndFolders addObject:driveFile]; if (driveFile.parents.count > 0) {
GTLDriveParentReference *parent = (GTLDriveParentReference *)driveFile.parents[0];
//since there is no rootfolder display the files right away
if (![parent.isRoot boolValue])
inDirectory = ![parent.identifier isEqualToString:[_folderId lastPathComponent]];
} }
BOOL supportedFile = [self _supportedFileExtension:[NSString stringWithFormat:@".%@",driveFile.fileExtension]];
if ((isDirectory || supportedFile) && !inDirectory)
[listOfGoodFilesAndFolders addObject:driveFile];
} }
_currentFileList = [_currentFileList count] ? [_currentFileList arrayByAddingObjectsFromArray:listOfGoodFilesAndFolders] : [NSArray arrayWithArray:listOfGoodFilesAndFolders]; _currentFileList = [_currentFileList count] ? [_currentFileList arrayByAddingObjectsFromArray:listOfGoodFilesAndFolders] : [NSArray arrayWithArray:listOfGoodFilesAndFolders];
if ([_currentFileList count] <= 10 && [self hasMoreFiles]) { if ([_currentFileList count] <= 10 && [self hasMoreFiles]) {
[self requestFileListing]; [self listFilesWithID:_folderId];
return; return;
} }
APLog(@"found filtered metadata for %lu files", (unsigned long)_currentFileList.count); APLog(@"found filtered metadata for %lu files", (unsigned long)_currentFileList.count);
//the files come in a chaotic order so we order alphabetically
NSArray *sortedArray = [_currentFileList sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSString *first = [(GTLDriveFile *)a title];
NSString *second = [(GTLDriveFile *)b title];
return [first compare:second];
}];
_currentFileList = sortedArray;
if ([self.delegate respondsToSelector:@selector(mediaListUpdated)]) if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
[self.delegate mediaListUpdated]; [self.delegate mediaListUpdated];
} }
......
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
GTLDriveFile *_selectedFile; GTLDriveFile *_selectedFile;
GTMOAuth2ViewControllerTouch *_authController; GTMOAuth2ViewControllerTouch *_authController;
NSString *_currentFolderId;
UIBarButtonItem *_backButton; UIBarButtonItem *_backButton;
UIBarButtonItem *_backToMenuButton; UIBarButtonItem *_backToMenuButton;
...@@ -181,12 +183,12 @@ ...@@ -181,12 +183,12 @@
} }
} }
- (void)_requestInformationForFiles - (void)_requestInformationForCurrentFolderId
{ {
[_activityIndicator startAnimating]; [_activityIndicator startAnimating];
[_googleDriveController requestFileListing]; [_googleDriveController requestDirectoryListingWithFolderId:_currentFolderId];
self.navigationItem.leftBarButtonItem = _backToMenuButton; self.navigationItem.leftBarButtonItem = ![_currentFolderId isEqualToString:@""] ? _backButton : _backToMenuButton;
} }
#pragma mark - interface interaction #pragma mark - interface interaction
...@@ -200,6 +202,10 @@ ...@@ -200,6 +202,10 @@
- (IBAction)goBack:(id)sender - (IBAction)goBack:(id)sender
{ {
if (![_currentFolderId isEqualToString:@""] && [_currentFolderId length] > 0) {
_currentFolderId = [_currentFolderId stringByDeletingLastPathComponent];
[self _requestInformationForCurrentFolderId];
} else
[[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration]; [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
} }
...@@ -240,8 +246,17 @@ ...@@ -240,8 +246,17 @@
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ {
_selectedFile = _googleDriveController.currentListFiles[indexPath.row]; _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", @""), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil]; UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", @""), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
[alert show]; [alert show];
} else {
/* dive into subdirectory */
if (![_currentFolderId isEqualToString:@""])
_currentFolderId = [_currentFolderId stringByAppendingString:@"/"];
_currentFolderId = [_currentFolderId stringByAppendingString:_selectedFile.identifier];
[self _requestInformationForCurrentFolderId];
}
_selectedFile = nil;
[self.tableView deselectRowAtIndexPath:indexPath animated:NO]; [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
} }
...@@ -252,7 +267,7 @@ ...@@ -252,7 +267,7 @@
if (maximumOffset - currentOffset <= - self.tableView.rowHeight) { if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) { if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
[self _requestInformationForFiles]; [self _requestInformationForCurrentFolderId];
} }
} }
} }
...@@ -319,8 +334,9 @@ ...@@ -319,8 +334,9 @@
[self.loginToCloudStorageView removeFromSuperview]; [self.loginToCloudStorageView removeFromSuperview];
//reload if we didn't come back from streaming //reload if we didn't come back from streaming
_currentFolderId = @"";
if([_googleDriveController.currentListFiles count] == 0) if([_googleDriveController.currentListFiles count] == 0)
[self _requestInformationForFiles]; [self _requestInformationForCurrentFolderId];
} }
#pragma mark - login dialog #pragma mark - login dialog
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment