forked from unsplash/unsplash-photopicker-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnsplashPhoto.swift
91 lines (80 loc) · 3.1 KB
/
UnsplashPhoto.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
//
// UnsplashPhoto.swift
// Submissions
//
// Created by Olivier Collet on 2017-04-10.
// Copyright © 2017 Unsplash. All rights reserved.
//
import UIKit
/// A struct representing a photo from the Unsplash API.
public struct UnsplashPhoto: Codable {
public enum URLKind: String, Codable {
case raw
case full
case regular
case small
case thumb
}
public enum LinkKind: String, Codable {
case own = "self"
case html
case download
case downloadLocation = "download_location"
}
public let identifier: String
public let height: Int
public let width: Int
public let color: UIColor?
public let exif: UnsplashPhotoExif?
public let user: UnsplashUser
public let urls: [URLKind: URL]
public let links: [LinkKind: URL]
public let likesCount: Int
public let downloadsCount: Int?
public let viewsCount: Int?
private enum CodingKeys: String, CodingKey {
case identifier = "id"
case height
case width
case color
case exif
case user
case urls
case links
case likesCount = "likes"
case downloadsCount = "downloads"
case viewsCount = "views"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
identifier = try container.decode(String.self, forKey: .identifier)
height = try container.decode(Int.self, forKey: .height)
width = try container.decode(Int.self, forKey: .width)
if let hexString = try? container.decode(String.self, forKey: .color) {
color = UIColor(hexString: hexString)
} else {
color = nil
}
exif = try? container.decode(UnsplashPhotoExif.self, forKey: .exif)
user = try container.decode(UnsplashUser.self, forKey: .user)
urls = try container.decode([URLKind: URL].self, forKey: .urls)
links = try container.decode([LinkKind: URL].self, forKey: .links)
likesCount = try container.decode(Int.self, forKey: .likesCount)
downloadsCount = try? container.decode(Int.self, forKey: .downloadsCount)
viewsCount = try? container.decode(Int.self, forKey: .viewsCount)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(identifier, forKey: .identifier)
try container.encode(height, forKey: .height)
try container.encode(width, forKey: .width)
try? container.encode(color?.hexString, forKey: .color)
try? container.encode(exif, forKey: .exif)
try container.encode(user, forKey: .user)
try container.encode(urls.convert({ ($0.key.rawValue, $0.value.absoluteString) }), forKey: .urls)
try container.encode(links.convert({ ($0.key.rawValue, $0.value.absoluteString) }), forKey: .links)
try container.encode(likesCount, forKey: .likesCount)
try? container.encode(downloadsCount, forKey: .downloadsCount)
try? container.encode(viewsCount, forKey: .viewsCount)
}
}