Dropbox restructured, cleanup
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
+162
-61
@@ -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
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user