-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
129 lines (93 loc) · 4.54 KB
/
ViewController.swift
File metadata and controls
129 lines (93 loc) · 4.54 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// ViewController.swift
// Flicks
//
// Created by Joey Singer on 2/6/17.
// Copyright © 2017 Joey Singer. All rights reserved.
//
import UIKit
import AFNetworking
import MBProgressHUD
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var movies: [NSDictionary]?
var endpoint: String!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let apiKey = "a07e22bc18f5cb106bfe4cc1f83ad8ed"
let url = URL(string: "https://api.themoviedb.org/3/movie/\(self.endpoint!)?api_key=\(apiKey)")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
MBProgressHUD.showAdded(to: self.view, animated: true)
let task: URLSessionDataTask = session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
MBProgressHUD.hide(for: self.view, animated: true)
if let data = data {
if let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
print(dataDictionary)
self.movies = dataDictionary["results"] as? [NSDictionary]
self.tableView.reloadData()
}
}
}
task.resume()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshControlAction(_:)), for: .valueChanged)
tableView.insertSubview(refreshControl, at: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let movies = movies {
return movies.count
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! MovieCell
let movie = movies![indexPath.row]
let title = movie["title"] as! String
let overview = movie["overview"] as! String
cell.titleLabel.text = title
cell.overviewLabel.text = overview
if let poster_path = movie["poster_path"] as? String {
let base_url = "https://image.tmdb.org/t/p/w500/"
let posterURL = URL(string: base_url + poster_path)!
cell.posterView.setImageWith(posterURL)
} else {
// No poster image. Can either set to nil (no image) or a default movie poster image
// that you include as an asset
cell.posterView.image = nil
}
//print("Row: \(indexPath.row)")
return cell
}
func refreshControlAction(_ refreshControl: UIRefreshControl) {
// ... Create the URLRequest `myRequest` ...
// Configure session so that completion handler is executed on main UI thread
let apiKey = "a07e22bc18f5cb106bfe4cc1f83ad8ed"
let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)")!
let session = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
let task: URLSessionDataTask = session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
// ... Use the new data to update the data source ...
// Reload the tableView now that there is new data
self.tableView.reloadData()
// Tell the refreshControl to stop spinning
refreshControl.endRefreshing()
}
task.resume()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPath(for: cell)
let movie = movies![indexPath!.row]
let detailViewController = segue.destination as! DetailViewController
detailViewController.movie = movie
}
}