Notifications.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Notifications.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. extension Request {
  26. /// Posted when a `Request` is resumed. The `Notification` contains the resumed `Request`.
  27. public static let didResumeNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didResume")
  28. /// Posted when a `Request` is suspended. The `Notification` contains the suspended `Request`.
  29. public static let didSuspendNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didSuspend")
  30. /// Posted when a `Request` is cancelled. The `Notification` contains the cancelled `Request`.
  31. public static let didCancelNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCancel")
  32. /// Posted when a `Request` is finished. The `Notification` contains the completed `Request`.
  33. public static let didFinishNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didFinish")
  34. /// Posted when a `URLSessionTask` is resumed. The `Notification` contains the `Request` associated with the `URLSessionTask`.
  35. public static let didResumeTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didResumeTask")
  36. /// Posted when a `URLSessionTask` is suspended. The `Notification` contains the `Request` associated with the `URLSessionTask`.
  37. public static let didSuspendTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didSuspendTask")
  38. /// Posted when a `URLSessionTask` is cancelled. The `Notification` contains the `Request` associated with the `URLSessionTask`.
  39. public static let didCancelTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCancelTask")
  40. /// Posted when a `URLSessionTask` is completed. The `Notification` contains the `Request` associated with the `URLSessionTask`.
  41. public static let didCompleteTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCompleteTask")
  42. }
  43. // MARK: -
  44. extension Notification {
  45. /// The `Request` contained by the instance's `userInfo`, `nil` otherwise.
  46. public var request: Request? {
  47. userInfo?[String.requestKey] as? Request
  48. }
  49. /// Convenience initializer for a `Notification` containing a `Request` payload.
  50. ///
  51. /// - Parameters:
  52. /// - name: The name of the notification.
  53. /// - request: The `Request` payload.
  54. init(name: Notification.Name, request: Request) {
  55. self.init(name: name, object: nil, userInfo: [String.requestKey: request])
  56. }
  57. }
  58. extension NotificationCenter {
  59. /// Convenience function for posting notifications with `Request` payloads.
  60. ///
  61. /// - Parameters:
  62. /// - name: The name of the notification.
  63. /// - request: The `Request` payload.
  64. func postNotification(named name: Notification.Name, with request: Request) {
  65. let notification = Notification(name: name, request: request)
  66. post(notification)
  67. }
  68. }
  69. extension String {
  70. /// User info dictionary key representing the `Request` associated with the notification.
  71. fileprivate static let requestKey = "org.alamofire.notification.key.request"
  72. }
  73. /// `EventMonitor` that provides Alamofire's notifications.
  74. public final class AlamofireNotifications: EventMonitor {
  75. public func requestDidResume(_ request: Request) {
  76. NotificationCenter.default.postNotification(named: Request.didResumeNotification, with: request)
  77. }
  78. public func requestDidSuspend(_ request: Request) {
  79. NotificationCenter.default.postNotification(named: Request.didSuspendNotification, with: request)
  80. }
  81. public func requestDidCancel(_ request: Request) {
  82. NotificationCenter.default.postNotification(named: Request.didCancelNotification, with: request)
  83. }
  84. public func requestDidFinish(_ request: Request) {
  85. NotificationCenter.default.postNotification(named: Request.didFinishNotification, with: request)
  86. }
  87. public func request(_ request: Request, didResumeTask task: URLSessionTask) {
  88. NotificationCenter.default.postNotification(named: Request.didResumeTaskNotification, with: request)
  89. }
  90. public func request(_ request: Request, didSuspendTask task: URLSessionTask) {
  91. NotificationCenter.default.postNotification(named: Request.didSuspendTaskNotification, with: request)
  92. }
  93. public func request(_ request: Request, didCancelTask task: URLSessionTask) {
  94. NotificationCenter.default.postNotification(named: Request.didCancelTaskNotification, with: request)
  95. }
  96. public func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: AFError?) {
  97. NotificationCenter.default.postNotification(named: Request.didCompleteTaskNotification, with: request)
  98. }
  99. }