-
-
Save enebin/05db8e60ad393cc86aba1af54366297e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
struct ImageMetaData: Decodable { | |
let identifier = UUID() | |
let height: Int | |
let width: Int | |
let color: String | |
let user: User | |
let urls: URLs | |
/// It will be used to calculate image's height dynamically | |
var imageRatio: CGFloat { | |
return CGFloat(height) / CGFloat(width) | |
} | |
var author: String { | |
return user.name | |
} | |
var smallSizedImageUrl: String { | |
return urls.small | |
} | |
var thumbnailSizedImageUrl: String { | |
return urls.thumb | |
} | |
struct URLs: Decodable { | |
let small: String | |
let thumb: String | |
} | |
struct User: Decodable { | |
let name: String | |
} | |
private enum CodingKeys: String, CodingKey { | |
case height | |
case width | |
case color | |
case user | |
case urls = "urls" | |
} | |
} | |
/// To use UICollectionViewDiffableDataSource, struct should be hashable | |
extension ImageMetaData: Hashable { | |
func hash(into hasher: inout Hasher) { | |
return hasher.combine(identifier) | |
} | |
static func == (lhs: ImageMetaData, rhs: ImageMetaData) -> Bool { | |
return lhs.identifier == rhs.identifier | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment