/
Movie.swift
56 lines (48 loc) · 1.79 KB
/
Movie.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
//
// Movie.swift
// SwiftUISearchExample
//
// Created by James Rochabrun on 7/4/20.
//
import Foundation
struct Movie: Decodable {
let id: Int
let title: String
var overview: String?
var posterPath: String?
var backdropPath: String?
var releaseDate: String?
enum CodingKeys: String, CodingKey {
case posterPath = "poster_path"
case backdropPath = "backdrop_path"
case releaseDate = "release_date"
case id
case title
case overview
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
posterPath = try container.decodeIfPresent(String.self, forKey: .posterPath) ?? ""
backdropPath = try container.decodeIfPresent(String.self, forKey: .backdropPath) ?? ""
releaseDate = try container.decodeIfPresent(String.self, forKey: .releaseDate) ?? ""
id = try container.decodeIfPresent(Int.self, forKey: .id) ?? 0
title = try container.decodeIfPresent(String.self, forKey: .title) ?? ""
overview = try container.decodeIfPresent(String.self, forKey: .overview) ?? ""
}
}
struct MovieViewModel: Identifiable {
let id: Int
let title: String
let overview: String
let releaseDate: String
let posterPathHighResURLString: String
let posterPathLowResURLString: String
init(movie: Movie) {
self.id = movie.id
self.title = movie.title
self.overview = movie.overview ?? "Not available overview"
self.releaseDate = movie.releaseDate ?? "Not available date"
self.posterPathHighResURLString = "https://image.tmdb.org/t/p/w500" + (movie.posterPath ?? "")
self.posterPathLowResURLString = "https://image.tmdb.org/t/p/w200" + (movie.posterPath ?? "")
}
}