Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
25
import Foundation
26
 
27
extension Request {
28
    /// Posted when a `Request` is resumed. The `Notification` contains the resumed `Request`.
29
    public static let didResumeNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didResume")
30
    /// Posted when a `Request` is suspended. The `Notification` contains the suspended `Request`.
31
    public static let didSuspendNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didSuspend")
32
    /// Posted when a `Request` is cancelled. The `Notification` contains the cancelled `Request`.
33
    public static let didCancelNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCancel")
34
    /// Posted when a `Request` is finished. The `Notification` contains the completed `Request`.
35
    public static let didFinishNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didFinish")
36
 
37
    /// Posted when a `URLSessionTask` is resumed. The `Notification` contains the `Request` associated with the `URLSessionTask`.
38
    public static let didResumeTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didResumeTask")
39
    /// Posted when a `URLSessionTask` is suspended. The `Notification` contains the `Request` associated with the `URLSessionTask`.
40
    public static let didSuspendTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didSuspendTask")
41
    /// Posted when a `URLSessionTask` is cancelled. The `Notification` contains the `Request` associated with the `URLSessionTask`.
42
    public static let didCancelTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCancelTask")
43
    /// Posted when a `URLSessionTask` is completed. The `Notification` contains the `Request` associated with the `URLSessionTask`.
44
    public static let didCompleteTaskNotification = Notification.Name(rawValue: "org.alamofire.notification.name.request.didCompleteTask")
45
}
46
 
47
// MARK: -
48
 
49
extension Notification {
50
    /// The `Request` contained by the instance's `userInfo`, `nil` otherwise.
51
    public var request: Request? {
52
        userInfo?[String.requestKey] as? Request
53
    }
54
 
55
    /// Convenience initializer for a `Notification` containing a `Request` payload.
56
    ///
57
    /// - Parameters:
58
    ///   - name:    The name of the notification.
59
    ///   - request: The `Request` payload.
60
    init(name: Notification.Name, request: Request) {
61
        self.init(name: name, object: nil, userInfo: [String.requestKey: request])
62
    }
63
}
64
 
65
extension NotificationCenter {
66
    /// Convenience function for posting notifications with `Request` payloads.
67
    ///
68
    /// - Parameters:
69
    ///   - name:    The name of the notification.
70
    ///   - request: The `Request` payload.
71
    func postNotification(named name: Notification.Name, with request: Request) {
72
        let notification = Notification(name: name, request: request)
73
        post(notification)
74
    }
75
}
76
 
77
extension String {
78
    /// User info dictionary key representing the `Request` associated with the notification.
79
    fileprivate static let requestKey = "org.alamofire.notification.key.request"
80
}
81
 
82
/// `EventMonitor` that provides Alamofire's notifications.
83
public final class AlamofireNotifications: EventMonitor {
84
    public func requestDidResume(_ request: Request) {
85
        NotificationCenter.default.postNotification(named: Request.didResumeNotification, with: request)
86
    }
87
 
88
    public func requestDidSuspend(_ request: Request) {
89
        NotificationCenter.default.postNotification(named: Request.didSuspendNotification, with: request)
90
    }
91
 
92
    public func requestDidCancel(_ request: Request) {
93
        NotificationCenter.default.postNotification(named: Request.didCancelNotification, with: request)
94
    }
95
 
96
    public func requestDidFinish(_ request: Request) {
97
        NotificationCenter.default.postNotification(named: Request.didFinishNotification, with: request)
98
    }
99
 
100
    public func request(_ request: Request, didResumeTask task: URLSessionTask) {
101
        NotificationCenter.default.postNotification(named: Request.didResumeTaskNotification, with: request)
102
    }
103
 
104
    public func request(_ request: Request, didSuspendTask task: URLSessionTask) {
105
        NotificationCenter.default.postNotification(named: Request.didSuspendTaskNotification, with: request)
106
    }
107
 
108
    public func request(_ request: Request, didCancelTask task: URLSessionTask) {
109
        NotificationCenter.default.postNotification(named: Request.didCancelTaskNotification, with: request)
110
    }
111
 
112
    public func request(_ request: Request, didCompleteTask task: URLSessionTask, with error: AFError?) {
113
        NotificationCenter.default.postNotification(named: Request.didCompleteTaskNotification, with: request)
114
    }
115
}