Browse Source

Refactored Notifications to use nested structs inside Notification.Name namespace.

Christian Noon 9 years ago
parent
commit
5c2f4365be
3 changed files with 42 additions and 20 deletions
  1. 5 1
      Source/Manager.swift
  2. 20 15
      Source/Notifications.swift
  3. 17 4
      Source/Request.swift

+ 5 - 1
Source/Manager.swift

@@ -509,7 +509,11 @@ public class Manager {
                 delegate.urlSession(session, task: task, didCompleteWithError: error)
             }
 
-            NotificationCenter.default.post(name: Notification.Name(rawValue: Notifications.Task.DidComplete), object: task)
+            NotificationCenter.default.post(
+                name: Notification.Name.Task.DidComplete,
+                object: self,
+                userInfo: [Notification.Key.Task: task]
+            )
 
             self[task] = nil
         }

+ 20 - 15
Source/Notifications.swift

@@ -24,24 +24,29 @@
 
 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.
+extension Notification.Name {
+    /// Used as a namespace for all `URLSessionTask` 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"
+        /// Posted when a `URLSessionTask` is resumed. The notification `object` contains the resumed `URLSessionTask`.
+        public static let DidResume = Notification.Name(rawValue: "com.alamofire.notification.name.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"
+        /// Posted when a `URLSessionTask` is suspended. The notification `object` contains the suspended `URLSessionTask`.
+        public static let DidSuspend = Notification.Name(rawValue: "com.alamofire.notification.name.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"
+        /// Posted when a `URLSessionTask` is cancelled. The notification `object` contains the cancelled `URLSessionTask`.
+        public static let DidCancel = Notification.Name(rawValue: "com.alamofire.notification.name.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"
+        /// Posted when a `URLSessionTask` is completed. The notification `object` contains the completed `URLSessionTask`.
+        public static let DidComplete = Notification.Name(rawValue: "com.alamofire.notification.name.task.didComplete")
+    }
+}
+
+// MARK: -
+
+extension Notification {
+    /// Used as a namespace for all `Notification` user info dictionary keys.
+    public struct Key {
+        /// User info dictionary key representing the `URLSessionTask` associated with the notification.
+        public static let Task = "com.alamofire.notification.key.task"
     }
 }

+ 17 - 4
Source/Request.swift

@@ -180,7 +180,12 @@ public class Request {
         if startTime == nil { startTime = CFAbsoluteTimeGetCurrent() }
 
         task.resume()
-        NotificationCenter.default.post(name: Notification.Name(rawValue: Notifications.Task.DidResume), object: task)
+
+        NotificationCenter.default.post(
+            name: Notification.Name.Task.DidResume,
+            object: self,
+            userInfo: [Notification.Key.Task: task]
+        )
     }
 
     /**
@@ -188,7 +193,12 @@ public class Request {
     */
     public func suspend() {
         task.suspend()
-        NotificationCenter.default.post(name: Notification.Name(rawValue: Notifications.Task.DidSuspend), object: task)
+
+        NotificationCenter.default.post(
+            name: Notification.Name.Task.DidSuspend,
+            object: self,
+            userInfo: [Notification.Key.Task: task]
+        )
     }
 
     /**
@@ -205,7 +215,11 @@ public class Request {
             task.cancel()
         }
 
-        NotificationCenter.default.post(name: Notification.Name(rawValue: Notifications.Task.DidCancel), object: task)
+        NotificationCenter.default.post(
+            name: Notification.Name.Task.DidCancel,
+            object: self,
+            userInfo: [Notification.Key.Task: task]
+        )
     }
 
     // MARK: - TaskDelegate
@@ -215,7 +229,6 @@ public class Request {
         executing all operations attached to the serial operation queue upon task completion.
     */
     public class TaskDelegate: NSObject {
-
         /// The serial operation queue used to execute all operations after the task completes.
         public let queue: OperationQueue