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

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 лет назад: 10
Родитель
Сommit
a64188dd5e

+ 0 - 8
Classes/Network/FLEXNetworkSettingsTableViewController.m

@@ -39,9 +39,6 @@
     UITableViewCell *networkDebuggingCell = [self switchCellWithTitle:@"Network Debugging" toggleAction:@selector(networkDebuggingToggled:) isOn:[FLEXNetworkObserver isEnabled]];
     UITableViewCell *networkDebuggingCell = [self switchCellWithTitle:@"Network Debugging" toggleAction:@selector(networkDebuggingToggled:) isOn:[FLEXNetworkObserver isEnabled]];
     [mutableCells addObject:networkDebuggingCell];
     [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];
     UITableViewCell *cacheMediaResponsesCell = [self switchCellWithTitle:@"Cache Media Responses" toggleAction:@selector(cacheMediaResponsesToggled:) isOn:NO];
     [mutableCells addObject:cacheMediaResponsesCell];
     [mutableCells addObject:cacheMediaResponsesCell];
 
 
@@ -64,11 +61,6 @@
     [FLEXNetworkObserver setEnabled:sender.isOn];
     [FLEXNetworkObserver setEnabled:sender.isOn];
 }
 }
 
 
-- (void)enableOnLaunchToggled:(UISwitch *)sender
-{
-    [FLEXNetworkObserver setShouldEnableOnLaunch:sender.isOn];
-}
-
 - (void)cacheMediaResponsesToggled:(UISwitch *)sender
 - (void)cacheMediaResponsesToggled:(UISwitch *)sender
 {
 {
     [[FLEXNetworkRecorder defaultRecorder] setShouldCacheMediaResponses:sender.isOn];
     [[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.
 /// Swizzling occurs when the observer is enabled for the first time.
 /// This reduces the impact of FLEX if network debugging is not desired.
 /// 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;
 + (void)setEnabled:(BOOL)enabled;
 + (BOOL)isEnabled;
 + (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
 @end

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

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