瀏覽代碼

GDrive: show folder structure

Carola Nitz 11 年之前
父節點
當前提交
90b3225c67
共有 3 個文件被更改,包括 62 次插入22 次删除
  1. 1 1
      Sources/VLCGoogleDriveController.h
  2. 37 13
      Sources/VLCGoogleDriveController.m
  3. 24 8
      Sources/VLCGoogleDriveTableViewController.m

+ 1 - 1
Sources/VLCGoogleDriveController.h

@@ -39,7 +39,7 @@
 - (void)startSession;
 - (void)stopSession;
 - (void)logout;
-- (void)requestFileListing;
+- (void)requestDirectoryListingWithFolderId:(NSString *)folderId;
 - (BOOL)hasMoreFiles;
 - (void)downloadFileToDocumentFolder:(GTLDriveFile *)file;
 

+ 37 - 13
Sources/VLCGoogleDriveController.m

@@ -27,6 +27,7 @@
     BOOL _downloadInProgress;
 
     NSString *_nextPageToken;
+    NSString *_folderId;
 
     CGFloat _averageSpeed;
     NSTimeInterval _startDL;
@@ -90,10 +91,14 @@
 }
 
 #pragma mark - file management
-- (void)requestFileListing
+- (void)requestDirectoryListingWithFolderId:(NSString *)folderId
 {
-    if (self.isAuthorized)
-        [self listFiles];
+    if (self.isAuthorized) {
+        //we entered a different folder so discard all current files
+        if (![folderId isEqualToString:_folderId])
+            _currentFileList = nil;
+        [self listFilesWithID:folderId];
+    }
 }
 
 - (BOOL)hasMoreFiles
@@ -103,6 +108,8 @@
 
 - (void)downloadFileToDocumentFolder:(GTLDriveFile *)file
 {
+    if ([file.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) return;
+
     if (!_listOfGoogleDriveFilesToDownload)
         _listOfGoogleDriveFilesToDownload = [[NSMutableArray alloc] init];
 
@@ -114,18 +121,18 @@
     [self _triggerNextDownload];
 }
 
-- (void)listFiles
+- (void)listFilesWithID:(NSString *)folderId
 {
     _fileList = nil;
-
+    _folderId = folderId;
     GTLQueryDrive *query;
 
     query = [GTLQueryDrive queryForFilesList];
-    query.fields = @"items(originalFilename,title,mimeType,fileExtension,fileSize,iconLink,downloadUrl,webContentLink,thumbnailLink),nextPageToken";
     query.pageToken = _nextPageToken;
     query.maxResults = 100;
-
-    APLog(@"fetching files with following queryfields:%@", query.fields);
+    if (![_folderId isEqualToString:@""]) {
+        query.q = [NSString stringWithFormat:@"'%@' in parents", [_folderId lastPathComponent]];
+    }
     _fileListTicket = [self.driveService executeQuery:query
                           completionHandler:^(GTLServiceTicket *ticket,
                                               GTLDriveFileList *fileList,
@@ -134,7 +141,7 @@
                                   _fileList = fileList;
                                   _nextPageToken = fileList.nextPageToken;
                                   _fileListTicket = nil;
-                                  [self _listOfGoodFiles];
+                                  [self _listOfGoodFilesAndFolders];
                               } else {
                                   [self showAlert:NSLocalizedString(@"GDRIVE_ERROR_FETCHING_FILES",nil) message:error.localizedDescription];
                               }
@@ -173,25 +180,42 @@
     return NO;
 }
 
-- (void)_listOfGoodFiles
+- (void)_listOfGoodFilesAndFolders
 {
     NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
 
     for (GTLDriveFile *driveFile in _fileList.items)
     {
         BOOL isDirectory = [driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
-        if (!isDirectory && [self _supportedFileExtension:[NSString stringWithFormat:@".%@",driveFile.fileExtension ]]) {
-            [listOfGoodFilesAndFolders addObject:driveFile];
+        BOOL inDirectory = NO;
+        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];
 
     if ([_currentFileList count] <= 10 && [self hasMoreFiles]) {
-        [self requestFileListing];
+        [self listFilesWithID:_folderId];
         return;
     }
 
     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)])
         [self.delegate mediaListUpdated];
 }

+ 24 - 8
Sources/VLCGoogleDriveTableViewController.m

@@ -26,6 +26,8 @@
     GTLDriveFile *_selectedFile;
     GTMOAuth2ViewControllerTouch *_authController;
 
+    NSString *_currentFolderId;
+
     UIBarButtonItem *_backButton;
     UIBarButtonItem *_backToMenuButton;
 
@@ -181,12 +183,12 @@
     }
 }
 
-- (void)_requestInformationForFiles
+- (void)_requestInformationForCurrentFolderId
 {
     [_activityIndicator startAnimating];
-    [_googleDriveController requestFileListing];
+    [_googleDriveController requestDirectoryListingWithFolderId:_currentFolderId];
 
-    self.navigationItem.leftBarButtonItem = _backToMenuButton;
+    self.navigationItem.leftBarButtonItem = ![_currentFolderId isEqualToString:@""] ? _backButton : _backToMenuButton;
 }
 
 #pragma mark - interface interaction
@@ -200,7 +202,11 @@
 
 - (IBAction)goBack:(id)sender
 {
-    [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
+    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];
 }
 
 #pragma mark - Table view data source
@@ -240,8 +246,17 @@
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
-    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];
+    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];
+        [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];
 }
 
@@ -252,7 +267,7 @@
 
     if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
         if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
-            [self _requestInformationForFiles];
+            [self _requestInformationForCurrentFolderId];
         }
     }
 }
@@ -319,8 +334,9 @@
         [self.loginToCloudStorageView removeFromSuperview];
 
     //reload if we didn't come back from streaming
+    _currentFolderId = @"";
     if([_googleDriveController.currentListFiles count] == 0)
-        [self _requestInformationForFiles];
+        [self _requestInformationForCurrentFolderId];
 }
 
 #pragma mark - login dialog