-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpcomingViewController.swift
101 lines (78 loc) · 3.24 KB
/
UpcomingViewController.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
//
// UpcomingViewController.swift
// RepliFlix
//
// Created by Krystal Zhang on 8/23/22.
//
import UIKit
class UpcomingViewController: UIViewController {
private var titles: [Title] = [Title]()
private let upcomingTable: UITableView = {
let table = UITableView()
table.register(TitleTableViewCell.self, forCellReuseIdentifier: TitleTableViewCell.identifier)
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
title = "Upcoming"
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .always
view.addSubview(upcomingTable)
upcomingTable.delegate = self
upcomingTable.dataSource = self
fetchUpcoming()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
upcomingTable.frame = view.bounds
}
private func fetchUpcoming(){
APICaller.shared.getUpcomingMovies { [weak self] result in
switch result {
case .success(let titles):
self?.titles = titles
DispatchQueue.main.async {
self?.upcomingTable.reloadData()
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
extension UpcomingViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TitleTableViewCell.identifier, for: indexPath) as? TitleTableViewCell else {
return UITableViewCell()
}
let title = titles[indexPath.row]
cell.configure(with: TitleViewModel(titleName: (title.original_title ?? title.original_name) ?? "Unknown title name", posterURL: title.poster_path ?? ""))
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 140
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let title = titles[indexPath.row]
guard let titleName = title.original_title ?? title.original_name else {
return
}
APICaller.shared.getMovie(with: titleName) {[weak self ] result in
switch result {
case .success(let videoElement):
DispatchQueue.main.async {
let vc = TitlePreviewViewController()
vc.configure(with: TitlePreviewViewModel(title: titleName, youtubeView: videoElement, titleOverview: title.overview ?? ""))
self?.navigationController?.pushViewController(vc, animated: true)
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
}