Просмотр исходного кода

Add network debugging settings view controller

Ryan Olson лет назад: 11
Родитель
Сommit
cdb9235a83

+ 13 - 0
Classes/Network/FLEXNetworkSettingsTableViewController.h

@@ -0,0 +1,13 @@
+//
+//  FLEXNetworkSettingsTableViewController.h
+//  FLEXInjected
+//
+//  Created by Ryan Olson on 2/20/15.
+//
+//
+
+#import <UIKit/UIKit.h>
+
+@interface FLEXNetworkSettingsTableViewController : UITableViewController
+
+@end

+ 184 - 0
Classes/Network/FLEXNetworkSettingsTableViewController.m

@@ -0,0 +1,184 @@
+//
+//  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 ()
+
+@property (nonatomic, copy) NSArray *cells;
+
+@property (nonatomic, strong) UITableViewCell *cacheLimitCell;
+
+@end
+
+@implementation FLEXNetworkSettingsTableViewController
+
+- (instancetype)initWithStyle:(UITableViewStyle)style
+{
+    self = [super initWithStyle:UITableViewStyleGrouped];
+    if (self) {
+
+    }
+    return self;
+}
+
+- (void)viewDidLoad
+{
+    [super viewDidLoad];
+
+    NSMutableArray *mutableCells = [NSMutableArray array];
+
+    UITableViewCell *networkDebuggingCell = [self switchCellWithTitle:@"Network Debugging" toggleAction:@selector(networkDebuggingToggled:) isOn:[FLEXNetworkObserver isEnabled]];
+    [mutableCells addObject:networkDebuggingCell];
+
+    UITableViewCell *enableOnLaunchCell = [self switchCellWithTitle:@"Enable on Launch" toggleAction:@selector(enableOnLaunchToggled:) isOn:[FLEXNetworkObserver shouldEnableOnLaunch]];
+    [mutableCells addObject:enableOnLaunchCell];
+
+    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)enableOnLaunchToggled:(UISwitch *)sender
+{
+    [FLEXNetworkObserver setShouldEnableOnLaunch: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
+{
+    [[FLEXNetworkRecorder defaultRecorder] clearRecordedActivity];
+}
+
+#pragma mark - Table view data source
+
+- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
+{
+    return 1;
+}
+
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
+{
+    return [self.cells count];
+}
+
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
+{
+    return [self.cells objectAtIndex:indexPath.row];
+}
+
+#pragma mark - Helpers
+
+- (UITableViewCell *)switchCellWithTitle:(NSString *)title toggleAction:(SEL)toggleAction isOn:(BOOL)isOn
+{
+    UITableViewCell *cell = [[UITableViewCell alloc] init];
+    cell.selectionStyle = UITableViewCellSelectionStyleNone;
+    cell.textLabel.text = title;
+    cell.textLabel.font = [[self class] cellTitleFont];
+
+    UISwitch *theSwitch = [[UISwitch alloc] init];
+    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 alloc] init];
+    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 alloc] init];
+    sliderCell.selectionStyle = UITableViewCellSelectionStyleNone;
+    sliderCell.textLabel.text = title;
+    sliderCell.textLabel.font = [[self class] cellTitleFont];
+
+    UISlider *slider = [[UISlider alloc] init];
+    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 [FLEXUtility defaultFontOfSize:14.0];
+}
+
+@end

+ 6 - 0
Example/UICatalog.xcodeproj/project.pbxproj

@@ -105,6 +105,7 @@
 		94C681F31A3E941800E1936D /* FLEXLayerExplorerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 94C681F21A3E941800E1936D /* FLEXLayerExplorerViewController.m */; };
 		94CB48391A8EC6000054A905 /* FLEXMultilineTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 94CB48381A8EC6000054A905 /* FLEXMultilineTableViewCell.m */; };
 		94CB4D431A97183E0054A905 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 94CB4D421A97183E0054A905 /* libz.dylib */; };
+		94CB4D4F1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 94CB4D4E1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.m */; };
 		D03647D919847248007D2A1B /* FLEXGlobalsTableViewControllerEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = D03647D819847248007D2A1B /* FLEXGlobalsTableViewControllerEntry.m */; };
 /* End PBXBuildFile section */
 
@@ -297,6 +298,8 @@
 		94CB48371A8EC6000054A905 /* FLEXMultilineTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXMultilineTableViewCell.h; sourceTree = "<group>"; };
 		94CB48381A8EC6000054A905 /* FLEXMultilineTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXMultilineTableViewCell.m; sourceTree = "<group>"; };
 		94CB4D421A97183E0054A905 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
+		94CB4D4D1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXNetworkSettingsTableViewController.h; sourceTree = "<group>"; };
+		94CB4D4E1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXNetworkSettingsTableViewController.m; sourceTree = "<group>"; };
 		D03647D51984720F007D2A1B /* FLEXManager+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FLEXManager+Private.h"; sourceTree = "<group>"; };
 		D03647D719847248007D2A1B /* FLEXGlobalsTableViewControllerEntry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXGlobalsTableViewControllerEntry.h; sourceTree = "<group>"; };
 		D03647D819847248007D2A1B /* FLEXGlobalsTableViewControllerEntry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXGlobalsTableViewControllerEntry.m; sourceTree = "<group>"; };
@@ -449,6 +452,8 @@
 			children = (
 				9421B87F1A8BBCB200BA3E46 /* FLEXNetworkHistoryTableViewController.h */,
 				9421B8801A8BBCB200BA3E46 /* FLEXNetworkHistoryTableViewController.m */,
+				94CB4D4D1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.h */,
+				94CB4D4E1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.m */,
 				9421B8811A8BBCB200BA3E46 /* FLEXNetworkRecorder.h */,
 				9421B8821A8BBCB200BA3E46 /* FLEXNetworkRecorder.m */,
 				9421B8831A8BBCB200BA3E46 /* FLEXNetworkTransaction.h */,
@@ -786,6 +791,7 @@
 				944F74B0197B458C009AB039 /* FLEXGlobalsTableViewController.m in Sources */,
 				944F748B197B458C009AB039 /* FLEXDefaultsExplorerViewController.m in Sources */,
 				944F749E197B458C009AB039 /* FLEXArgumentInputStructView.m in Sources */,
+				94CB4D4F1A9829F80054A905 /* FLEXNetworkSettingsTableViewController.m in Sources */,
 				944F74B3197B458C009AB039 /* FLEXLiveObjectsTableViewController.m in Sources */,
 				944F748E197B458C009AB039 /* FLEXImageExplorerViewController.m in Sources */,
 				944F74B6197B458C009AB039 /* FLEXHierarchyTableViewController.m in Sources */,