Explorar o código

Simplify network debugging settings

Having two switches to enable network debugging was confusing. One switch now enables network debugging and persists across launches of the app.
Ryan Olson %!s(int64=10) %!d(string=hai) anos
pai
achega
a64188dd5e

+ 0 - 8
Classes/Network/FLEXNetworkSettingsTableViewController.m

@@ -39,9 +39,6 @@
     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];
 
@@ -64,11 +61,6 @@
     [FLEXNetworkObserver setEnabled:sender.isOn];
 }
 
-- (void)enableOnLaunchToggled:(UISwitch *)sender
-{
-    [FLEXNetworkObserver setShouldEnableOnLaunch:sender.isOn];
-}
-
 - (void)cacheMediaResponsesToggled:(UISwitch *)sender
 {
     [[FLEXNetworkRecorder defaultRecorder] setShouldCacheMediaResponses:sender.isOn];

+ 1 - 5
Classes/Network/PonyDebugger/FLEXNetworkObserver.h

@@ -22,12 +22,8 @@ extern NSString *const kFLEXNetworkObserverEnabledStateChangedNotification;
 
 /// Swizzling occurs when the observer is enabled for the first time.
 /// This reduces the impact of FLEX if network debugging is not desired.
+/// NOTE: this setting persists between launches of the app.
 + (void)setEnabled:(BOOL)enabled;
 + (BOOL)isEnabled;
 
-/// The enable on launch setting is persisted accross launches of the app.
-/// If YES, the observer will automatically enable itself early in the application lifecycle.
-+ (void)setShouldEnableOnLaunch:(BOOL)shouldEnableOnLaunch;
-+ (BOOL)shouldEnableOnLaunch;
-
 @end

+ 12 - 24
Classes/Network/PonyDebugger/FLEXNetworkObserver.m

@@ -20,7 +20,7 @@
 #import <dispatch/queue.h>
 
 NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNetworkObserverEnabledStateChangedNotification";
-static NSString *const kFLEXNetworkObserverEnableOnLaunchDefaultsKey = @"com.flex.FLEXNetworkObserver.enableOnLaunch";
+static NSString *const kFLEXNetworkObserverEnabledDefaultsKey = @"com.flex.FLEXNetworkObserver.enableOnLaunch";
 
 typedef void (^NSURLSessionAsyncCompletion)(id fileURLOrData, NSURLResponse *response, NSError *error);
 
@@ -67,7 +67,6 @@ didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id <NSU
 
 @interface FLEXNetworkObserver ()
 
-@property (nonatomic, assign, getter=isEnabled) BOOL enabled;
 @property (nonatomic, strong) NSMutableDictionary *requestStatesForRequestIDs;
 @property (nonatomic, strong) dispatch_queue_t queue;
 
@@ -79,43 +78,32 @@ didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id <NSU
 
 + (void)setEnabled:(BOOL)enabled
 {
+    BOOL previouslyEnabled = [self isEnabled];
+    
+    [[NSUserDefaults standardUserDefaults] setBool:enabled forKey:kFLEXNetworkObserverEnabledDefaultsKey];
+    
     if (enabled) {
         // Inject if needed. This injection is protected with a dispatch_once, so we're ok calling it multiple times.
         // By doing the injection lazily, we keep the impact of the tool lower when this feature isn't enabled.
         [self injectIntoAllNSURLConnectionDelegateClasses];
     }
-    [[self sharedObserver] setEnabled:enabled];
-}
-
-+ (BOOL)isEnabled
-{
-    return [[self sharedObserver] isEnabled];
-}
-
-- (void)setEnabled:(BOOL)enabled
-{
-    if (_enabled != enabled) {
-        _enabled = enabled;
+    
+    if (previouslyEnabled != enabled) {
         [[NSNotificationCenter defaultCenter] postNotificationName:kFLEXNetworkObserverEnabledStateChangedNotification object:self];
     }
 }
 
-+ (void)setShouldEnableOnLaunch:(BOOL)shouldEnableOnLaunch
-{
-    [[NSUserDefaults standardUserDefaults] setBool:shouldEnableOnLaunch forKey:kFLEXNetworkObserverEnableOnLaunchDefaultsKey];
-}
-
-+ (BOOL)shouldEnableOnLaunch
++ (BOOL)isEnabled
 {
-    return [[[NSUserDefaults standardUserDefaults] objectForKey:kFLEXNetworkObserverEnableOnLaunchDefaultsKey] boolValue];
+    return [[[NSUserDefaults standardUserDefaults] objectForKey:kFLEXNetworkObserverEnabledDefaultsKey] boolValue];
 }
 
 + (void)load
 {
     // We don't want to do the swizzling from +load because not all the classes may be loaded at this point.
     dispatch_async(dispatch_get_main_queue(), ^{
-        if ([self shouldEnableOnLaunch]) {
-            [self setEnabled:YES];
+        if ([self isEnabled]) {
+            [self injectIntoAllNSURLConnectionDelegateClasses];
         }
     });
 }
@@ -972,7 +960,7 @@ static char const * const kFLEXRequestIDKey = "kFLEXRequestIDKey";
 
 - (void)performBlock:(dispatch_block_t)block
 {
-    if (self.isEnabled) {
+    if ([[self class] isEnabled]) {
         dispatch_async(_queue, block);
     }
 }