-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoviesViewController.swift
203 lines (150 loc) · 7.6 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// MoviesViewController.swift
// Flicks
//
// Created by samman on 2/5/17.
// Copyright © 2017 samman. All rights reserved.
//
import UIKit
import AFNetworking
import MBProgressHUD
class MoviesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var movies : [NSDictionary]? = []
var endpoint: String!
// for search bar
var filteredData: [NSDictionary]!
override func viewDidLoad() {
super.viewDidLoad()
// For pulling to refresh
// Initialize a UIRefreshControl
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshControlAction(_:)), for: UIControlEvents.valueChanged)
// add refresh control to table view
tableView.insertSubview(refreshControl, at: 0)
tableView.dataSource = self
searchBar.delegate = self
tableView.delegate = self
tableView.backgroundColor = UIColor .red
// Do any additional setup after loading the view.
let apiKey = "a07e22bc18f5cb106bfe4cc1f83ad8ed"
let ep = self.endpoint!
print("https://api.themoviedb.org/3/movie/\(ep)?api_key=\(apiKey)")
let url = URL(string: "https://api.themoviedb.org/3/movie/\(ep)?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
if let data = data {
if let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
MBProgressHUD.hide(for: self.view, animated: true)
print(dataDictionary)
self.movies = dataDictionary["results"] as! [NSDictionary]
self.filteredData = self.movies
self.tableView.reloadData()
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let movies = self.filteredData {
return movies.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! CustomMovieCell
let movie = filteredData![indexPath.row]
let title = movie["title"] as! String?
let overview = movie["overview"] as! String?
let poster_base_url = "https://image.tmdb.org/t/p/w342"
if let poster_path = movie["poster_path"] as! String? {
let poster_url = NSURL(string: poster_base_url + poster_path)
cell.posterView.setImageWith(poster_url as! URL)
}
cell.backgroundColor = UIColor .orange
cell.titleLabel.text = title
cell.overviewLabel.text = overview
// for fitting in the content
cell.titleLabel.sizeToFit()
cell.selectionStyle = .none
print("row \(indexPath.row)")
return cell
}
// Makes a network request to get updated data
// Updates the tableView with the new data
// Hides the RefreshControl
func refreshControlAction(_ refreshControl: UIRefreshControl) {
// Do any additional setup after loading the view.
let apiKey = "a07e22bc18f5cb106bfe4cc1f83ad8ed"
let url = URL(string: "https://api.themoviedb.org/3/movie/now_playing?api_key=\(apiKey)")!
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10)
// Configure session so that completion handler is executed on main UI thread
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
if let data = data {
if let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
MBProgressHUD.hide(for: self.view, animated: true)
print(dataDictionary)
self.movies = dataDictionary["results"] as! [NSDictionary]
self.filteredData = self.movies
self.tableView.reloadData()
// Tell the refreshControl to stop spinning
refreshControl.endRefreshing()
}
}
}
task.resume()
}
// Search Bar
// This method updates filteredData based on the text in the Search Box
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// When there is no text, filteredData is the same as the original data
// When user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
self.filteredData = searchText.isEmpty ? movies : movies?.filter({(dataDict: NSDictionary) -> Bool in
// If dataItem matches the searchText, return true to include it
let title = dataDict["title"] as! String
// return true if searchText matches title
return title.range(of: searchText, options: .caseInsensitive) != nil
})
tableView.reloadData()
}
// When search starts
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchBar.showsCancelButton = true
}
// when cancel button clicked
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
self.filteredData = self.movies
searchBar.resignFirstResponder()
self.tableView.reloadData()
}
// 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?) {
let cell = sender as! UITableViewCell
// get the indexpath for the given cell
let indexPath = tableView.indexPath(for: cell)
// get the movie
let movie = self.filteredData![(indexPath!.row)]
// get the detail view controller we segue to
let detailViewControl = segue.destination as! DetailViewController
// add to the dictionary in the custom class
detailViewControl.movie = movie
print("Segue")
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
}