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
protocol NibOwnerLoadable: AnyObject { | |
static var nib: UINib { get } | |
} | |
// MARK: - Default implmentation | |
extension NibOwnerLoadable { | |
static var nib: UINib { | |
UINib(nibName: String(describing: self), bundle: Bundle(for: self)) | |
} |
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
func dismissAndPresent() { | |
weak var pvc = self.presentingViewController | |
self.dismiss(animated: true, completion: { | |
let vc = ViewController() | |
pvc?.present(vc, animated: true, completion: 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
func dismissToRootVC() { | |
self.view.window!.rootViewController?.dismiss(animated: false, completion: 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
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
launchScreenAnimation() | |
} | |
private func launchScreenAnimation() { | |
guard let launchScreen = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController() else { return } | |
self.view.addSubview(launchScreen.view) |
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
var networkManager: NetworkManager! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
networkManager.getNewMovies(page: 1) { movies, error in | |
if let error = error { | |
print(error) | |
} | |
if let movies = movies { |
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
// 定義 getNewMovies 函數,其中包含頁數、和 [Movie]? 錯誤訊息(String?)的 Completion。 | |
func getNewMovies(page: Int, completion: @escaping (_ movie: [Movie]?,_ error: String?)->()){ | |
// 調用 Router,在一個閉包裡傳送 page 和處理 Completion。 | |
router.request(.newMovies(page: page)) { data, response, error in | |
// 如果沒有網路、或其他原因而無法呼叫 API,URLSession 會回傳一個錯誤。 | |
if error != nil { | |
// 注意這並不是 API 錯誤,而是用戶端的錯誤,而且很可能是因為網路連線不穩。 | |
completion(nil, "Please check your network connection.") | |
} | |
// 因為要判斷 Status Code,所以我們需要將 response 轉型成 HTTPURLResponse。 |
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
fileprivate func handleNetworkResponse(_ response: HTTPURLResponse) -> Result<String>{ | |
switch response.statusCode { | |
case 200...299: return .success | |
case 401...500: return .failure(NetworkResponse.authenticationError.rawValue) | |
case 501...599: return .failure(NetworkResponse.badRequest.rawValue) | |
case 600: return .failure(NetworkResponse.outdated.rawValue) | |
default: return .failure(NetworkResponse.failed.rawValue) | |
} | |
} |
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 MovieApiResponse { | |
let page: Int | |
let numberOfResults: Int | |
let numberOfPages: Int | |
let movies: [Movie] | |
} | |
extension MovieApiResponse: Decodable { | |
private enum MovieApiResponseCodingKeys: String, CodingKey { |
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
extension MovieApi: EndPointType { | |
var environmentBaseURL : String { | |
switch NetworkManager.environment { | |
case .production: return "https://api.themoviedb.org/3/movie/" | |
case .qa: return "https://qa.themoviedb.org/3/movie/" | |
case .staging: return "https://staging.themoviedb.org/3/movie/" | |
} | |
} | |
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
func request(_ route: EndPoint, completion: @escaping NetworkRouterCompletion) { | |
let session = URLSession.shared | |
do { | |
// 要建立 Request,我們會調用 buildRequest,並透過一個 route(即是 EndPoint) 來生成。 | |
let request = try buildRequest(from: route) | |
task = session.dataTask(with: request, completionHandler: { (data, response, error) in | |
// 將 data、response、error 傳回 Completion | |
completion(data, response, error) | |
}) | |
} catch { |
NewerOlder