I'm using UIKit, Swift, just that is what I can use. And I'm making this: but I do not know if it is ok because I do not know how to implement it correctly my network is this :
import UIKit
import Foundation
class NetworkManager {
// MARK: - ENUM NETWORKCONSTANTS
enum NetworkConstants {
static let urlString = "https://api.themoviedb.org/3/movie/popular?api_key=669056d94db324f563f195952deaede4&language=en-US&page=1"
static let imageUrlFirstPart = "https://image.tmdb.org/t/p/w500"
}
// MARK: - GETMOVIEDATA - URL AND PARSE FUNC
func getMovieData(completion: @escaping ([MovieData]) -> Void) {
guard let url = URL(string: NetworkConstants.urlString) else {
completion([])
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: url) { (data, response, error) in
guard error == nil, let dataResponse = response as? HTTPURLResponse, dataResponse.statusCode == 200, let data = data else {
completion([])
return
}
do {
let saveDataKey = "id"
var moviesData = [ResultsMovieModel]()
let decoder = JSONDecoder()
let movie = try decoder.decode(ResultsMovieModel.self, from: data)
let defaults = UserDefaults.standard
if let readData = defaults.data(forKey: saveDataKey) {
do {
moviesData = try decoder.decode([ResultsMovieModel].self, from: readData)
} catch {
print(error)
}
}
moviesData.append(movie)
let saveData = try JSONEncoder().encode(moviesData)
defaults.setValue(saveData, forKey: saveDataKey)
completion(movie.results)
} catch {
print(error)
}
}
dataTask.resume()
// let dataTask = session.dataTask(with: url) { (data, response, error) in
// guard error == nil, let dataResponse = response as? HTTPURLResponse,
// dataResponse.statusCode == 200, let data = data else {
// completion([])
// return
// }
// guard let parsedData = try? decoder.decode(ResultsMovieModel.self, from: data) else {
// completion([])
// return
// }
// if let saveDataUD = UserDefaults.standard.data(forKey: saveDataKey) {
// do {
// moviesData = try decoder.decode([ResultsMovieModel].self, from: saveDataUD)
// } catch {
// print(error)
// }
// }
// moviesData.append(parsedData)
// let saveData = try JSONEncoder().encode(moviesData)
// completion(parsedData.results)
// }
// dataTask.resume()
}
// MARK: - LOADMOVIEIMAGE FUNC
func loadMovieImage(backdropPath: String, completion: @escaping (UIImage) -> Void) {
let imageUrl = NetworkConstants.imageUrlFirstPart + backdropPath
guard let url = URL(string: imageUrl) else {
completion(UIImage(named: "artwork-source") ?? UIImage())
return
}
do {
let session = URLSession.shared
let task = session.dataTask(with: url) { data, response, error in
guard let dataResponse = data, let image = UIImage(data: dataResponse) else {
return
}
completion(image)
}
task.resume()
}
// let imageUrl = NetworkConstants.imageUrlFirstPart + backdropPath
// guard let url = URL(string: imageUrl) else {
// completion(UIImage(named: "artwork-source") ?? UIImage())
// return
// }
// let session = URLSession.shared
// let task = session.dataTask(with: url) { data, response, error in
// guard let dataResponse = data, let image = UIImage(data: dataResponse) else {
// completion(UIImage(named: "artwork-source") ?? UIImage())
// return
// }
// completion(image)
// }
// task.resume()
// }
}
}