· 4 years ago · Apr 21, 2021, 05:06 AM
1//
2// Service.swift
3// Movie Finder
4//
5// Created by Edson Neto on 19/04/21.
6//
7
8import Foundation
9
10protocol MovieListResult {
11 func movieListResponse(movies: [Movies])
12}
13
14class Service {
15 let apiURL = "https://www.omdbapi.com/?apikey=b560e805&type=movie"
16 var movies = [Movies]()
17
18 var movieListDelegate: MovieListResult!
19
20 func searchMovie(_ movieName: String){
21 let urlString = "\(apiURL)&s=\(movieName)"
22 performRequest(urlString: urlString)
23 }
24
25 func performRequest(urlString: String){
26 if let url = URL(string: urlString){
27 let session = URLSession(configuration: .default)
28 let task = session.dataTask(with: url) { (data, response, error) in
29
30 guard let data = data, error == nil else {
31 return
32 }
33
34 var result: MovieData?
35 do{
36 result = try JSONDecoder().decode(MovieData.self, from: data)
37 }
38 catch {
39 print(error)
40 }
41 guard let finalResult = result else {
42 return
43 }
44 let newMovies = finalResult.Search
45 self.movies.append(contentsOf: newMovies)
46
47 self.movieListDelegate.movieListResponse(movies: self.movies)
48
49
50 }
51 task.resume()
52 }
53 movies.removeAll()
54 }
55
56}
57Service
58------------
59Movie Data
60
61//
62// MovieData.swift
63// Movie Finder
64//
65// Created by Edson Neto on 19/04/21.
66//
67
68import Foundation
69
70struct MovieData: Codable {
71 var Search: [Movies]
72}
73
74struct Movies: Codable{
75 var Title: String
76 var Year: String
77 var imdbID: String
78 var Poster: String
79}
80
81------------------
82Collection view cell
83//
84// MovieCollectionViewCell.swift
85// Movie Finder
86//
87// Created by Edson Neto on 19/04/21.
88//
89
90import UIKit
91
92class MovieCollectionViewCell: UICollectionViewCell {
93
94 @IBOutlet var moviePosterImageView: UIImageView!
95 @IBOutlet var movieTitleLabel: UILabel!
96
97 override func awakeFromNib() {
98 super.awakeFromNib()
99 // Initialization code
100 }
101
102 func setup(with movie: Movies){
103 DispatchQueue.main.async {
104 let url = URL(string: movie.Poster)
105 if let data = try? Data(contentsOf: url!){
106 self.moviePosterImageView.image = UIImage(data: data)
107 }
108 self.movieTitleLabel.text = movie.Title
109 }
110
111 }
112
113}
114
115---------
116viewcontroller
117
118//
119// ViewController.swift
120// Movie Finder
121//
122// Created by Edson Neto on 19/04/21.
123
124// API key: b560e805
125
126
127import UIKit
128
129class ViewController: UIViewController, UICollectionViewDelegate{
130
131 let service = Service()
132 var moviesResult = [Movies]()
133
134 @IBOutlet var movieListCollectionView: UICollectionView!
135 @IBOutlet var searchBar: UISearchBar!
136
137 override func viewDidLoad() {
138 super.viewDidLoad()
139 // Do any additional setup after loading the view.
140
141 movieListCollectionView.dataSource = self
142 movieListCollectionView.delegate = self
143 service.movieListDelegate = self
144 searchBar.delegate = self
145
146 movieListCollectionView.collectionViewLayout = UICollectionViewFlowLayout()
147 movieListCollectionView.register(UINib(nibName: "MovieCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MovieCollectionViewCell")
148
149
150 }
151
152
153}
154
155extension ViewController: MovieListResult{
156 func movieListResponse(movies: [Movies]) {
157 moviesResult = movies
158 }
159}
160
161extension ViewController: UISearchBarDelegate{
162 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
163 if let searchText = searchBar.text{
164 let formattedText = searchText.replacingOccurrences(of: " ", with: "%20").lowercased()
165 service.searchMovie(formattedText)
166 }
167 searchBar.text = ""
168 searchBar.resignFirstResponder()
169 DispatchQueue.main.async {
170 self.movieListCollectionView.reloadData()
171 }
172 }
173}
174
175extension ViewController: UICollectionViewDataSource{
176
177 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
178
179 return moviesResult.count
180 }
181
182 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
183 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MovieCollectionViewCell", for: indexPath) as! MovieCollectionViewCell
184 cell.setup(with: moviesResult[indexPath.row])
185 return cell
186 }
187
188 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
189 print(moviesResult[indexPath.row].imdbID)
190 }
191
192}
193
194extension ViewController: UICollectionViewDelegateFlowLayout{
195 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
196 return CGSize(width: 200, height: 300)
197 }
198}
199
200-----
201
202