Skip to content

Instantly share code, notes, and snippets.

@matux
Created July 28, 2015 00:57
Show Gist options
  • Select an option

  • Save matux/32b8df7985335f3a31ff to your computer and use it in GitHub Desktop.

Select an option

Save matux/32b8df7985335f3a31ff to your computer and use it in GitHub Desktop.
import UIKit
infix operator » {}
func » <U: BaseSegueIdentifier, T: UIViewController where T: Segueable, T.SegueIdentifier == U>
(viewController: T, destination: U)
{
viewController.performSegueWithIdentifier(destination.identifier, sender: viewController)
}
protocol Segueable {
typealias SegueIdentifier
typealias Segue
}
class BaseSegueIdentifier {
let identifier: String
init(_ identifier: String) {
self.identifier = identifier
}
}
class BaseViewController: UIViewController {
private var transientSeguePreparationClosure: ((segue: UIStoryboardSegue) -> Void)? = nil
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
self.transientSeguePreparationClosure?(segue: segue)
self.transientSeguePreparationClosure = nil
}
func performSegue(segueIdentifier: BaseSegueIdentifier, prepare: ((segue: UIStoryboardSegue) -> Void)? = nil) {
self.transientSeguePreparationClosure = prepare
self.performSegueWithIdentifier(segueIdentifier.identifier, sender: self)
}
}
infix operator ◊ {}
func ◊ <U: BaseSegueIdentifier, T: UIViewController where T: Segueable, T.SegueIdentifier == U>
(viewController: T, destination: U) -> (() -> Void) -> Void
{
return { closure in closure() }
}
// -----------------------------------------------------------------------------------------------------------
class AddNameAndEmailViewController: BaseViewController {
func haceloPorMi() {
// Validated
self » Segue.AddPhoneNumber
// Not validated
self.performSegueWithIdentifier(Segue.AddPhoneNumber.identifier, sender: self)
// self.performSegue(.AddPhoneNumber) { storyboardSegue in
// // preparation
// }
// Invalid
//self » EditProfileViewController.Segue.ProfilePhoto
//self » BaseSegueIdentifier("profilePhoto")
// The Dream
(self ◊ Segue.AddPhoneNumber) { storyboardSegue in
storyboardSegue.tuVieja = true
}
}
}
extension AddNameAndEmailViewController: Segueable {
typealias Segue = AddNameAndEmailViewController.SegueDestinations
final class SegueIdentifier: BaseSegueIdentifier {}
struct SegueDestinations {
static let TermsOfService = SegueIdentifier("termsOfService")
static let AddPhoneNumber = SegueIdentifier("addPhoneNumber")
}
}
// -----------------------------------------------------------------------------------------------------------
class EditProfileViewController: BaseViewController {
func haceloPorMi() {
self.performSegueWithIdentifier(Segue.ProfilePhoto.identifier, sender: self)
self » Segue.PlaceSearch
}
}
extension EditProfileViewController: Segueable {
typealias Segue = EditProfileViewController.SegueDestinations
final class SegueIdentifier: BaseSegueIdentifier {}
struct SegueDestinations {
static let ProfilePhoto = SegueIdentifier("profilePhoto")
static let PlaceSearch = SegueIdentifier("PlaceSearch")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment