Skip to content

Commit

Permalink
Issue #2 - Added reachability notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josip Bernat committed Apr 28, 2014
1 parent d0fc0db commit 185b8e9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
35 changes: 34 additions & 1 deletion JBMessage/JBMessage/JBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ typedef NS_ENUM(NSInteger, JBRequestSerializerType) {
JBRequestSerializerTypePropertyList /// AFPropertyListRequestSerializer
};

typedef NS_ENUM(NSInteger, JBMessageReachabilityStatus) {
JBMessageReachabilityStatusUnknown = -1, /// AFNetworkReachabilityStatusUnknown
JBMessageReachabilityStatusNotReachable = 0, /// AFNetworkReachabilityStatusNotReachable
JBMessageReachabilityStatusReachableViaWiFi = 1, /// AFNetworkReachabilityStatusReachableViaWiFi
JBMessageReachabilityStatusReachableViaWWAN = 2 /// AFNetworkReachabilityStatusReachableViaWWAN
};

/**
* NSNotification posted when reachability status changes. UserInfo dictionary contains NSNumber with JBMessageReachabilityStatus under JBMessageReachabilityStatusKey key.
*/
extern NSString * const JBMessageReachabilityStatusChangedNotification;

/**
* UserInfo dictionary key which contains JBMessageReachabilityStatus status value.
*/
extern NSString * const JBMessageReachabilityStatusKey;

/**
* Block object containing response object and error. Used as callback when request is done with execution.
*
Expand Down Expand Up @@ -149,7 +166,23 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, long long totalBytesRead,
*/
+ (void)requsterMaxNumberOfConcurrentMessages:(NSUInteger)maxConcurrentMessages;

#pragma mark - Operation Controll
#pragma mark - Reachability

/**
* Current reachability status.
*
* @return JBMessageReachabilityStatus value holding current reachability status.
*/
+ (JBMessageReachabilityStatus)reachabilityStatus;

/**
* Determents if internet is reachable or not using reachabilityStatus.
*
* @return Boolean value determening if internet is reachable.
*/
+ (BOOL)isInternetReachable;

#pragma mark - Operation Control

/**
* Called when operation has started with the job inside NSOperationQueue. You may wish to override this method on your subclass if you need to make some aditional config before executing request. You must call super operationDidStart in order to execute request.
Expand Down
47 changes: 46 additions & 1 deletion JBMessage/JBMessage/JBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
JBHTTPMethod const JBHTTPMethodPUT = @"PUT";
JBHTTPMethod const JBHTTPMethodDELETE = @"DELETE";

NSString * const JBMessageReachabilityStatusChangedNotification = @"JBMessageReachabilityStatusChangedNotification";
NSString * const JBMessageReachabilityStatusKey = @"JBMessageReachabilityStatusKey";

static dispatch_queue_t jb_message_completion_callback_queue() {

static dispatch_queue_t completion_queue;
Expand All @@ -35,6 +38,9 @@ @interface JBMessage () {
#pragma mark - Shared Queue
+ (NSOperationQueue *)sharedQueue;

#pragma mark - Shared Instance
+ (instancetype)sharedInstance;

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTaskIdentifier;

@end
Expand Down Expand Up @@ -73,6 +79,35 @@ + (NSOperationQueue *)sharedQueue {
return queue;
}

#pragma mark - Shared Instance

+ (void)load {
[self sharedInstance];
}

+ (instancetype)sharedInstance {

static JBMessage *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
[instance updateReachability];
});
return instance;
}

- (void)updateReachability {

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

[[NSNotificationCenter defaultCenter] postNotificationName:JBMessageReachabilityStatusChangedNotification
object:nil
userInfo:@{JBMessageReachabilityStatusKey: @(status)}];
}];

[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

#pragma mark - URL Registration

static NSString *baseUrlString = nil;
Expand All @@ -90,6 +125,16 @@ + (void)requsterMaxNumberOfConcurrentMessages:(NSUInteger)maxConcurrentMessages
[[JBMessage sharedQueue] setMaxConcurrentOperationCount:maxConcurrentMessages];
}

#pragma mark - Reachability

+ (JBMessageReachabilityStatus)reachabilityStatus {
return (JBMessageReachabilityStatus) [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus];
}

+ (BOOL)isInternetReachable {
return [[AFNetworkReachabilityManager sharedManager] isReachable];
}

#pragma mark - Initialization

+ (instancetype)messageWithParameters:(NSDictionary *) parameters
Expand Down Expand Up @@ -228,7 +273,7 @@ - (void)executeRequest {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
#ifdef DEBUG
NSString *response = [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding];
if (response) { NSLog(@"Response error: %@", response); }
if (response && response.length) { NSLog(@"Response error: %@", response); }
#endif
__strong JBMessage *strongThis = this;
[strongThis receivedResponse:operation.responseData error:error];
Expand Down

0 comments on commit 185b8e9

Please sign in to comment.