RASettingViewController+TableDelegate.m 5.5 KB

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