Skip to content

Commit

Permalink
Refactored NotificationNames and moved status changes into userInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnoon committed Feb 7, 2016
1 parent 88fe58c commit 634d0b2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 56 deletions.
20 changes: 10 additions & 10 deletions Alamofire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
4C83F41D1B749E0E00203445 /* Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C83F41A1B749E0E00203445 /* Stream.swift */; };
4CA028C51B7466C500C84163 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA028C41B7466C500C84163 /* ResultTests.swift */; };
4CA028C61B7466C500C84163 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA028C41B7466C500C84163 /* ResultTests.swift */; };
4CB928291C66BFBC00CE5F08 /* NotificationNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* NotificationNames.swift */; };
4CB9282A1C66BFBC00CE5F08 /* NotificationNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* NotificationNames.swift */; };
4CB9282B1C66BFBC00CE5F08 /* NotificationNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* NotificationNames.swift */; };
4CB9282C1C66BFBC00CE5F08 /* NotificationNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* NotificationNames.swift */; };
4CB928291C66BFBC00CE5F08 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* Notifications.swift */; };
4CB9282A1C66BFBC00CE5F08 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* Notifications.swift */; };
4CB9282B1C66BFBC00CE5F08 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* Notifications.swift */; };
4CB9282C1C66BFBC00CE5F08 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB928281C66BFBC00CE5F08 /* Notifications.swift */; };
4CCFA79A1B2BE71600B6F460 /* URLProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CCFA7991B2BE71600B6F460 /* URLProtocolTests.swift */; };
4CCFA79B1B2BE71600B6F460 /* URLProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CCFA7991B2BE71600B6F460 /* URLProtocolTests.swift */; };
4CDE2C371AF8932A00BABAE5 /* Manager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C361AF8932A00BABAE5 /* Manager.swift */; };
Expand Down Expand Up @@ -272,7 +272,7 @@
4C812C601B535F6D0017E0BF /* testssl-expire.disig.sk.cer */ = {isa = PBXFileReference; lastKnownFileType = file; name = "testssl-expire.disig.sk.cer"; path = "disig.sk/testssl-expire.disig.sk.cer"; sourceTree = "<group>"; };
4C83F41A1B749E0E00203445 /* Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stream.swift; sourceTree = "<group>"; };
4CA028C41B7466C500C84163 /* ResultTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = "<group>"; };
4CB928281C66BFBC00CE5F08 /* NotificationNames.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationNames.swift; sourceTree = "<group>"; };
4CB928281C66BFBC00CE5F08 /* Notifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
4CCFA7991B2BE71600B6F460 /* URLProtocolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLProtocolTests.swift; sourceTree = "<group>"; };
4CDE2C361AF8932A00BABAE5 /* Manager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Manager.swift; sourceTree = "<group>"; };
4CDE2C391AF899EC00BABAE5 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -510,7 +510,7 @@
children = (
4C1DC8531B68908E00476DE3 /* Error.swift */,
4CDE2C361AF8932A00BABAE5 /* Manager.swift */,
4CB928281C66BFBC00CE5F08 /* NotificationNames.swift */,
4CB928281C66BFBC00CE5F08 /* Notifications.swift */,
4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */,
4CDE2C391AF899EC00BABAE5 /* Request.swift */,
4C0B62501BB1001C009302D3 /* Response.swift */,
Expand Down Expand Up @@ -969,7 +969,7 @@
4CF627131BA7CBF60011A099 /* Validation.swift in Sources */,
4CF6270E1BA7CBF60011A099 /* MultipartFormData.swift in Sources */,
4C80F9F81BB730EF001B46D2 /* Response.swift in Sources */,
4CB9282B1C66BFBC00CE5F08 /* NotificationNames.swift in Sources */,
4CB9282B1C66BFBC00CE5F08 /* Notifications.swift in Sources */,
4CF627091BA7CBF60011A099 /* Manager.swift in Sources */,
4CF6270F1BA7CBF60011A099 /* ResponseSerialization.swift in Sources */,
4CF6270B1BA7CBF60011A099 /* Request.swift in Sources */,
Expand Down Expand Up @@ -1019,7 +1019,7 @@
4C1DC8551B68908E00476DE3 /* Error.swift in Sources */,
4CDE2C381AF8932A00BABAE5 /* Manager.swift in Sources */,
4C0B62521BB1001C009302D3 /* Response.swift in Sources */,
4CB9282A1C66BFBC00CE5F08 /* NotificationNames.swift in Sources */,
4CB9282A1C66BFBC00CE5F08 /* Notifications.swift in Sources */,
4DD67C251A5C590000ED2280 /* Alamofire.swift in Sources */,
4C23EB441B327C5B0090E0BC /* MultipartFormData.swift in Sources */,
4C811F8E1B51856D00E0F59A /* ServerTrustPolicy.swift in Sources */,
Expand All @@ -1043,7 +1043,7 @@
E4202FD21B667AA100C997FB /* ResponseSerialization.swift in Sources */,
E4202FD31B667AA100C997FB /* Manager.swift in Sources */,
4C0B62531BB1001C009302D3 /* Response.swift in Sources */,
4CB9282C1C66BFBC00CE5F08 /* NotificationNames.swift in Sources */,
4CB9282C1C66BFBC00CE5F08 /* Notifications.swift in Sources */,
4CEC605B1B745C9100E684F4 /* Result.swift in Sources */,
E4202FD41B667AA100C997FB /* Alamofire.swift in Sources */,
E4202FD51B667AA100C997FB /* MultipartFormData.swift in Sources */,
Expand Down Expand Up @@ -1073,7 +1073,7 @@
4C83F41B1B749E0E00203445 /* Stream.swift in Sources */,
4CDE2C3D1AF89D4900BABAE5 /* Download.swift in Sources */,
4CDE2C431AF89F0900BABAE5 /* Validation.swift in Sources */,
4CB928291C66BFBC00CE5F08 /* NotificationNames.swift in Sources */,
4CB928291C66BFBC00CE5F08 /* Notifications.swift in Sources */,
4C0E5BF81B673D3400816CCC /* Result.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion Source/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public class Manager {
delegate.URLSession(session, task: task, didCompleteWithError: error)
}

NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.TaskDidComplete, object: task)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.Task.DidComplete, object: task)

self[task] = nil
}
Expand Down
13 changes: 7 additions & 6 deletions Source/NetworkReachabilityManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
import Foundation
import SystemConfiguration

/// Notification posted when network reachability status changes. The notification `object` contains the update
/// network reachability status as an `NSNumber` which will need to be converted.
public let NetworkReachabilityStatusDidChangeNotification = "com.alamofire.network.reachability.status.did.change"

/**
Defines the various states of network reachability.

Expand Down Expand Up @@ -179,9 +175,14 @@ public class NetworkReachabilityManager {
listener?(networkReachabilityStatus)

dispatch_async(dispatch_get_main_queue()) {
let userInfo: [NSObject: AnyObject] = [
Notifications.NetworkReachability.StatusDidChangeUserInfoStatusKey: networkReachabilityStatus.rawValue
]

NSNotificationCenter.defaultCenter().postNotificationName(
NetworkReachabilityStatusDidChangeNotification,
object: networkReachabilityStatus.rawValue
Notifications.NetworkReachability.StatusDidChange,
object: self,
userInfo: userInfo
)
}
}
Expand Down
30 changes: 0 additions & 30 deletions Source/NotificationNames.swift

This file was deleted.

58 changes: 58 additions & 0 deletions Source/Notifications.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Notifications.swift
//
// Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

/// Contains all the `NSNotification` names posted by Alamofire with descriptions of each notification's payload.
public struct Notifications {
/// Used as a namespace for all `NSURLSessionTask` related notifications.
public struct Task {
/// Notification posted when an `NSURLSessionTask` is resumed. The notification `object` contains the resumed
/// `NSURLSessionTask`.
public static let DidResume = "com.alamofire.notifications.task.didResume"

/// Notification posted when an `NSURLSessionTask` is suspended. The notification `object` contains the
/// suspended `NSURLSessionTask`.
public static let DidSuspend = "com.alamofire.notifications.task.didSuspend"

/// Notification posted when an `NSURLSessionTask` is cancelled. The notification `object` contains the
/// cancelled `NSURLSessionTask`.
public static let DidCancel = "com.alamofire.notifications.task.didCancel"

/// Notification posted when an `NSURLSessionTask` is completed. The notification `object` contains the
/// completed `NSURLSessionTask`.
public static let DidComplete = "com.alamofire.notifications.task.didComplete"
}

/// Used as a namespace for all `NetworkReachability` related notifications.
public struct NetworkReachability {
/// Notification posted when network reachability status changes. The notification `object` contains the
/// `NetworkReachabilityManager` reporting the status change. The `userInfo` dictionary contains the
/// `StatusDidChangeUserInfoStatusKey` whose value is the network reachability status as an `NSNumber` which
/// will need to be converted.
public static let StatusDidChange = "com.alamofire.notifications.networkReachability.statusDidChange"

/// The network reachability status user info key for the `StatusDidChange` notification containing the
/// `NetworkReachabilityStatus` rawValue as an `NSNumber`.
public static let StatusDidChangeUserInfoStatusKey = "NetworkReachabilityStatus"
}
}
6 changes: 3 additions & 3 deletions Source/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ public class Request {
if startTime == nil { startTime = CFAbsoluteTimeGetCurrent() }

task.resume()
NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.TaskDidResume, object: task)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.Task.DidResume, object: task)
}

/**
Suspends the request.
*/
public func suspend() {
task.suspend()
NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.TaskDidSuspend, object: task)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.Task.DidSuspend, object: task)
}

/**
Expand All @@ -186,7 +186,7 @@ public class Request {
task.cancel()
}

NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.TaskDidCancel, object: task)
NSNotificationCenter.defaultCenter().postNotificationName(Notifications.Task.DidCancel, object: task)
}

// MARK: - TaskDelegate
Expand Down
20 changes: 14 additions & 6 deletions Tests/NetworkReachabilityManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ class NetworkReachabilityManagerTestCase: BaseTestCase {

var networkReachabilityStatus: NetworkReachabilityStatus?

expectationForNotification(NetworkReachabilityStatusDidChangeNotification, object: nil) { notification in
expectationForNotification(Notifications.NetworkReachability.StatusDidChange, object: nil) { notification in
if let
value = (notification.object as? NSNumber)?.integerValue,
status = NetworkReachabilityStatus(rawValue: value)
object = notification.object,
userInfo = notification.userInfo,
number = userInfo[Notifications.NetworkReachability.StatusDidChangeUserInfoStatusKey],
statusValue = (number as? NSNumber)?.integerValue,
status = NetworkReachabilityStatus(rawValue: statusValue)
where manager === object
{
networkReachabilityStatus = status
return true
Expand All @@ -143,10 +147,14 @@ class NetworkReachabilityManagerTestCase: BaseTestCase {

var networkReachabilityStatus: NetworkReachabilityStatus?

expectationForNotification(NetworkReachabilityStatusDidChangeNotification, object: nil) { notification in
expectationForNotification(Notifications.NetworkReachability.StatusDidChange, object: nil) { notification in
if let
value = (notification.object as? NSNumber)?.integerValue,
status = NetworkReachabilityStatus(rawValue: value)
object = notification.object,
userInfo = notification.userInfo,
number = userInfo[Notifications.NetworkReachability.StatusDidChangeUserInfoStatusKey],
statusValue = (number as? NSNumber)?.integerValue,
status = NetworkReachabilityStatus(rawValue: statusValue)
where manager === object
{
networkReachabilityStatus = status
return true
Expand Down

0 comments on commit 634d0b2

Please sign in to comment.