Skip to content

Instantly share code, notes, and snippets.

@kylpo
Created July 10, 2019 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylpo/b18f2537678ff6e5af2a8798d7e3bb23 to your computer and use it in GitHub Desktop.
Save kylpo/b18f2537678ff6e5af2a8798d7e3bb23 to your computer and use it in GitHub Desktop.
Renderable
// Help with onTap from https://cocoacasts.com/elegant-controls-in-swift-with-closures
import UIKit
class Button: UIButton {
// typealias DidTapButton = (Button) -> Void
// var onTap: ((Button) -> Void)? {
var onTap: (() -> Void)? {
didSet {
if onTap != nil {
addTarget(self, action: #selector(didTouchUpInside), for: .touchUpInside)
} else {
removeTarget(self, action: #selector(didTouchUpInside), for: .touchUpInside)
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
tintColor = .black
// super.init(frame: frame, type: .system)
// type = .system
}
convenience init(apply closure: (Button) -> Void) {
self.init(frame: .zero)
closure(self)
}
@available(*, unavailable)
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Actions
@objc
func didTouchUpInside(sender: UIButton) {
if let handler = onTap {
// handler(self)
handler()
}
}
}
extension UIView {
@discardableResult
func button(apply closure: (Button) -> Void) -> Button {
return render(Button(), apply: closure)
}
}
import UIKit
class Col: UIStackView {
override init(frame: CGRect) {
super.init(frame: frame)
axis = .vertical
}
convenience init(apply closure: (Col) -> Void) {
self.init(frame: .zero)
closure(self)
}
@available(*, unavailable)
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIView {
@discardableResult
func col(apply closure: (Col) -> Void) -> Col {
return render(Col(), apply: closure)
}
}
class EditTallyScene: ViewController {
override func loadView() {
view = View {
$0.backgroundColor = .blue
$0.accessibilityIdentifier = Id.EditTally.scene
$0.col {
$0.edgesToSuperview(usingSafeArea: true)
$0.distribution = .equalCentering
$0.row {
$0.height(60)
$0.button {
$0.accessibilityIdentifier = Id.EditTally.cancelButton
$0.setTitle(.PLACEHOLDER, for: .normal)
$0.onTap = onExit
}
$0.button {
$0.accessibilityIdentifier = Id.EditTally.saveButton
$0.setTitle(.PLACEHOLDER, for: .normal)
$0.onTap = handleUserSaved
}
}
$0.textField {
$0.accessibilityIdentifier = Id.EditTally.nameInput
$0.tag = 1
$0.height(40)
$0.widthToSuperview()
}
$0.textField {
$0.accessibilityIdentifier = Id.EditTally.noteInput
$0.tag = 2
$0.height(40)
$0.widthToSuperview()
}
}
}
}
// MARK: - Actions
func handleUserSaved() {
_ = tallyManager.insert(name: "Hello")
onExit()
}
}
import UIKit
class Row: UIStackView {
override init(frame: CGRect = .zero) {
super.init(frame: frame)
axis = .horizontal
}
convenience init(apply closure: (Row) -> Void) {
self.init()
closure(self)
}
@available(*, unavailable)
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIView {
/// Inits, adds, configures, then returns subview Row
@discardableResult
func row(apply closure: (Row) -> Void) -> Row {
return render(Row(), apply: closure)
}
}
import UIKit
class TextField: UITextField {
convenience init(apply closure: (TextField) -> Void) {
self.init(frame: .zero)
closure(self)
}
}
extension UIView {
/// Inits, adds, configures, then returns subview Row
@discardableResult
func textField(apply closure: (TextField) -> Void) -> TextField {
return render(TextField(), apply: closure)
}
}
import UIKit
class View: UIView {
convenience init(apply closure: (View) -> Void) {
self.init(frame: .zero)
closure(self)
}
}
extension UIView {
@discardableResult
func view(apply closure: (View) -> Void) -> View {
return render(View(), apply: closure)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment