Skip to content

Instantly share code, notes, and snippets.

@alanf
Created February 6, 2018 22:30
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 alanf/9bdf3f76377fb754a3d37dbaf6899af2 to your computer and use it in GitHub Desktop.
Save alanf/9bdf3f76377fb754a3d37dbaf6899af2 to your computer and use it in GitHub Desktop.
class ExampleUsage: SimpleAlertDelegate {
private func configureRestPeriodAlert(lastStimulationTime: Date) -> SimpleAlertViewController {
let alertStoryboard = UIStoryboard(name: "Alert", bundle: Bundle.main)
guard let alertViewController = alertStoryboard.instantiateViewController(withIdentifier: "Alert") as? SimpleAlertViewController else { fatalError() }
alertViewController.modalTransitionStyle = .coverVertical
alertViewController.modalPresentationStyle = .overCurrentContext
let viewModel = SimpleAlertViewController.ViewModel()
viewModel.title.value = NSLocalizedString("RestPeriodAlertTitle", tableName: "", bundle: Bundle.main, value: "Rest Period", comment: "Title for alert which recommends waiting between Neuropriming sessions.")
let messageTemplate = NSLocalizedString("RestPeriodAlertMessage", tableName: "", bundle: Bundle.main, value: "Research supports a rest period of at least 8 hours between Neuropriming sessions.\n\nTime since last session:\n%@", comment: "Message for alert which recommends waiting between Neuropriming sessions. Argument is the time that has passed since the end of the last session, formatted as hh:mm:ss")
let messageFunc: (Int) -> (String) = { _ in return String(format: messageTemplate, lastStimulationTime.timeIntervalSinceNow.longStylizedSecondsString()) }
SafeSignal<Int>.interval(1.0).map(messageFunc).bind(to: viewModel.message)
viewModel.message.value = messageFunc(0)
viewModel.primaryButtonTitle.value = NSLocalizedString("RestPeriodAlertCancelButtonTitle", tableName: "", bundle: Bundle.main, value: "Wait", comment: "Title for alert button which indicates that the user will wait instead of proceeding with a new session.")
viewModel.secondaryButtonTitle.value = NSLocalizedString("RestPeriodAlertContinueButtonTitle", tableName: "", bundle: Bundle.main, value: "Continue", comment: "Title for alert button which indicates that the user will proceed with a new session despite the recommendation to wait.")
alertViewController.configure(viewModel: viewModel, delegate: self)
return alertViewController
}
func didTapPrimaryButton(alertViewController: SimpleAlertViewController) { /* */ }
func didTapSecondaryButton(alertViewController: SimpleAlertViewController) { /* */ }
func didAttemptToStimulate() {
let lastStimulationTime = Date() //
self.vc?.present(configureRestPeriodAlert(lastStimulationTime: lastStimulationTime), animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment