This file contains hidden or 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 fetchData() { | |
| switch type { | |
| case .movies: | |
| 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 hidden or 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
| extension HomeViewController: UICollectionViewDelegateFlowLayout { | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
| switch type { | |
| case .movies: return CGSize(width: collectionView.frame.size.width, height: Constants.moviesCellHeight) | |
| case .series: return CGSize(width: collectionView.frame.size.width, height: Constants.seriesCellHeight) | |
| } | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { | |
| return CGSize(width: collectionView.frame.size.width, height: Constants.sectionHeaderHeight) |
This file contains hidden or 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
| extension HomeViewController: UICollectionViewDataSource { | |
| func numberOfSections(in collectionView: UICollectionView) -> Int { | |
| return pageModel?.data.count ?? 0 | |
| } | |
| func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
| return 1 | |
| } | |
| func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
This file contains hidden or 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
| // These are defined on top of the file | |
| private let kRailCellIdentifier = "RailCell" | |
| private let kRailHeaderIdentifier = "RailHeaderView" | |
| class HomeViewController: UIViewController { | |
| @IBOutlet weak var collectionView: UICollectionView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| setupView() |
This file contains hidden or 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 | |
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | |
| } | |
| } |