Last active
November 29, 2018 22:37
-
-
Save iSapozhnik/6e052c5e8aff3c805dc4eb8b487b561d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Injectable { | |
associatedtype T | |
func inject(_ parameters: T) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct LoginFlowInjecter: Injecter { | |
func prepare(for segue: UIStoryboardSegue, parameters: Injectee...) { | |
switch segue.destination { | |
case let vc as SignUpViewController: | |
let parameter: InjectableUserManager = self.parameter(from: parameters) | |
vc.inject(parameter) | |
default: | |
break | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginViewController: UIViewController { | |
@IBOutlet weak var emailTextfield: UITextField! | |
@IBOutlet weak var passwordTextfield: UITextField! | |
private var userManager: InjectableUserManager? | |
private let router = LoginFlowInjecter() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
@IBAction func login(_ sender: Any) { | |
guard let email = emailTextfield.text, let password = passwordTextfield.text else { return } | |
userManager?.signIn(withEmail: email, password: password, completion: { success in | |
// Hooray! | |
}) | |
} | |
@IBAction func signup(_ sender: Any) { | |
} | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
// TODO: prepare is a variadic func | |
guard let userManager = self.userManager else { return } | |
router.prepare(for: segue, parameters: userManager) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Routable { | |
func prepare(for segue: UIStoryboardSegue, parameters: Injectee...) | |
} | |
extension Routable { | |
func parameter<T>(from parameters: [Injectee]) -> T { | |
for someParameter in parameters { | |
if let parameter = someParameter as? T { | |
return parameter | |
} | |
} | |
fatalError("Can not find injectable parameter") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol InjectableUserManager: Injectee { | |
func signIn(withEmail email: String, password: String, completion: @escaping (Result<Bool, UserError>) -> Void) | |
func signUp(withName name: String, email: String, password: String, completion: @escaping (Result<User, UserError>) -> Void) | |
} | |
class UserManager: InjectableUserManager { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment