RASettingViewController+TableDelegate.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // RASettingViewController+TableDelegate.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/9/12.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RASettingViewController+TableDelegate.h"
  9. #import "RASettingSectionModel.h"
  10. #import "RASettingOptionModel.h"
  11. #import "RASettingSwitchModel.h"
  12. #import "RASettingActionModel.h"
  13. #import "RAOptionViewController.h"
  14. @implementation RASettingViewController (TableDelegate)
  15. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  16. RASettingSectionModel *sectionModel = [self.sections objectAtIndex:indexPath.section];
  17. RASettingBaseModel *model = [sectionModel.settings objectAtIndex:indexPath.row];
  18. return model.height;
  19. }
  20. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  21. return 30.0f;
  22. }
  23. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  24. return [UIView new];
  25. }
  26. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  27. RASettingSectionModel *sectionModel = [self.sections objectAtIndex:indexPath.section];
  28. RASettingBaseModel *model = [sectionModel.settings objectAtIndex:indexPath.row];
  29. switch (model.type) {
  30. case RASettingTypeOption: {
  31. RASettingOptionModel *optionModel = (RASettingOptionModel *)model;
  32. [self showOption:optionModel];
  33. }
  34. break;
  35. case RASettingTypeSwitch: {
  36. }
  37. break;
  38. case RASettingTypeAction: {
  39. RASettingActionModel *actionModel = (RASettingActionModel *)model;
  40. [self clickAction:actionModel];
  41. }
  42. break;
  43. default: {
  44. }
  45. break;
  46. }
  47. }
  48. #pragma mark - Private Action
  49. - (void)clickAction:(RASettingActionModel *)model {
  50. switch (model.actionType) {
  51. case RASettingActionTypeCleanCache: {
  52. [self cleanDiskCache:model];
  53. }
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. - (void)cleanDiskCache:(RASettingActionModel *)model {
  60. model.active = YES;
  61. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"are you sure to clean cached file" preferredStyle:UIAlertControllerStyleAlert];
  62. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  63. model.active = NO;
  64. }];
  65. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  66. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  67. NSFileManager *defaultManager = [NSFileManager defaultManager];
  68. NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  69. if ([defaultManager fileExistsAtPath:cacheDir]) {
  70. [defaultManager removeItemAtPath:cacheDir error:nil];
  71. }
  72. sleep(1.0);
  73. dispatch_async(dispatch_get_main_queue(), ^{
  74. model.active = NO;
  75. });
  76. });
  77. }];
  78. [alertVC addAction:cancelAction];
  79. [alertVC addAction:yesAction];
  80. [self presentViewController:alertVC animated:YES completion:nil];
  81. }
  82. #pragma mark - Show Option
  83. - (void)showOption:(RASettingOptionModel *)model {
  84. if (!model) {
  85. return;
  86. }
  87. RAOptionViewController *optionVC = [RAOptionViewController optionViewControllerWith:model.options selectedIndex:model.selectedIndex];
  88. optionVC.optionTitle = model.title;
  89. optionVC.selectBlk = ^(NSInteger index) {
  90. [model setSelectedIndex:(int)index];
  91. };
  92. [self.navigationController pushViewController:optionVC animated:YES];
  93. }
  94. @end