FLEXNetworkSettingsController.m 9.0 KB

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