FLEXNetworkSettingsController.m 9.6 KB

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