FLEXNetworkSettingsTableViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // FLEXNetworkSettingsTableViewController.m
  3. // FLEXInjected
  4. //
  5. // Created by Ryan Olson on 2/20/15.
  6. //
  7. //
  8. #import "FLEXNetworkSettingsTableViewController.h"
  9. #import "FLEXNetworkObserver.h"
  10. #import "FLEXNetworkRecorder.h"
  11. #import "FLEXUtility.h"
  12. @interface FLEXNetworkSettingsTableViewController () <UIActionSheetDelegate>
  13. @property (nonatomic, copy) NSArray<UITableViewCell *> *cells;
  14. @property (nonatomic) UITableViewCell *cacheLimitCell;
  15. @end
  16. @implementation FLEXNetworkSettingsTableViewController
  17. - (instancetype)initWithStyle:(UITableViewStyle)style {
  18. self = [super initWithStyle:UITableViewStyleGrouped];
  19. if (self) {
  20. }
  21. return self;
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. NSMutableArray<UITableViewCell *> *mutableCells = [NSMutableArray array];
  26. UITableViewCell *networkDebuggingCell = [self switchCellWithTitle:@"Network Debugging" toggleAction:@selector(networkDebuggingToggled:) isOn:FLEXNetworkObserver.isEnabled];
  27. [mutableCells addObject:networkDebuggingCell];
  28. UITableViewCell *cacheMediaResponsesCell = [self switchCellWithTitle:@"Cache Media Responses" toggleAction:@selector(cacheMediaResponsesToggled:) isOn:NO];
  29. [mutableCells addObject:cacheMediaResponsesCell];
  30. NSUInteger currentCacheLimit = [[FLEXNetworkRecorder defaultRecorder] responseCacheByteLimit];
  31. const NSUInteger fiftyMega = 50 * 1024 * 1024;
  32. NSString *cacheLimitTitle = [self titleForCacheLimitCellWithValue:currentCacheLimit];
  33. self.cacheLimitCell = [self sliderCellWithTitle:cacheLimitTitle changedAction:@selector(cacheLimitAdjusted:) minimum:0.0 maximum:fiftyMega initialValue:currentCacheLimit];
  34. [mutableCells addObject:self.cacheLimitCell];
  35. UITableViewCell *clearRecordedRequestsCell = [self buttonCellWithTitle:@"❌ Clear Recorded Requests" touchUpAction:@selector(clearRequestsTapped:) isDestructive:YES];
  36. [mutableCells addObject:clearRecordedRequestsCell];
  37. self.cells = mutableCells;
  38. }
  39. #pragma mark - Settings Actions
  40. - (void)networkDebuggingToggled:(UISwitch *)sender {
  41. [FLEXNetworkObserver setEnabled:sender.isOn];
  42. }
  43. - (void)cacheMediaResponsesToggled:(UISwitch *)sender {
  44. [[FLEXNetworkRecorder defaultRecorder] setShouldCacheMediaResponses:sender.isOn];
  45. }
  46. - (void)cacheLimitAdjusted:(UISlider *)sender {
  47. [[FLEXNetworkRecorder defaultRecorder] setResponseCacheByteLimit:sender.value];
  48. self.cacheLimitCell.textLabel.text = [self titleForCacheLimitCellWithValue:sender.value];
  49. }
  50. - (void)clearRequestsTapped:(UIButton *)sender {
  51. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  52. make.button(@"Cancel").cancelStyle();
  53. make.button(@"Clear Recorded Requests").destructiveStyle().handler(^(NSArray *strings) {
  54. [[FLEXNetworkRecorder defaultRecorder] clearRecordedActivity];
  55. });
  56. } showFrom:self];
  57. self.popoverPresentationController.sourceView = sender;
  58. self.popoverPresentationController.sourceRect = sender.bounds;
  59. }
  60. #pragma mark - Table view data source
  61. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  62. return 1;
  63. }
  64. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  65. return self.cells.count;
  66. }
  67. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  68. return self.cells[indexPath.row];
  69. }
  70. #pragma mark - Helpers
  71. - (UITableViewCell *)switchCellWithTitle:(NSString *)title toggleAction:(SEL)toggleAction isOn:(BOOL)isOn {
  72. UITableViewCell *cell = [UITableViewCell new];
  73. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  74. cell.textLabel.text = title;
  75. cell.textLabel.font = [[self class] cellTitleFont];
  76. UISwitch *theSwitch = [UISwitch new];
  77. theSwitch.on = isOn;
  78. [theSwitch addTarget:self action:toggleAction forControlEvents:UIControlEventValueChanged];
  79. CGFloat switchOriginY = round((cell.contentView.frame.size.height - theSwitch.frame.size.height) / 2.0);
  80. CGFloat switchOriginX = CGRectGetMaxX(cell.contentView.frame) - theSwitch.frame.size.width - self.tableView.separatorInset.left;
  81. theSwitch.frame = CGRectMake(switchOriginX, switchOriginY, theSwitch.frame.size.width, theSwitch.frame.size.height);
  82. theSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  83. [cell.contentView addSubview:theSwitch];
  84. return cell;
  85. }
  86. - (UITableViewCell *)buttonCellWithTitle:(NSString *)title touchUpAction:(SEL)action isDestructive:(BOOL)isDestructive {
  87. UITableViewCell *buttonCell = [UITableViewCell new];
  88. buttonCell.selectionStyle = UITableViewCellSelectionStyleNone;
  89. UIButton *actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
  90. [actionButton setTitle:title forState:UIControlStateNormal];
  91. if (isDestructive) {
  92. actionButton.tintColor = UIColor.redColor;
  93. }
  94. actionButton.titleLabel.font = [[self class] cellTitleFont];
  95. [actionButton addTarget:self action:@selector(clearRequestsTapped:) forControlEvents:UIControlEventTouchUpInside];
  96. [buttonCell.contentView addSubview:actionButton];
  97. actionButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  98. actionButton.frame = buttonCell.contentView.frame;
  99. actionButton.contentEdgeInsets = UIEdgeInsetsMake(0.0, self.tableView.separatorInset.left, 0.0, self.tableView.separatorInset.left);
  100. actionButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  101. return buttonCell;
  102. }
  103. - (NSString *)titleForCacheLimitCellWithValue:(long long)cacheLimit {
  104. NSInteger limitInMB = round(cacheLimit / (1024 * 1024));
  105. return [NSString stringWithFormat:@"Cache Limit (%ld MB)", (long)limitInMB];
  106. }
  107. - (UITableViewCell *)sliderCellWithTitle:(NSString *)title changedAction:(SEL)changedAction minimum:(CGFloat)minimum maximum:(CGFloat)maximum initialValue:(CGFloat)initialValue {
  108. UITableViewCell *sliderCell = [UITableViewCell new];
  109. sliderCell.selectionStyle = UITableViewCellSelectionStyleNone;
  110. sliderCell.textLabel.text = title;
  111. sliderCell.textLabel.font = [[self class] cellTitleFont];
  112. UISlider *slider = [UISlider new];
  113. slider.minimumValue = minimum;
  114. slider.maximumValue = maximum;
  115. slider.value = initialValue;
  116. [slider addTarget:self action:changedAction forControlEvents:UIControlEventValueChanged];
  117. [slider sizeToFit];
  118. CGFloat sliderWidth = round(sliderCell.contentView.frame.size.width * 2.0 / 5.0);
  119. CGFloat sliderOriginY = round((sliderCell.contentView.frame.size.height - slider.frame.size.height) / 2.0);
  120. CGFloat sliderOriginX = CGRectGetMaxX(sliderCell.contentView.frame) - sliderWidth - self.tableView.separatorInset.left;
  121. slider.frame = CGRectMake(sliderOriginX, sliderOriginY, sliderWidth, slider.frame.size.height);
  122. slider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  123. [sliderCell.contentView addSubview:slider];
  124. return sliderCell;
  125. }
  126. + (UIFont *)cellTitleFont {
  127. return [UIFont systemFontOfSize:14.0];
  128. }
  129. @end