RASettingViewController+TableDelegate.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "RAChangePasswordViewController.h"
  14. #import "RAOptionViewController.h"
  15. @implementation RASettingViewController (TableDelegate)
  16. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  17. RASettingSectionModel *sectionModel = [self.sections objectAtIndex:indexPath.section];
  18. RASettingBaseModel *model = [sectionModel.settings objectAtIndex:indexPath.row];
  19. return model.height;
  20. }
  21. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  22. return 30.0f;
  23. }
  24. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  25. return [UIView new];
  26. }
  27. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  28. RASettingSectionModel *sectionModel = [self.sections objectAtIndex:indexPath.section];
  29. RASettingBaseModel *model = [sectionModel.settings objectAtIndex:indexPath.row];
  30. switch (model.type) {
  31. case RASettingTypeOption: {
  32. RASettingOptionModel *optionModel = (RASettingOptionModel *)model;
  33. [self showOption:optionModel];
  34. }
  35. break;
  36. case RASettingTypeSwitch: {
  37. }
  38. break;
  39. case RASettingTypeAction: {
  40. RASettingActionModel *actionModel = (RASettingActionModel *)model;
  41. [self clickAction:actionModel];
  42. }
  43. break;
  44. default: {
  45. }
  46. break;
  47. }
  48. }
  49. #pragma mark - Private Action
  50. - (void)clickAction:(RASettingActionModel *)model {
  51. switch (model.actionType) {
  52. case RASettingActionTypeCleanCache: {
  53. [self cleanDiskCache:model];
  54. }
  55. break;
  56. case RASettingActionTypeChangePassword: {
  57. [self changePassword:model];
  58. }
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. - (void)cleanDiskCache:(RASettingActionModel *)model {
  65. model.active = YES;
  66. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"are you sure to clean cached file" preferredStyle:UIAlertControllerStyleAlert];
  67. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  68. model.active = NO;
  69. }];
  70. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  71. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  72. NSFileManager *defaultManager = [NSFileManager defaultManager];
  73. NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  74. if ([defaultManager fileExistsAtPath:cacheDir]) {
  75. [defaultManager removeItemAtPath:cacheDir error:nil];
  76. }
  77. sleep(1.0);
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. model.active = NO;
  80. [self showAlert:@"Clean Success"];
  81. });
  82. });
  83. }];
  84. [alertVC addAction:cancelAction];
  85. [alertVC addAction:yesAction];
  86. [self presentViewController:alertVC animated:YES completion:nil];
  87. }
  88. - (void)changePassword:(RASettingActionModel *)model {
  89. RAChangePasswordViewController *vc = [RAChangePasswordViewController viewControllerFromStoryboard];
  90. [self presentViewController:vc animated:YES completion:nil];
  91. }
  92. #pragma mark - Show Option
  93. - (void)showOption:(RASettingOptionModel *)model {
  94. if (!model) {
  95. return;
  96. }
  97. RAOptionViewController *optionVC = [RAOptionViewController optionViewControllerWith:model.options selectedIndex:model.selectedIndex];
  98. optionVC.optionTitle = model.title;
  99. optionVC.selectBlk = ^(NSInteger index) {
  100. [model setSelectedIndex:(int)index];
  101. };
  102. [self.navigationController pushViewController:optionVC animated:YES];
  103. }
  104. @end