diff --git a/FMC PLanner 2.xcworkspace/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/FMC PLanner 2.xcworkspace/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 25ee3d8..ed9a9b4 100644 --- a/FMC PLanner 2.xcworkspace/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/FMC PLanner 2.xcworkspace/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -2,22 +2,4 @@ - - - - - - diff --git a/FMC Planner 2.xcodeproj/project.pbxproj b/FMC Planner 2.xcodeproj/project.pbxproj index 6c1a74c..6cc4ddd 100644 --- a/FMC Planner 2.xcodeproj/project.pbxproj +++ b/FMC Planner 2.xcodeproj/project.pbxproj @@ -144,6 +144,8 @@ C605C480B46761595F28A6B2 /* Frameworks */ = { isa = PBXGroup; children = ( + E2B694BB1CAC0C56007B28EF /* Security.framework */, + E28E106C1C9FF93D006E2C6B /* QuartzCore.framework */, E22E0B5A1CAD657000B71EE5 /* libSWTableViewCell.a */, E22E0B571CAD656700B71EE5 /* libPods-FMC Planner 2UITests.a */, E22E0B551CAD655D00B71EE5 /* libPods-FMC Planner 2.a */, @@ -155,8 +157,6 @@ E22EE9C31C9DB7B3001F5BC0 = { isa = PBXGroup; children = ( - E2B694BB1CAC0C56007B28EF /* Security.framework */, - E28E106C1C9FF93D006E2C6B /* QuartzCore.framework */, E22EE9CE1C9DB7B3001F5BC0 /* FMC Planner 2 */, E22EE9E81C9DB7B4001F5BC0 /* FMC Planner 2Tests */, E22EE9F31C9DB7B4001F5BC0 /* FMC Planner 2UITests */, diff --git a/FMC Planner 2/AppDelegate.h b/FMC Planner 2/AppDelegate.h index 1052884..d856c88 100644 --- a/FMC Planner 2/AppDelegate.h +++ b/FMC Planner 2/AppDelegate.h @@ -22,4 +22,8 @@ @property (strong, nonatomic) UIViewController *smWebView; +#pragma mark - Refrence to main view for Alerts and shit. + +@property (strong, nonatomic) UIViewController *smMenu; + @end diff --git a/FMC Planner 2/AppDelegate.m b/FMC Planner 2/AppDelegate.m index c48d343..cbc552b 100644 --- a/FMC Planner 2/AppDelegate.m +++ b/FMC Planner 2/AppDelegate.m @@ -22,6 +22,10 @@ credentialsForProtectionSpace:_dbClient.kDropboxProtectionSpace]; NSURLCredential *credential = [credentials.objectEnumerator nextObject]; _dbClient.token = credential.password; + // Reset if user changed it + [_dbClient setupDropboxWithHandler:^{ + } + presenter:nil]; return YES; } @@ -73,8 +77,11 @@ completion:^(void) { [_smWebView.navigationController popViewControllerAnimated:YES]; + [_dbClient setupDropboxWithHandler:^{ + } + presenter:_smMenu]; } - presenter:nil]; + presenter:_smMenu]; return YES; } diff --git a/FMC Planner 2/DropboxV2ObjC.h b/FMC Planner 2/DropboxV2ObjC.h index b77b0f6..e800829 100644 --- a/FMC Planner 2/DropboxV2ObjC.h +++ b/FMC Planner 2/DropboxV2ObjC.h @@ -39,9 +39,9 @@ completion:(void (^)(NSArray *data))handler presenter:(UIViewController *)presenter; -#pragma mark - Other methods - -- (NSDictionary *)getUserInfoWithPresenter:(UIViewController *)presenter; +#pragma mark - Setup methods +- (void)setupDropboxWithHandler:(void (^)(void))handler + presenter:(UIViewController *)presenter; @end \ No newline at end of file diff --git a/FMC Planner 2/DropboxV2ObjC.m b/FMC Planner 2/DropboxV2ObjC.m index 1b3c7a0..7d05440 100644 --- a/FMC Planner 2/DropboxV2ObjC.m +++ b/FMC Planner 2/DropboxV2ObjC.m @@ -137,8 +137,7 @@ path = _rootDirectory; } else { - path = [path - stringByAppendingString:[NSString stringWithFormat:@"/%@", path]]; + path = [NSString stringWithFormat:@"/%@", path]; } NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; @@ -211,6 +210,9 @@ }]; } +/** + * Specify if EXPORT or SAVE in file name + */ - (void)downloadFromDropbox:(NSArray *)files presenter:(UIViewController *)presenter completion:(void (^)(void))handler @@ -254,8 +256,14 @@ options:0 error:nil] valueForKey:@"error_summary"] == nil) { - [data writeToFile:[SharedDeclerations - savePathForFile:string] + [data writeToFile: + [SharedDeclerations + savePathForFile: + [string + substringFromIndex: + [string + rangeOfString:@"/"] + .location]] atomically:YES]; } else { @@ -294,6 +302,9 @@ handler(); } +/** + * Specify if SAVE or OLDSAVE in file name + */ - (void)uploadToDropbox:(NSArray *)files presenter:(UIViewController *)presenter completion:(void (^)(void))handler @@ -369,65 +380,83 @@ handler(); } -#pragma mark - Other operations +#pragma mark - Setup methods -// !!!: Cannot be asynchronous -- (NSDictionary *)getUserInfoWithPresenter:(UIViewController *)presenter +- (void)setupDropboxWithHandler:(void (^)(void))handler + presenter:(UIViewController *)presenter { - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; - request.URL = - [NSURL URLWithString: - @"https://api.dropboxapi.com/2/users/get_current_account"]; - [request addValue:[NSString stringWithFormat:@"Bearer %@", _token] - .precomposedStringWithCanonicalMapping - forHTTPHeaderField:(@"Authorization") - .precomposedStringWithCanonicalMapping]; - [request addValue:(@"application/json") - .precomposedStringWithCanonicalMapping - forHTTPHeaderField:(@"Content-Type") - .precomposedStringWithCanonicalMapping]; - request.HTTPMethod = @"POST"; - request.HTTPBody = _kJSONNullObject; - - NSURLResponse *response = nil; - NSError *error = nil; - NSError *error2 = nil; - id data = [self parseJSON:[NSURLConnection sendSynchronousRequest:request - returningResponse:&response - error:&error2]]; - if ([data isKindOfClass:[NSDictionary class]]) { - if ([[NSJSONSerialization JSONObjectWithData:data options:0 error:nil] - valueForKey:@"error_summary"] == nil) { - return data; - } - else { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [SharedDeclerations - presentErrorAlert: - [NSError errorWithDomain:DropboxErrorDomain - code:100 - userInfo:[[NSJSONSerialization - JSONObjectWithData:data - options:0 - error:nil] - valueForKey: - DropboxErrorUserInfo]] - presenter:presenter]; - }]; - return nil; - } - } - else if (error != nil) { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - - [SharedDeclerations presentErrorAlert:error presenter:presenter]; - - }]; - return nil; - } - else { - return nil; - } + NSOperationQueue *queue = [[NSOperationQueue alloc] init]; + [queue addOperationWithBlock:^{ + // Exports (PMDG format) + [self contentsOfPath:nil + completion:^(NSArray *data) { + BOOL exports = false; + for (NSDictionary *dict in data) { + if ([[dict valueForKey:@"name"] + isEqualToString:@"EXPORTS"]) { + exports = true; + break; + } + } + if (!exports) { + [self createFolderAtPath:@"EXPORTS" + presenter:presenter]; + } + } + presenter:presenter]; + // NavData (Two files from PMDG Navigraph data + [self contentsOfPath:nil + completion:^(NSArray *data) { + BOOL exports = false; + for (NSDictionary *dict in data) { + if ([[dict valueForKey:@"name"] + isEqualToString:@"NAVDATA"]) { + exports = true; + break; + } + } + if (!exports) { + [self createFolderAtPath:@"NAVDATA" + presenter:presenter]; + } + } + presenter:presenter]; + // Backups of Saves + [self + contentsOfPath:nil + completion:^(NSArray *data) { + BOOL exports = false; + for (NSDictionary *dict in data) { + if ([[dict valueForKey:@"name"] + isEqualToString:@"SAVES"]) { + exports = true; + break; + } + } + if (!exports) { + [self createFolderAtPath:@"SAVES" presenter:presenter]; + } + } + presenter:presenter]; + // Old saves (FMC Planner + (or X)) + [self contentsOfPath:nil + completion:^(NSArray *data) { + BOOL exports = false; + for (NSDictionary *dict in data) { + if ([[dict valueForKey:@"name"] + isEqualToString:@"OLDSAVES"]) { + exports = true; + break; + } + } + if (!exports) { + [self createFolderAtPath:@"OLDSAVES" + presenter:presenter]; + } + } + presenter:presenter]; + }]; + handler(); } #pragma mark - Helper methods @@ -446,4 +475,76 @@ } } +- (void)createFolderAtPath:(NSString *)path + presenter:(UIViewController *)presenter +{ + if (path == nil) { + path = _rootDirectory; + } + else { + path = [NSString stringWithFormat:@"/%@", path]; + } + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; + request.URL = [NSURL + URLWithString:@"https://api.dropboxapi.com/2/files/create_folder"]; + [request addValue:[NSString stringWithFormat:@"Bearer %@", _token] + .precomposedStringWithCanonicalMapping + forHTTPHeaderField:(@"Authorization") + .precomposedStringWithCanonicalMapping]; + [request addValue:(@"application/json") + .precomposedStringWithCanonicalMapping + forHTTPHeaderField:(@"Content-Type") + .precomposedStringWithCanonicalMapping]; + request.HTTPMethod = @"POST"; + NSData *data = [NSJSONSerialization dataWithJSONObject:@{ + @"path" : path.precomposedStringWithCanonicalMapping + } + options:0 + error:nil]; + NSString *temp = + [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; + temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; + request.HTTPBody = [temp dataUsingEncoding:NSASCIIStringEncoding]; + + [NSURLConnection + sendAsynchronousRequest:request + queue:[[NSOperationQueue alloc] init] + completionHandler:^(NSURLResponse *response, NSData *data, + NSError *error) { + if (error == nil) { + if ([[NSJSONSerialization JSONObjectWithData:data + options:0 + error:nil] + valueForKey:@"error_summary"] == nil) { + } + else { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError + errorWithDomain:DropboxErrorDomain + code:100 + userInfo: + [[NSJSONSerialization + JSONObjectWithData:data + options:0 + error:nil] + valueForKey: + DropboxErrorUserInfo]] + presenter:presenter]; + }]; + } + } + else if (error != nil) { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + + [SharedDeclerations presentErrorAlert:error + presenter:presenter]; + + }]; + } + + }]; +} + @end diff --git a/FMC Planner 2/TableViewController.m b/FMC Planner 2/TableViewController.m index 89bb0aa..a57a0b5 100644 --- a/FMC Planner 2/TableViewController.m +++ b/FMC Planner 2/TableViewController.m @@ -42,7 +42,7 @@ DropboxV2ObjC *dbClient = nil; } if ([self.title isEqualToString:@"Download"]) { [dbClient - contentsOfPath:nil + contentsOfPath:@"SAVES" completion:^(NSArray *data) { NSMutableArray *temp = [[NSMutableArray alloc] init]; @@ -200,16 +200,26 @@ DropboxV2ObjC *dbClient = nil; - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index { + NSString *file = @""; + if ([self.title isEqualToString:@"Download"] || + [self.title isEqualToString:@"Upload"]) { + file = @"SAVES/"; + } + else if (false) { + // TODO: Import of old saves and export of PMDG + } if ([((UIButton *)cell.leftUtilityButtons[index]) .titleLabel.text isEqualToString:@"Mark"]) { - [_markList addObject:cell.textLabel.text]; + [_markList + addObject:[file stringByAppendingString:cell.textLabel.text]]; cell.leftUtilityButtons = [self leftButtonsSelected]; cell.accessoryType = UITableViewCellAccessoryCheckmark; [cell hideUtilityButtonsAnimated:YES]; } else if ([((UIButton *)cell.leftUtilityButtons[index]) .titleLabel.text isEqualToString:@"Unmark"]) { - [_markList removeObject:cell.textLabel.text]; + [_markList + removeObject:[file stringByAppendingString:cell.textLabel.text]]; cell.leftUtilityButtons = [self leftButtonsNormal]; cell.accessoryType = UITableViewCellAccessoryNone; [cell hideUtilityButtonsAnimated:YES]; diff --git a/FMC Planner 2/ViewControllerServiceMenu.m b/FMC Planner 2/ViewControllerServiceMenu.m index 81b1553..3e13fbf 100644 --- a/FMC Planner 2/ViewControllerServiceMenu.m +++ b/FMC Planner 2/ViewControllerServiceMenu.m @@ -64,6 +64,7 @@ [self.navigationController pushViewController:webController animated:YES]; ((AppDelegate *)[UIApplication sharedApplication].delegate).smWebView = webController; + ((AppDelegate *)[UIApplication sharedApplication].delegate).smMenu = self; [web loadRequest: [NSURLRequest requestWithURL: