RASettingViewController+TableDelegate.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. dispatch_async(dispatch_get_main_queue(), ^{
  67. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"are you sure to clean cached file" preferredStyle:UIAlertControllerStyleAlert];
  68. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  69. model.active = NO;
  70. }];
  71. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  72. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  73. NSFileManager *defaultManager = [NSFileManager defaultManager];
  74. NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  75. NSString *imageCacheDir = [cacheDir stringByAppendingPathComponent:@"ImageCache"];
  76. if ([defaultManager fileExistsAtPath:imageCacheDir]) {
  77. [defaultManager removeItemAtPath:imageCacheDir error:nil];
  78. }
  79. // sleep(1.0);
  80. // dispatch_async(dispatch_get_main_queue(), ^{
  81. // model.active = NO;
  82. //
  83. // [self showAlert:@"Clean Success"];
  84. // });
  85. dispatch_after(dispatch_time(1 * NSEC_PER_SEC, DISPATCH_TIME_NOW), dispatch_get_main_queue(), ^{
  86. model.active = NO;
  87. [self showAlert:@"Clean Success"];
  88. });
  89. });
  90. //
  91. }];
  92. [alertVC addAction:cancelAction];
  93. [alertVC addAction:yesAction];
  94. [self presentViewController:alertVC animated:YES completion:nil];
  95. });
  96. }
  97. - (void)changePassword:(RASettingActionModel *)model {
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. RAChangePasswordViewController *vc = [RAChangePasswordViewController viewControllerFromStoryboard];
  100. [self presentViewController:vc animated:YES completion:nil];
  101. });
  102. }
  103. #pragma mark - Show Option
  104. - (void)showOption:(RASettingOptionModel *)model {
  105. if (!model) {
  106. return;
  107. }
  108. dispatch_async(dispatch_get_main_queue(), ^{
  109. RAOptionViewController *optionVC = [RAOptionViewController optionViewControllerWith:model.options selectedIndex:model.selectedIndex];
  110. optionVC.optionTitle = model.title;
  111. optionVC.selectBlk = ^(NSInteger index) {
  112. [model setSelectedIndex:(int)index];
  113. };
  114. [self.navigationController pushViewController:optionVC animated:YES];
  115. });
  116. }
  117. @end