Creating the login presenter

A presenter, as we saw in Chapter 3Implementing Tetris Logic and Functionality, is the middleman between a view and a model. It is necessary to create suitable presenters for views to properly facilitate clean view-model interactions. Creating a presenter is fairly easy. We need to first create an interface that properly declares the behaviors that will be exhibited by the presenter. Create a LoginPresenter interface in the login package with the following code:

package com.example.messenger.ui.login

interface LoginPresenter {
fun executeLogin(username: String, password: String)
}

As can be easily seen in the preceding code snippet, we want a class that acts as a LoginPresenter for a LoginView to possess an executeLogin(String, String) function. This function will be called by the view and will then interact with a model handling the login logic for the application. We will need to create a LoginPresenterImpl class that implements LoginPresenter:

package com.example.messenger.ui.login

import com.example.messenger.data.local.AppPreferences
import com.example.messenger.ui.auth.AuthInteractor


class LoginPresenterImpl(private val view: LoginView) :
LoginPresenter, AuthInteractor.onAuthFinishedListener,
LoginInteractor.OnDetailsRetrievalFinishedListener {

private val interactor: LoginInteractor = LoginInteractorImpl()
private val preferences: AppPreferences = AppPreferences.create(view.getContext())

override fun onPasswordError() {
view.hideProgress()
view.setPasswordError()
}

override fun onUsernameError() {
view.hideProgress()
view.setUsernameError()
}

override fun onAuthSuccess() {
interactor.persistAccessToken(preferences)
interactor.retrieveDetails(preferences, this)
}

override fun onAuthError() {
view.showAuthError()
view.hideProgress()
}

override fun onDetailsRetrievalSuccess() {
interactor.persistUserDetails(preferences)
view.hideProgress()
view.navigateToHome()
}

override fun onDetailsRetrievalError() {
interactor.retrieveDetails(preferences, this)
}

override fun executeLogin(username: String, password: String) {
view.showProgress()
interactor.login(username, password, this)
}
}

The LoginPresenterImpl class implements LoginPresenter, AuthInteractor.onAuthFinishedListener, and LoginInteractor.OnDetailsRetrievalFinishedListener, and, as such, implements all behaviors required by the interfaces. LoginPresenterImpl overrides seven functions in all: onPasswordError(), onUsernameError(), onAuthSuccess(), onAuthError(), onDetailsRetrievalSuccess(), onDetailsRetrievalError(), and executeLogin(String, String). The interaction between the LoginPresenter and LoginInteractor can be seen within the onAuthSuccess() and executeLogin(String, String) functions. When a user submits their login details, the LoginView calls the executeLogin(String, String) function in LoginPresenter. In turn, LoginPresenter uses LoginInteractor to handle the actual login procedure by calling the login(String, String) function of LoginInteractor.

If the user login is successful, the onAuthSuccess() callback function of LoginPresenter is invoked by the LoginInteractor. This then leads to the storing of the access token returned by the server and the retrieval of the logged-in user's account details. When the login request is declined by the server, onAuthError() is called and an informative error message is displayed to the user.

When a user's account details are successfully retrieved by the interactor, the onDetailsRetrievalSuccess() callback of LoginPresenter is invoked. This leads to the storage of the account details. The progress bar shown to the user over the course of the login is then hidden with view.hideProgress(), after which the user is navigated to the home screen with view.navigateToHome(). If the retrieval of user details fails, onDetailsRetrievalError() is called by LoginInteractor. The presenter then requests another attempt at retrieving the user's account details by calling interactor.retrieveDetails(preferences, this) once more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243