FMCPlanner2/FMC Planner 2/ViewControllerServiceMenu.m

210 lines
7.6 KiB
Objective-C

//
// ViewControllerServiceMenu.m
// FMC Planner 2
//
// Created by Kilian Hofmann on 26.03.16.
// Copyright © 2016 Kilian Hofmann. All rights reserved.
//
#import "ViewControllerServiceMenu.h"
@implementation ViewControllerServiceMenu
#pragma mark - View management and navigation
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Service Menu";
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"FMC"
style:UIBarButtonItemStyleDone
target:self
action:@selector(back)];
dbClient = _main.dbClient;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (dbClient.token != nil) {
[_dbConnectButton setTitle:@"Disconnect from Dropbox"
forState:UIControlStateNormal];
[_dbConnectButton removeTarget:self
action:@selector(dbConnect:)
forControlEvents:UIControlEventTouchUpInside];
[_dbConnectButton addTarget:self
action:@selector(dbDisconnect:)
forControlEvents:UIControlEventTouchUpInside];
}
_dbDownload.enabled = YES;
_dbDownload.alpha = 1.0;
_dbUpload.enabled = YES;
_dbUpload.alpha = 1.0;
_dbNavData.enabled = YES;
_dbNavData.alpha = 1.0;
if (_main.downloadActive) {
_dbDownload.enabled = NO;
_dbDownload.alpha = 0.5;
}
if (_main.uploadActive) {
_dbUpload.enabled = NO;
_dbUpload.alpha = 0.5;
}
if (_main.navDataActive) {
_dbNavData.enabled = NO;
_dbNavData.alpha = 0.5;
}
}
- (void)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
((TableViewController *)segue.destinationViewController).title =
segue.identifier;
((TableViewController *)segue.destinationViewController).main = _main;
((TableViewController *)segue.destinationViewController).sm = self;
}
#pragma mark - Dropbox Button
// Standard action (hence set), dynamically set and unset
- (IBAction)dbConnect:(UIButton *)sender
{
UIViewController *webController = [[UIViewController alloc] init];
webController.title = @"Dropbox";
UIWebView *web = [[UIWebView alloc] initWithFrame:webController.view.frame];
[webController.view addSubview:web];
[self.navigationController pushViewController:webController animated:YES];
((AppDelegate *)[UIApplication sharedApplication].delegate).smWebView =
webController;
((AppDelegate *)[UIApplication sharedApplication].delegate).smMenu = self;
[web loadRequest:
[NSURLRequest
requestWithURL:
[NSURL URLWithString:@"https://dropbox.com/1/oauth2/"
@"authorize?response_type=token&"
@"client_id=7q5bwtmi2h2iszh&redirect_"
@"uri=x-fmc:/"]]];
}
// Dynamically set and unset
- (IBAction)dbDisconnect:(UIButton *)sender
{
[dbClient deauthorizeUserWithPresenter:self];
[_dbConnectButton setTitle:@"Connect to Dropbox"
forState:UIControlStateNormal];
[_dbConnectButton removeTarget:self
action:@selector(dbDisconnect:)
forControlEvents:UIControlEventTouchUpInside];
[_dbConnectButton addTarget:self
action:@selector(dbConnect:)
forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)dbGetNavData:(UIButton *)sender
{
[self alertViewiOS7];
}
- (void)dbGetNavDataHelper
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
_dbNavData.enabled = NO;
_dbNavData.alpha = 0.5;
_main.navDataActive = true;
[queue addOperationWithBlock:^{
[dbClient
contentsOfPath:@"NAVDATA"
completion:^(NSArray *files) {
if (files != nil) {
NSMutableArray *dlList = [[NSMutableArray alloc] init];
for (NSDictionary *file in files) {
[dlList
addObject:[[file valueForKey:@"path_display"]
substringFromIndex:1]];
}
[dbClient
downloadFromDropbox:dlList
presenter:self
completion:^{
[[NSOperationQueue mainQueue]
addOperationWithBlock:^{
_main.navDataActive = false;
_dbNavData.enabled = YES;
_dbNavData.alpha = 1.0;
[self.view setNeedsDisplay];
}];
}];
}
else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_main.navDataActive = false;
_dbNavData.enabled = YES;
_dbNavData.alpha = 1.0;
[self.view setNeedsDisplay];
}];
}
}
presenter:self];
}];
}
#pragma mark - Delegate for UIAlertView and UIAlertController outsource
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self dbGetNavDataHelper];
}
}
- (void)alertViewiOS7
{
if ([UIAlertController class]) {
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"NavData"
message:@"This app requires the QualityWings "
@"NavData "
@"files. Please ensure that you have the "
@"awys.txt, ints.txt and navs.txt inside "
@"the "
@"NAVDATA folder in your Dropbox."
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *defaultAction =
[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
UIAlertAction *loadAction =
[UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self dbGetNavDataHelper];
}];
[alert addAction:defaultAction];
[alert addAction:loadAction];
[self presentViewController:alert animated:YES completion:nil];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"NavData"
message:@"This app requires the QualityWings NavData "
@"files. Please ensure that you have the "
@"awys.txt, ints.txt and navs.txt inside the "
@"NAVDATA folder in your Dropbox."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
[alert show];
}
}
@end