// // 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:@""] error:nil]; _dataSource = [[temp filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"NOT (SELF BEGINSWITH %@)", @"."]] mutableCopy]; } if ([self.title isEqualToString:@"Download"]) { [dbClient contentsOfPath:nil 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]; [self.tableView reloadData]; }]; } } #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:_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 { if ([self.title isEqualToString:@"Download"]) { [dbClient downloadFromDropbox:_markList]; [self.navigationController popViewControllerAnimated:YES]; } if ([self.title isEqualToString:@"Upload"]) { [dbClient uploadToDropbox:_markList]; [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark - SWTableViewDelegate - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index { if ([((UIButton *)cell.leftUtilityButtons[index]) .titleLabel.text isEqualToString:@"Mark"]) { [_markList addObject: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]; 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