Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 7dd55b0bd1d53048329e3034d349bfa330b62dff 0 parents
@danielctull authored
22 DCTNetworkActivityIndicatorController.h
@@ -0,0 +1,22 @@
+//
+// DCTNetworkActivityIndicatorController.h
+// DCTConnectionController
+//
+// Created by Daniel Tull on 07.08.2011.
+// Copyright (c) 2011 Daniel Tull. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+extern NSString *const DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification;
+
+@interface DCTNetworkActivityIndicatorController : NSObject
+
++ (DCTNetworkActivityIndicatorController *)sharedNetworkActivityIndicatorController;
+
+@property (nonatomic, readonly) NSUInteger networkActivity;
+
+- (void)decrementNetworkActivity;
+- (void)incrementNetworkActivity;
+
+@end
94 DCTNetworkActivityIndicatorController.m
@@ -0,0 +1,94 @@
+//
+// DCTNetworkActivityIndicatorController.m
+// DCTConnectionController
+//
+// Created by Daniel Tull on 07.08.2011.
+// Copyright (c) 2011 Daniel Tull. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "DCTNetworkActivityIndicatorController.h"
+#import "NSObject+DCTKVOExtras.h"
+
+// These tie to DCTConnectionQueue
+NSString *const DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification = @"DCTConnectionQueueActiveConnectionCountIncreasedNotification";
+NSString *const DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification = @"DCTConnectionQueueActiveConnectionCountDecreasedNotification";
+
+NSString *const DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey = @"networkActivity";
+
+NSString *const DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification = @"DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification";
+
+static DCTNetworkActivityIndicatorController *sharedInstance = nil;
+
+@interface DCTNetworkActivityIndicatorController ()
+- (void)dctInternal_calculateNetworkActivity;
+@end
+
+@implementation DCTNetworkActivityIndicatorController
+
+@synthesize networkActivity;
+
++ (void)load {
+ [DCTNetworkActivityIndicatorController sharedNetworkActivityIndicatorController];
+}
+
++ (DCTNetworkActivityIndicatorController *)sharedNetworkActivityIndicatorController {
+
+ static dispatch_once_t sharedToken;
+ dispatch_once(&sharedToken, ^{
+ sharedInstance = [[self alloc] init];
+ });
+
+ return sharedInstance;
+}
+
+- (id)init {
+
+ if (!(self = [super init])) return nil;
+
+ networkActivity = 0;
+
+ NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+ [notificationCenter addObserver:self selector:@selector(incrementNetworkActivity) name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification object:nil];
+ [notificationCenter addObserver:self selector:@selector(decrementNetworkActivity) name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification object:nil];
+
+ return self;
+}
+
+- (void)dealloc {
+ NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+
+ [notificationCenter removeObserver:self name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountIncreasedNotification object:nil];
+ [notificationCenter removeObserver:self name:DCTInternal_NetworkActivityIndicatorControllerConnectionQueueActiveConnectionCountDecreasedNotification object:nil];
+}
+
+- (void)decrementNetworkActivity {
+
+ NSAssert((networkActivity > 0), @"%@ increment/decrement calls mismatch", self);
+
+ [self willChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
+ networkActivity--;
+ [self didChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
+
+ [self dctInternal_calculateNetworkActivity];
+}
+
+- (void)incrementNetworkActivity {
+
+ [self willChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
+ networkActivity++;
+ [self didChangeValueForKey:DCTInternal_NetworkActivityIndicatorControllerNetworkActivityKey];
+
+ [self dctInternal_calculateNetworkActivity];
+}
+
+- (void)dctInternal_calculateNetworkActivity {
+ [[NSNotificationCenter defaultCenter] postNotificationName:DCTNetworkActivityIndicatorControllerNetworkActivityChangedNotification object:self];
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(networkActivity > 0)];
+}
+
+- (NSString *)description {
+ return [NSString stringWithFormat:@"<%@ %p; networkActivity = %i>", NSStringFromClass([self class]), self, self.networkActivity];
+}
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.