This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window = UIWindow(frame: UIScreen.main.bounds) | |
let controller = HomeViewController(nibName: "HomeViewController", bundle: nil) | |
window?.rootViewController = controller | |
window?.makeKeyAndVisible() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func setupView() { | |
let moviesVC = HomeViewController(nibName: "HomeViewController", bundle: nil) | |
moviesVC.title = "Movies" | |
let seriesVC = HomeViewController(nibName: "HomeViewController", bundle: nil) | |
seriesVC.title = "Series" | |
let settingsVC = SettingsViewController(nibName: "SettingsViewController", bundle: nil) | |
settingsVC.title = "Settings" | |
viewControllers = [moviesVC, seriesVC, settingsVC] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct NetworkManager { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func executeRequest<T: Codable>(request: URLRequest, completion: ((T?, Error?) -> Void)?) { | |
let session = URLSession(configuration: .default) | |
let dataTask = session.dataTask(with: request) { (data, response, error) in | |
guard let data = data else { | |
completion?(nil, error) | |
return | |
} | |
if let decodedResponse = try? JSONDecoder().decode(T.self, from: data) { | |
DispatchQueue.main.async { | |
completion?(decodedResponse, nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let decodedResponse = try? JSONDecoder().decode(T.self, from: data) { | |
DispatchQueue.main.async { | |
completion?(decodedResponse, nil) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias RailCompletionClosure = ((PageModel?, Error?) -> Void) | |
public func fetchMovieData(completion: RailCompletionClosure?) { | |
guard let request = createRequest(for: "https://raw.githubusercontent.com/atulkhatri/random/master/bootcamp-home-movies.json") else { | |
completion?(nil, NetworkError.invalidUrl) | |
return | |
} | |
executeRequest(request: request, completion: completion) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NetworkError: Error { | |
case invalidUrl | |
case invalidData | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct PageModel: Codable { | |
let data: [RailModel] | |
} | |
struct RailModel: Codable { | |
let title, type: String | |
let list: [AssetModel] | |
} | |
struct AssetModel: Codable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NetworkManager().fetchMovieData { [weak self] (page, error) in | |
guard let self = self else { return } | |
if let page = page { | |
self.loadData(page: page) | |
} else { | |
print("No movies found!!") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func createRequest(for url: String) -> URLRequest? { | |
guard let url = URL(string: url) else { return nil } | |
var request = URLRequest(url: url) | |
request.httpMethod = "GET" | |
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") | |
return request | |
} |
OlderNewer