-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoviesViewController.swift
98 lines (70 loc) · 3.58 KB
/
MoviesViewController.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
//
// MoviesViewController.swift
// Movie App
//
// Created by Erin DeLong on 4/3/21.
//
import UIKit
import AlamofireImage
class MoviesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var movies = [[String:Any]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view. called when screen comes up
// downloads array of movies and stores it in movies variable
let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=a07e22bc18f5cb106bfe4cc1f83ad8ed")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: request) { (data, response, error) in
// This will run when the network request returns
if let error = error {
print(error.localizedDescription)
} else if let data = data {
let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
self.movies = dataDictionary["results"] as! [[String: Any]]
self.tableView.reloadData()
print(dataDictionary)
// TODO: Get the array of movies
// TODO: Store the movies in a property to use elsewhere
// TODO: Reload your table view data
}
}
task.resume()
}
// called when view controller starts up and movies have not been recied so movies.count is 0
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movies.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! MovieCell
let movie = movies[indexPath.row]
let title = movie["title"] as! String
let synopsis = movie["overview"] as! String
// determines what text shows up in each cell
cell.titleLabel.text = title
cell.synopsisLabel.text = synopsis
let baseUrl = "https://image.tmdb.org/t/p/w185"
let posterPath = movie["poster_path"] as! String
let posterUrl = URL(string: baseUrl + posterPath)!
cell.posterView.af_setImage(withURL: posterUrl)
return cell
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
print("Loading up details screen")
//find selected movie how do we know what was tapped on)
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPath(for: cell)!
let movie = movies[indexPath.row]
//pass selected movie to the details view controller
let detailsViewController = segue.destination as! MovieDetailsViewController
detailsViewController.movie = movie
tableView.deselectRow(at: indexPath, animated: true)
}
}