FLEXNetworkSettingsController.m 9.5 KB

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