Skip to content

Instantly share code, notes, and snippets.

@enebin
Created April 17, 2022 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enebin/05db8e60ad393cc86aba1af54366297e to your computer and use it in GitHub Desktop.
Save enebin/05db8e60ad393cc86aba1af54366297e to your computer and use it in GitHub Desktop.
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