Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is a User Model , struct that contains the user's login data
- struct User {
- var username: String?
- var password: String?
- }
- //This is the protocol to implement in the View Controller , being called by the presenter.
- protocol LoginView: class {
- func didLogin (error: Error?)
- }
- // The presenter's protocol
- protocol LoginPresenter {
- init (view: LoginView?, user: User?)
- func login ()
- }
- //The presenter itself , implementing the Login Presetner Protocol
- struct LoginViewPresenter: LoginPresenter {
- weak var view: LoginView?
- var user: User?
- init(view: LoginView?, user: User?) {
- self.view = view
- self.user = user
- }
- func login() {
- // Here you will place your login logic, including network call etc.
- // This is a sample only to demonstrate what happnes when you call didLogin() with no error
- view?.didLogin(error: nil)
- }
- }
- class LoginViewController: UIViewController, LoginView {
- //MARK: Properties
- var presenter: LoginPresenter?
- @IBOutlet weak var loginButton: UIButton!
- //MARK: Actions
- @IBAction func setDefaultLabelText(_ sender: UIButton) {
- presenter?.login() //Calling login method, through the presenter
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- setupLayout()
- }
- //Implementing the didLogin method, placed in the LoginView Protocol
- func didLogin(error: Error?) {
- if let error = error {
- print("Error -> \(error.localizedDescription)")
- } else {
- print("Logged in with user \(presenter?.user)")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement