| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // FLEXNetworkSettingsTableViewController.m
- // FLEXInjected
- //
- // Created by Ryan Olson on 2/20/15.
- //
- //
- #import "FLEXNetworkSettingsTableViewController.h"
- #import "FLEXNetworkObserver.h"
- #import "FLEXNetworkRecorder.h"
- #import "FLEXUtility.h"
- @interface FLEXNetworkSettingsTableViewController () <UIActionSheetDelegate>
- @property (nonatomic, copy) NSArray<UITableViewCell *> *cells;
- @property (nonatomic) UITableViewCell *cacheLimitCell;
- @end
- @implementation FLEXNetworkSettingsTableViewController
- - (instancetype)initWithStyle:(UITableViewStyle)style {
- self = [super initWithStyle:UITableViewStyleGrouped];
- if (self) {
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSMutableArray<UITableViewCell *> *mutableCells = [NSMutableArray new];
- UITableViewCell *networkDebuggingCell = [self switchCellWithTitle:@"Network Debugging" toggleAction:@selector(networkDebuggingToggled:) isOn:FLEXNetworkObserver.isEnabled];
- [mutableCells addObject:networkDebuggingCell];
- UITableViewCell *cacheMediaResponsesCell = [self switchCellWithTitle:@"Cache Media Responses" toggleAction:@selector(cacheMediaResponsesToggled:) isOn:NO];
- [mutableCells addObject:cacheMediaResponsesCell];
- NSUInteger currentCacheLimit = [FLEXNetworkRecorder.defaultRecorder responseCacheByteLimit];
- const NSUInteger fiftyMega = 50 * 1024 * 1024;
- NSString *cacheLimitTitle = [self titleForCacheLimitCellWithValue:currentCacheLimit];
- self.cacheLimitCell = [self sliderCellWithTitle:cacheLimitTitle changedAction:@selector(cacheLimitAdjusted:) minimum:0.0 maximum:fiftyMega initialValue:currentCacheLimit];
- [mutableCells addObject:self.cacheLimitCell];
- UITableViewCell *clearRecordedRequestsCell = [self buttonCellWithTitle:@"❌ Clear Recorded Requests" touchUpAction:@selector(clearRequestsTapped:) isDestructive:YES];
- [mutableCells addObject:clearRecordedRequestsCell];
- self.cells = mutableCells;
- }
- #pragma mark - Settings Actions
- - (void)networkDebuggingToggled:(UISwitch *)sender {
- [FLEXNetworkObserver setEnabled:sender.isOn];
- }
- - (void)cacheMediaResponsesToggled:(UISwitch *)sender {
- [FLEXNetworkRecorder.defaultRecorder setShouldCacheMediaResponses:sender.isOn];
- }
- - (void)cacheLimitAdjusted:(UISlider *)sender {
- [FLEXNetworkRecorder.defaultRecorder setResponseCacheByteLimit:sender.value];
- self.cacheLimitCell.textLabel.text = [self titleForCacheLimitCellWithValue:sender.value];
- }
- - (void)clearRequestsTapped:(UIButton *)sender {
- [FLEXAlert makeSheet:^(FLEXAlert *make) {
- make.button(@"Cancel").cancelStyle();
- make.button(@"Clear Recorded Requests").destructiveStyle().handler(^(NSArray *strings) {
- [FLEXNetworkRecorder.defaultRecorder clearRecordedActivity];
- });
- } showFrom:self];
- self.popoverPresentationController.sourceView = sender;
- self.popoverPresentationController.sourceRect = sender.bounds;
- }
- #pragma mark - Table View Data Source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.cells.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- return self.cells[indexPath.row];
- }
- #pragma mark - Helpers
- - (UITableViewCell *)switchCellWithTitle:(NSString *)title toggleAction:(SEL)toggleAction isOn:(BOOL)isOn {
- UITableViewCell *cell = [UITableViewCell new];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.textLabel.text = title;
- cell.textLabel.font = [[self class] cellTitleFont];
- UISwitch *theSwitch = [UISwitch new];
- theSwitch.on = isOn;
- [theSwitch addTarget:self action:toggleAction forControlEvents:UIControlEventValueChanged];
- CGFloat switchOriginY = round((cell.contentView.frame.size.height - theSwitch.frame.size.height) / 2.0);
- CGFloat switchOriginX = CGRectGetMaxX(cell.contentView.frame) - theSwitch.frame.size.width - self.tableView.separatorInset.left;
- theSwitch.frame = CGRectMake(switchOriginX, switchOriginY, theSwitch.frame.size.width, theSwitch.frame.size.height);
- theSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
- [cell.contentView addSubview:theSwitch];
- return cell;
- }
- - (UITableViewCell *)buttonCellWithTitle:(NSString *)title touchUpAction:(SEL)action isDestructive:(BOOL)isDestructive {
- UITableViewCell *buttonCell = [UITableViewCell new];
- buttonCell.selectionStyle = UITableViewCellSelectionStyleNone;
- UIButton *actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
- [actionButton setTitle:title forState:UIControlStateNormal];
- if (isDestructive) {
- actionButton.tintColor = UIColor.redColor;
- }
- actionButton.titleLabel.font = [[self class] cellTitleFont];
- [actionButton addTarget:self action:@selector(clearRequestsTapped:) forControlEvents:UIControlEventTouchUpInside];
- [buttonCell.contentView addSubview:actionButton];
- actionButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- actionButton.frame = buttonCell.contentView.frame;
- actionButton.contentEdgeInsets = UIEdgeInsetsMake(0.0, self.tableView.separatorInset.left, 0.0, self.tableView.separatorInset.left);
- actionButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- return buttonCell;
- }
- - (NSString *)titleForCacheLimitCellWithValue:(long long)cacheLimit {
- NSInteger limitInMB = round(cacheLimit / (1024 * 1024));
- return [NSString stringWithFormat:@"Cache Limit (%ld MB)", (long)limitInMB];
- }
- - (UITableViewCell *)sliderCellWithTitle:(NSString *)title changedAction:(SEL)changedAction minimum:(CGFloat)minimum maximum:(CGFloat)maximum initialValue:(CGFloat)initialValue {
- UITableViewCell *sliderCell = [UITableViewCell new];
- sliderCell.selectionStyle = UITableViewCellSelectionStyleNone;
- sliderCell.textLabel.text = title;
- sliderCell.textLabel.font = [[self class] cellTitleFont];
- UISlider *slider = [UISlider new];
- slider.minimumValue = minimum;
- slider.maximumValue = maximum;
- slider.value = initialValue;
- [slider addTarget:self action:changedAction forControlEvents:UIControlEventValueChanged];
- [slider sizeToFit];
- CGFloat sliderWidth = round(sliderCell.contentView.frame.size.width * 2.0 / 5.0);
- CGFloat sliderOriginY = round((sliderCell.contentView.frame.size.height - slider.frame.size.height) / 2.0);
- CGFloat sliderOriginX = CGRectGetMaxX(sliderCell.contentView.frame) - sliderWidth - self.tableView.separatorInset.left;
- slider.frame = CGRectMake(sliderOriginX, sliderOriginY, sliderWidth, slider.frame.size.height);
- slider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
- [sliderCell.contentView addSubview:slider];
- return sliderCell;
- }
- + (UIFont *)cellTitleFont {
- return [UIFont systemFontOfSize:14.0];
- }
- @end
|