// // RASettingViewController+TableDelegate.m // Apex And Drivers // // Created by Jack on 2018/9/12. // Copyright © 2018年 USAI. All rights reserved. // #import "RASettingViewController+TableDelegate.h" #import "RASettingSectionModel.h" #import "RASettingOptionModel.h" #import "RASettingSwitchModel.h" #import "RASettingActionModel.h" #import "RASettingLinkModel.h" #import "RAChangePasswordViewController.h" #import "RAOptionViewController.h" @implementation RASettingViewController (TableDelegate) - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { RASettingSectionModel *sectionModel = [self.sections objectAtIndex:indexPath.section]; RASettingBaseModel *model = [sectionModel.settings objectAtIndex:indexPath.row]; return model.height; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 30.0f; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [UIView new]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { RASettingSectionModel *sectionModel = [self.sections objectAtIndex:indexPath.section]; RASettingBaseModel *model = [sectionModel.settings objectAtIndex:indexPath.row]; switch (model.type) { case RASettingTypeOption: { RASettingOptionModel *optionModel = (RASettingOptionModel *)model; [self showOption:optionModel]; } break; case RASettingTypeSwitch: { } break; case RASettingTypeAction: { RASettingActionModel *actionModel = (RASettingActionModel *)model; [self clickAction:actionModel]; } break; case RASettingTypeLink: { RASettingLinkModel *linkModel = (RASettingLinkModel *)model; [self clickLink:linkModel]; } break; default: { } break; } } #pragma mark - Private Action - (void)clickAction:(RASettingActionModel *)model { switch (model.actionType) { case RASettingActionTypeCleanCache: { [self cleanDiskCache:model]; } break; case RASettingActionTypeChangePassword: { [self changePassword:model]; } break; default: break; } } - (void)cleanDiskCache:(RASettingActionModel *)model { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"are you sure to clean cached file" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { model.active = YES; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSFileManager *defaultManager = [NSFileManager defaultManager]; NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; NSString *imageCacheDir = [cacheDir stringByAppendingPathComponent:@"ImageCache"]; if ([defaultManager fileExistsAtPath:imageCacheDir]) { [defaultManager removeItemAtPath:imageCacheDir error:nil]; } // sleep(1.0); // dispatch_async(dispatch_get_main_queue(), ^{ // model.active = NO; // // [self showAlert:@"Clean Success"]; // }); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ model.active = NO; [self showAlert:@"Clean Success"]; }); }); // }]; [alertVC addAction:cancelAction]; [alertVC addAction:yesAction]; [self presentViewController:alertVC animated:YES completion:nil]; }); } - (void)changePassword:(RASettingActionModel *)model { dispatch_async(dispatch_get_main_queue(), ^{ RAChangePasswordViewController *vc = [RAChangePasswordViewController viewControllerFromStoryboard]; [self presentViewController:vc animated:YES completion:nil]; }); } #pragma mark - Show Option - (void)showOption:(RASettingOptionModel *)model { if (!model) { return; } dispatch_async(dispatch_get_main_queue(), ^{ RAOptionViewController *optionVC = [RAOptionViewController optionViewControllerWith:model.options selectedIndex:model.selectedIndex]; optionVC.optionTitle = model.title; optionVC.selectBlk = ^(NSInteger index) { [model setSelectedIndex:(int)index]; }; [self.navigationController pushViewController:optionVC animated:YES]; }); } #pragma mark - Link - (void)clickLink:(RASettingLinkModel *)model { NSString *link = model.link; NSURL *url = [NSURL URLWithString:link]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } } @end