FLEXNetworkSettingsController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // FLEXNetworkSettingsController.m
  3. // FLEXInjected
  4. //
  5. // Created by Ryan Olson on 2/20/15.
  6. //
  7. #import "FLEXNetworkSettingsController.h"
  8. #import "FLEXNetworkObserver.h"
  9. #import "FLEXNetworkRecorder.h"
  10. #import "FLEXUtility.h"
  11. #import "FLEXTableView.h"
  12. #import "FLEXColor.h"
  13. @interface FLEXNetworkSettingsController () <UIActionSheetDelegate>
  14. @property (nonatomic) float cacheLimitValue;
  15. @property (nonatomic, readonly) NSString *cacheLimitCellTitle;
  16. @property (nonatomic, readonly) UISwitch *observerSwitch;
  17. @property (nonatomic, readonly) UISwitch *cacheMediaSwitch;
  18. @property (nonatomic, readonly) UISlider *cacheLimitSlider;
  19. @property (nonatomic) UILabel *cacheLimitLabel;
  20. @property (nonatomic) NSMutableArray<NSString *> *hostBlacklist;
  21. @end
  22. @implementation FLEXNetworkSettingsController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self disableToolbar];
  26. self.hostBlacklist = FLEXNetworkRecorder.defaultRecorder.hostBlacklist.mutableCopy;
  27. _observerSwitch = [UISwitch new];
  28. _cacheMediaSwitch = [UISwitch new];
  29. _cacheLimitSlider = [UISlider new];
  30. self.observerSwitch.on = FLEXNetworkObserver.enabled;
  31. [self.observerSwitch addTarget:self
  32. action:@selector(networkDebuggingToggled:)
  33. forControlEvents:UIControlEventValueChanged
  34. ];
  35. self.cacheMediaSwitch.on = FLEXNetworkRecorder.defaultRecorder.shouldCacheMediaResponses;
  36. [self.cacheMediaSwitch addTarget:self
  37. action:@selector(cacheMediaResponsesToggled:)
  38. forControlEvents:UIControlEventValueChanged
  39. ];
  40. [self.cacheLimitSlider addTarget:self
  41. action:@selector(cacheLimitAdjusted:)
  42. forControlEvents:UIControlEventValueChanged
  43. ];
  44. UISlider *slider = self.cacheLimitSlider;
  45. self.cacheLimitValue = FLEXNetworkRecorder.defaultRecorder.responseCacheByteLimit;
  46. const NSUInteger fiftyMega = 50 * 1024 * 1024;
  47. slider.minimumValue = 0;
  48. slider.maximumValue = fiftyMega;
  49. slider.value = self.cacheLimitValue;
  50. }
  51. - (void)setCacheLimitValue:(float)cacheLimitValue {
  52. _cacheLimitValue = cacheLimitValue;
  53. self.cacheLimitLabel.text = self.cacheLimitCellTitle;
  54. [FLEXNetworkRecorder.defaultRecorder setResponseCacheByteLimit:cacheLimitValue];
  55. }
  56. - (NSString *)cacheLimitCellTitle {
  57. NSInteger cacheLimit = self.cacheLimitValue;
  58. NSInteger limitInMB = round(cacheLimit / (1024 * 1024));
  59. return [NSString stringWithFormat:@"Cache Limit (%@ MB)", @(limitInMB)];
  60. }
  61. #pragma mark - Settings Actions
  62. - (void)networkDebuggingToggled:(UISwitch *)sender {
  63. FLEXNetworkObserver.enabled = sender.isOn;
  64. }
  65. - (void)cacheMediaResponsesToggled:(UISwitch *)sender {
  66. FLEXNetworkRecorder.defaultRecorder.shouldCacheMediaResponses = sender.isOn;
  67. }
  68. - (void)cacheLimitAdjusted:(UISlider *)sender {
  69. self.cacheLimitValue = sender.value;
  70. }
  71. #pragma mark - Table View
  72. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  73. return self.hostBlacklist.count ? 2 : 1;
  74. }
  75. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  76. switch (section) {
  77. case 0: return 4;
  78. case 1: return self.hostBlacklist.count;
  79. default: return 0;
  80. }
  81. }
  82. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  83. switch (section) {
  84. case 0: return @"General";
  85. case 1: return @"Host Blacklist";
  86. default: return nil;
  87. }
  88. }
  89. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  90. UITableViewCell *cell = [self.tableView
  91. dequeueReusableCellWithIdentifier:kFLEXDefaultCell forIndexPath:indexPath
  92. ];
  93. cell.accessoryView = nil;
  94. cell.textLabel.textColor = FLEXColor.primaryTextColor;
  95. switch (indexPath.section) {
  96. // Settings
  97. case 0: {
  98. switch (indexPath.row) {
  99. case 0:
  100. cell.textLabel.text = @"Network Debugging";
  101. cell.accessoryView = self.observerSwitch;
  102. break;
  103. case 1:
  104. cell.textLabel.text = @"Cache Media Responses";
  105. cell.accessoryView = self.cacheMediaSwitch;
  106. break;
  107. case 2:
  108. cell.textLabel.text = @"Reset Host Blacklist";
  109. cell.textLabel.textColor = tableView.tintColor;
  110. break;
  111. case 3:
  112. cell.textLabel.text = self.cacheLimitCellTitle;
  113. self.cacheLimitLabel = cell.textLabel;
  114. [self.cacheLimitSlider removeFromSuperview];
  115. [cell.contentView addSubview:self.cacheLimitSlider];
  116. CGRect container = cell.contentView.frame;
  117. UISlider *slider = self.cacheLimitSlider;
  118. [slider sizeToFit];
  119. CGFloat sliderWidth = 150.f;
  120. CGFloat sliderOriginY = FLEXFloor((container.size.height - slider.frame.size.height) / 2.0);
  121. CGFloat sliderOriginX = CGRectGetMaxX(container) - sliderWidth - tableView.separatorInset.left;
  122. self.cacheLimitSlider.frame = CGRectMake(
  123. sliderOriginX, sliderOriginY, sliderWidth, slider.frame.size.height
  124. );
  125. // Make wider, keep in middle of cell, keep to trailing edge of cell
  126. self.cacheLimitSlider.autoresizingMask = ({
  127. UIViewAutoresizingFlexibleWidth |
  128. UIViewAutoresizingFlexibleLeftMargin |
  129. UIViewAutoresizingFlexibleTopMargin |
  130. UIViewAutoresizingFlexibleBottomMargin;
  131. });
  132. break;
  133. }
  134. break;
  135. }
  136. // Blacklist entries
  137. case 1: {
  138. cell.textLabel.text = self.hostBlacklist[indexPath.row];
  139. break;
  140. }
  141. default:
  142. @throw NSInternalInconsistencyException;
  143. break;
  144. }
  145. return cell;
  146. }
  147. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)ip {
  148. // Can only select the "Reset Host Blacklist" row
  149. return ip.section == 0 && ip.row == 2;
  150. }
  151. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  152. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  153. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  154. make.title(@"Reset Host Blacklist");
  155. make.message(@"You cannot undo this action. Are you sure?");
  156. make.button(@"Reset").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  157. self.hostBlacklist = nil;
  158. [FLEXNetworkRecorder.defaultRecorder.hostBlacklist removeAllObjects];
  159. [FLEXNetworkRecorder.defaultRecorder synchronizeBlacklist];
  160. [self.tableView deleteSections:
  161. [NSIndexSet indexSetWithIndex:1]
  162. withRowAnimation:UITableViewRowAnimationAutomatic];
  163. });
  164. make.button(@"Cancel").cancelStyle();
  165. } showFrom:self];
  166. }
  167. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  168. return indexPath.section == 1;
  169. }
  170. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)style
  171. forRowAtIndexPath:(NSIndexPath *)indexPath {
  172. NSParameterAssert(style == UITableViewCellEditingStyleDelete);
  173. NSString *host = self.hostBlacklist[indexPath.row];
  174. [self.hostBlacklist removeObjectAtIndex:indexPath.row];
  175. [FLEXNetworkRecorder.defaultRecorder.hostBlacklist removeObject:host];
  176. [FLEXNetworkRecorder.defaultRecorder synchronizeBlacklist];
  177. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  178. }
  179. @end