Skip to content

Instantly share code, notes, and snippets.

@alanf
Created February 6, 2018 22:23
Show Gist options
  • Save alanf/3d08c6a42ae1fb33470adf879956299d to your computer and use it in GitHub Desktop.
Save alanf/3d08c6a42ae1fb33470adf879956299d to your computer and use it in GitHub Desktop.
import Foundation
import ReactiveKit
import Bond
import UIKit
protocol SimpleAlertDelegate: class {
func didTapSecondaryButton(alertViewController: SimpleAlertViewController)
func didTapPrimaryButton(alertViewController: SimpleAlertViewController)
}
class SimpleAlertViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var messageTextView: UITextView!
@IBOutlet weak var secondaryButton: UIButton!
@IBOutlet weak var primaryButton: UIButton!
struct ViewModel {
let title = Property<String?>(nil)
let message = Property<String?>(nil)
let primaryButtonTitle = Property<String?>(nil)
let secondaryButtonTitle = Property<String?>(nil)
}
private weak var delegate: SimpleAlertDelegate?
private var viewModel: ViewModel?
func configure(viewModel: ViewModel, delegate: SimpleAlertDelegate) {
self.delegate = delegate
self.viewModel = viewModel
}
override func viewDidLoad() {
super.viewDidLoad()
guard let viewModel = viewModel else { fatalError() }
viewModel.title.bind(to: titleLabel).dispose(in: bag)
viewModel.message.bind(to: messageTextView).dispose(in: bag)
viewModel.primaryButtonTitle.bind(to: primaryButton.reactive.title).dispose(in: bag)
viewModel.secondaryButtonTitle.bind(to: secondaryButton.reactive.title).dispose(in: bag)
}
@IBAction func didTapSecondaryButton(_ sender: Any) {
delegate?.didTapSecondaryButton(alertViewController: self)
}
@IBAction func didTapPrimaryButton(_ sender: Any) {
delegate?.didTapPrimaryButton(alertViewController: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment