Notifications.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public extension Request {
  26. /// Posted when a `Request` is resumed. The `Notification` contains the resumed `Request`.
  27. static let didResume = Notification.Name(rawValue: "org.alamofire.notification.name.request.didResume")
  28. /// Posted when a `Request` is suspended. The `Notification` contains the suspended `Request`.
  29. static let didSuspend = Notification.Name(rawValue: "org.alamofire.notification.name.request.didSuspend")
  30. /// Posted when a `Request` is cancelled. The `Notification` contains the cancelled `Request`.
  31. static let didCancel = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCancel")
  32. /// Posted when a `Request` is finished. The `Notification` contains the completed `Request`.
  33. static let didFinish = Notification.Name(rawValue: "org.alamofire.notification.name.request.didFinish")
  34. }
  35. // MARK: -
  36. extension Notification {
  37. /// The `Request` contained by the instance's `userInfo`, `nil` otherwise.
  38. public var request: Request? {
  39. return userInfo?[String.requestKey] as? Request
  40. }
  41. /// Convenience initializer for a `Notification` containing a `Request` payload.
  42. ///
  43. /// - Parameters:
  44. /// - name: The name of the notification.
  45. /// - request: The `Request` payload.
  46. init(name: Notification.Name, request: Request) {
  47. self.init(name: name, object: nil, userInfo: [String.requestKey: request])
  48. }
  49. }
  50. extension NotificationCenter {
  51. /// Convenience function for posting notifications with `Request` payloads.
  52. ///
  53. /// - Parameters:
  54. /// - name: The name of the notification.
  55. /// - request: The `Request` payload.
  56. func postNotification(named name: Notification.Name, with request: Request) {
  57. let notification = Notification(name: name, request: request)
  58. post(notification)
  59. }
  60. }
  61. extension String {
  62. /// User info dictionary key representing the `Request` associated with the notification.
  63. fileprivate static let requestKey = "org.alamofire.notification.key.request"
  64. }
  65. /// `EventMonitor` that provides Alamofire's notifications.
  66. public final class AlamofireNotifications: EventMonitor {
  67. public func requestDidFinish(_ request: Request) {
  68. NotificationCenter.default.postNotification(named: Request.didFinish, with: request)
  69. }
  70. public func requestDidResume(_ request: Request) {
  71. NotificationCenter.default.postNotification(named: Request.didResume, with: request)
  72. }
  73. public func requestDidSuspend(_ request: Request) {
  74. NotificationCenter.default.postNotification(named: Request.didSuspend, with: request)
  75. }
  76. public func requestDidCancel(_ request: Request) {
  77. NotificationCenter.default.postNotification(named: Request.didCancel, with: request)
  78. }
  79. }