Im adding a “move to folder” functionality to my app with dropbox support. Im trying to get all folders, and subfolders throughout the whole users dropbox. Heres the code i have right now that if logged, has the right order but is backwards
-(void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
if (allFolders == nil) {
allFolders = [[NSMutableArray alloc] init];
[allFolders addObject:@"/"];
}
for (DBMetadata *fileMetadata in metadata.contents) {
if ([fileMetadata isDirectory]) {
NSArray *filePathComponents = [fileMetadata.path pathComponents];
NSString *inFolder = [filePathComponents objectAtIndex:[filePathComponents count]-2];
NSLog(@"file is in folder %@",inFolder);
[allFolders insertObject:fileMetadata.filename atIndex:[allFolders indexOfObject:inFolder]];
if ([metadata.contents count] > 0) [[self restClient] loadMetadata:fileMetadata.path];
}
}
}
the issue with this is my hViewFoldersViewController also has to show all folders for the documents directory for the app, and i use a custom NSObject called Folder Item which stores all items path, name, and indent level for the table. if i use this method, i cannot get the path because al lFolders in inside the dropbox helper class, i can move it to the viewcontroller but the thing that is confusing me is getting the objectAtIndex. if i leave it to just adding objects, it will show all first level folders, then second, then third. i want it so if in dropbox you have a folder called “test 1” and then test 1 contained “test 2”, then it shows test 1, then with more indent the test 2 folder and then other first directory folders. and if i use my FolderItem in the array i cannot find the objectAtIndex. How can i get the index of the folders i need to insert then?
The issue you are having is that you are building a simple list of filenames. You need to build a tree structure. One way is to create an array of dictionaries. Each element in the array is a dictionary. The dictionary contains the filename and an array of its subdirectories.
As the
restClient:loadedMetadata:method is called, you need to look at the metadata to determine the parent directory. Use this to find the correct spot in your tree structure so you can fill in the subdirectory array at the proper node in the tree.