-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathGiphyMedia.swift
102 lines (83 loc) · 2.35 KB
/
GiphyMedia.swift
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import WPMediaPicker
import MobileCoreServices
struct GiphyImageCollection {
private(set) var largeURL: URL
private(set) var previewURL: URL
private(set) var staticThumbnailURL: URL
private(set) var largeSize: CGSize
}
/// Models a Giphy image
///
final class GiphyMedia: NSObject {
private(set) var id: String
private(set) var name: String
private(set) var caption: String
private let updatedDate: Date
private let images: GiphyImageCollection
init(id: String, name: String, caption: String, images: GiphyImageCollection, date: Date? = nil) {
self.id = id
self.name = name
self.caption = caption
self.updatedDate = date ?? Date()
self.images = images
}
}
extension GiphyMedia: WPMediaAsset {
func image(with size: CGSize, completionHandler: @escaping WPMediaImageBlock) -> WPMediaRequestID {
let url = imageURL(with: size)
DispatchQueue.global().async {
do {
let data = try Data(contentsOf: url)
let image = UIImage(data: data)
completionHandler(image, nil)
} catch {
completionHandler(nil, error)
}
}
// Giphy API doesn't return a numerical ID value
return 0
}
private func imageURL(with size: CGSize) -> URL {
return size == .zero ? images.previewURL : images.staticThumbnailURL
}
func cancelImageRequest(_ requestID: WPMediaRequestID) {
// Can't be canceled
}
func videoAsset(completionHandler: @escaping WPMediaAssetBlock) -> WPMediaRequestID {
return 0
}
func assetType() -> WPMediaType {
return .image
}
func duration() -> TimeInterval {
return 0
}
func baseAsset() -> Any {
return self
}
func identifier() -> String {
return id
}
func date() -> Date {
return updatedDate
}
func pixelSize() -> CGSize {
return images.largeSize
}
func utTypeIdentifier() -> String? {
return String(kUTTypeGIF)
}
}
// MARK: - ExportableAsset conformance
extension GiphyMedia: ExportableAsset {
var assetMediaType: MediaType {
return .image
}
}
//// MARK: - MediaExternalAsset conformance
//
extension GiphyMedia: MediaExternalAsset {
var URL: URL {
return images.previewURL
}
}