// // TableViewController.m // FMC Planner 2 // // Created by Kilian Hofmann on 27.03.16. // Copyright © 2016 Kilian Hofmann. All rights reserved. // #import "TableViewController.h" @implementation TableViewController DropboxV2ObjC *dbClient = nil; #pragma mark - View management and navigation - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation // bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); _markList = [[NSMutableArray alloc] init]; dbClient = ((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient; if ([self.title isEqualToString:@"Manage"] || [self.title isEqualToString:@"Upload"]) { NSArray *temp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[SharedDeclerations savePathForFile:@"SAVES"] error:nil]; _dataSource = [[temp filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"NOT (SELF BEGINSWITH %@)", @"."]] mutableCopy]; } if ([self.title isEqualToString:@"Download"]) { [dbClient contentsOfPath:@"SAVES" completion:^(NSArray *data) { NSMutableArray *temp = [[NSMutableArray alloc] init]; for (NSDictionary *dict in data) { [temp addObject:[dict valueForKey:@"name"]]; } _dataSource = [[temp filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"NOT (SELF BEGINSWITH %@)", @"."]] mutableCopy]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self.tableView reloadData]; }]; } presenter:self]; } } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; if ([self.title isEqualToString:@"Manage"] || [self.title isEqualToString:@"Upload"]) { NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile: [SharedDeclerations savePathForFile:[@"SAVES/" stringByAppendingString: _dataSource[indexPath.row]]]]; cell.textLabel.text = [data valueForKey:@"MENU@LSKR1"]; } if ([self.title isEqualToString:@"Download"]) { cell.textLabel.text = _dataSource[indexPath.row]; } if ([self.title isEqualToString:@"Manage"]) { cell.rightUtilityButtons = [self rightButtons]; } else { cell.leftUtilityButtons = [self leftButtonsNormal]; } cell.delegate = self; return cell; } #pragma mark - Table view cell selection action - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.title isEqualToString:@"Manage"]) { [self dismissViewControllerAnimated:YES completion:^(void) { [_main loadSave:[tableView cellForRowAtIndexPath: indexPath] .textLabel.text]; }]; } else { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } } #pragma mark - Helper methods /** * AKA Delete button */ - (NSArray *)rightButtons { NSMutableArray *array = [[NSMutableArray alloc] init]; [array sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"Delete"]; return array; } /** * AKA Mark button white */ - (NSArray *)leftButtonsNormal { NSMutableArray *array = [[NSMutableArray alloc] init]; [array sw_addUtilityButtonWithColor:[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] title:@"Mark"]; return array; } /** * AKA Mark button green */ - (NSArray *)leftButtonsSelected { NSMutableArray *array = [[NSMutableArray alloc] init]; [array sw_addUtilityButtonWithColor:[UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0] title:@"Unmark"]; return array; } - (void)finishSelection { NSOperationQueue *queue = [[NSOperationQueue alloc] init]; if ([self.title isEqualToString:@"Download"]) { _sm.dbDownload.enabled = NO; _sm.dbDownload.alpha = 0.5; _main.downloadActive = true; [queue addOperationWithBlock:^{ [dbClient downloadFromDropbox:_markList presenter:self completion:^{ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ _sm.dbDownload.enabled = YES; _sm.dbDownload.alpha = 1.0; _main.downloadActive = false; [self.view setNeedsDisplay]; }]; }]; }]; [self.navigationController popViewControllerAnimated:YES]; } if ([self.title isEqualToString:@"Upload"]) { _sm.dbUpload.enabled = NO; _sm.dbUpload.alpha = 0.5; _main.uploadActive = true; [queue addOperationWithBlock:^{ [dbClient uploadToDropbox:_markList presenter:self completion:^{ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ _sm.dbUpload.enabled = YES; _sm.dbUpload.alpha = 1.0; _main.uploadActive = false; [self.view setNeedsDisplay]; }]; }]; }]; [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark - SWTableViewDelegate - (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:[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:[file stringByAppendingString:cell.textLabel.text]]; cell.leftUtilityButtons = [self leftButtonsNormal]; cell.accessoryType = UITableViewCellAccessoryNone; [cell hideUtilityButtonsAnimated:YES]; } if (_markList.count > 0 && self.navigationItem.rightBarButtonItem == nil) { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleDone target:self action:@selector(finishSelection)]; } else if (_markList.count == 0) { self.navigationItem.rightBarButtonItem = nil; } } - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [[NSFileManager defaultManager] removeItemAtPath:[SharedDeclerations savePathForFile:_dataSource[indexPath.row]] error:nil]; [_dataSource removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade]; } @end