Created
July 15, 2018 15:09
-
-
Save arifinfrds/e6c20bb0c4305d4d8fbdd184d6c73aee to your computer and use it in GitHub Desktop.
arifinfrds/iOS-MVCS-Alamofire/MVCS Alamofire/controller/ViewController.swift
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
import UIKit | |
import SDWebImage | |
class ViewController: UIViewController { | |
// MARK: - Outlet | |
@IBOutlet weak var headerImageView: UIImageView! | |
@IBOutlet weak var titleLabel: UILabel! | |
@IBOutlet weak var subtitleLabel: UILabel! | |
// MARK: - View life cycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
attemptFetchPhoto(withId: 1) | |
} | |
// MARK: - Networking | |
func attemptFetchPhoto(withId id: Int) { | |
DataService.shared.requestFetchPhoto(with: id) {[weak self] (photo, error) in | |
if let error = error { | |
print("error: \(error.localizedDescription)") | |
return | |
} | |
guard let photo = photo else { | |
return | |
} | |
self?.updateUI(with: photo) | |
} | |
} | |
// MARK: - UI Setup | |
func updateUI(with photo: Photo) { | |
print("photo: \(photo)") | |
// change http to https with own extension function first | |
if | |
let urlString = photo.url, | |
let finalUrlString = String.replaceHttpToHttps(with: urlString), | |
let url = URL(string: finalUrlString), | |
let title = photo.title, | |
let albumId = photo.albumID | |
{ | |
headerImageView.sd_setImage(with: url, completed: nil) | |
titleLabel.text = title | |
subtitleLabel.text = "This photo has albumID: \(albumId)" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment