Skip to content

Instantly share code, notes, and snippets.

@florianldt
Created February 3, 2020 03:16
Show Gist options
  • Save florianldt/df67d4bb01049f88d31c656f754e9fe3 to your computer and use it in GitHub Desktop.
Save florianldt/df67d4bb01049f88d31c656f754e9fe3 to your computer and use it in GitHub Desktop.
How to build a URL based on information passed on pushViewController
import UIKit
import PlaygroundSupport
enum Styles {
enum Colors {
static let buttonBackgroundColor = UIColor(red: 72/255,
green: 199/255,
blue: 116/255,
alpha: 1)
}
}
class HomeViewController: UIViewController {
enum Settings {
static let videoId = "9e074ec5"
}
let toDetailButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("To video \(Settings.videoId)",
for: .normal)
button.backgroundColor = Styles.Colors.buttonBackgroundColor
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 4
button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
button.contentEdgeInsets = UIEdgeInsets(top: 0,
left: 16,
bottom: 0,
right: 16)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
title = "Home"
view.backgroundColor = .white
view.addSubview(toDetailButton)
NSLayoutConstraint.activate([
toDetailButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
toDetailButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
toDetailButton.heightAnchor.constraint(equalToConstant: 40),
])
toDetailButton.addTarget(self,
action: #selector(onToDetailButton),
for: .touchUpInside)
}
@objc
private func onToDetailButton() {
let detailViewController = DetailViewController(videoId: Settings.videoId)
navigationController?.pushViewController(detailViewController,
animated: true)
}
}
class DetailViewController: UIViewController {
enum Settings {
static let endpoint = URL(string: "http://download.appboomclap.co.kr/content")!
static let fileExtention = "mp4"
}
let videoId: String
let videoUrlLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 16)
label.numberOfLines = 2
label.textAlignment = .center
return label
}()
init(videoId: String) {
self.videoId = videoId
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("TODO")
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Detail"
view.backgroundColor = .white
view.addSubview(videoUrlLabel)
NSLayoutConstraint.activate([
videoUrlLabel.leftAnchor.constraint(equalTo: view.leftAnchor,
constant: 20),
videoUrlLabel.rightAnchor.constraint(equalTo: view.rightAnchor,
constant: -20),
videoUrlLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
let url = urlBuilder()
videoUrlLabel.text = url.absoluteString
}
private func urlBuilder() -> URL {
let url = Settings
.endpoint
.appendingPathComponent(videoId)
.appendingPathExtension(Settings.fileExtention)
return url
}
}
let viewController = HomeViewController()
let navigationController = UINavigationController(rootViewController: viewController)
PlaygroundPage.current.liveView = navigationController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment