-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRichPushRequest.swift
More file actions
51 lines (43 loc) · 1.54 KB
/
RichPushRequest.swift
File metadata and controls
51 lines (43 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import CioInternalCommon
import Foundation
/// Request to download and attach a rich push image. Only created by the handler when the push
/// has a valid image URL, so this request always uses the shared HTTP client and cancel() always cancels it.
class RichPushRequest {
private let completionHandler: (PushNotification) -> Void
private var push: PushNotification
private let imageURL: URL
private let httpClient: HttpClient
private var isCompleted = false
init(
push: PushNotification,
imageURL: URL,
httpClient: HttpClient,
completionHandler: @escaping (PushNotification) -> Void
) {
self.completionHandler = completionHandler
self.push = push
self.imageURL = imageURL
self.httpClient = httpClient
}
func start() {
httpClient.downloadFile(url: imageURL, fileType: .richPushImage) { [weak self] localFilePath in
guard let self = self else { return }
if let localFilePath = localFilePath {
self.push.cioRichPushImageFile = localFilePath
}
self.completeOnce()
}
}
/// Call when the request must be aborted (e.g. NSE expiry). Cancels the download and completes.
func cancel() {
guard !isCompleted else { return }
isCompleted = true
httpClient.cancel(finishTasks: false)
completionHandler(push)
}
private func completeOnce() {
guard !isCompleted else { return }
isCompleted = true
completionHandler(push)
}
}