// // 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 #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; if ([self.title isEqualToString:@"Manage"]) { NSArray *temp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[SharedDeclerations savePathForFile:@""] error:nil]; _dataSource = [[temp filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"NOT (SELF BEGINSWITH %@)", @"."]] mutableCopy]; } if ([self.title isEqualToString:@"Download"]) { DropboxV2ObjC *dbClient = ((AppDelegate *)[UIApplication sharedApplication].delegate) .dbClient; [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 { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; if ([self.title isEqualToString:@"Manage"]) { 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]; } return cell; } #pragma mark - Table view editing action - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { // Detemine if it's in editing mode if ([self.title isEqualToString:@"Manage"]) { return UITableViewCellEditingStyleDelete; } return UITableViewCellEditingStyleNone; } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source [[NSFileManager defaultManager] removeItemAtPath:[SharedDeclerations savePathForFile:_dataSource[indexPath.row]] error:nil]; [_dataSource removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into // the // array, and add a new row to the table view } } #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]; }]; } } @end