Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. //This is a User Model , struct that contains the user's login data
  2. struct User {
  3. var username: String?
  4. var password: String?
  5. }
  6.  
  7. //This is the protocol to implement in the View Controller , being called by the presenter.
  8. protocol LoginView: class {
  9. func didLogin (error: Error?)
  10. }
  11.  
  12. // The presenter's protocol
  13. protocol LoginPresenter {
  14. init (view: LoginView?, user: User?)
  15. func login ()
  16. }
  17.  
  18. //The presenter itself , implementing the Login Presetner Protocol
  19. struct LoginViewPresenter: LoginPresenter {
  20. weak var view: LoginView?
  21. var user: User?
  22.  
  23. init(view: LoginView?, user: User?) {
  24. self.view = view
  25. self.user = user
  26. }
  27.  
  28. func login() {
  29. // Here you will place your login logic, including network call etc.
  30. // This is a sample only to demonstrate what happnes when you call didLogin() with no error
  31. view?.didLogin(error: nil)
  32. }
  33. }
  34.  
  35. class LoginViewController: UIViewController, LoginView {
  36.  
  37. //MARK: Properties
  38. var presenter: LoginPresenter?
  39. @IBOutlet weak var loginButton: UIButton!
  40.  
  41. //MARK: Actions
  42. @IBAction func setDefaultLabelText(_ sender: UIButton) {
  43. presenter?.login() //Calling login method, through the presenter
  44. }
  45.  
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. setupLayout()
  49. }
  50.  
  51. //Implementing the didLogin method, placed in the LoginView Protocol
  52. func didLogin(error: Error?) {
  53. if let error = error {
  54. print("Error -> \(error.localizedDescription)")
  55. } else {
  56. print("Logged in with user \(presenter?.user)")
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement