Creating the signup presenter

As we did when creating the LoginPresenter, we need to create a SignUpPresenter interface along with a SignUpPresenterImpl class. The SignUpPresenter we are making is in no way complex. For this application, we need our signup presenter to possess a property of the AppPreferences type as well as a function that executes the signup process. The following is the SignUpPresenter interface:

package com.example.messenger.ui.signup

import com.example.messenger.data.local.AppPreferences

interface SignUpPresenter {
var preferences: AppPreferences

fun executeSignUp(username: String, phoneNumber: String, password: String)
}

Now, here is the code for our SignUpPresenter implementation:

package com.example.messenger.ui.signup

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

class SignUpPresenterImpl(private val view: SignUpView): SignUpPresenter,
SignUpInteractor.OnSignUpFinishedListener,
AuthInteractor.onAuthFinishedListener {

private val interactor: SignUpInteractor = SignUpInteractorImpl()
override var preferences: AppPreferences = AppPreferences
.create(view.getContext())

The onSuccess() callback below is invoked when user is successfully signed up:

 override fun onSuccess() {
interactor.getAuthorization(this)
}

The callback invoked when an error occurs during user sign up:


override fun onError() {
view.hideProgress()
view.showSignUpError()
}

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

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

override fun onPhoneNumberError() {
view.hideProgress()
view.setPhoneNumberError()
}

override fun executeSignUp(username: String, phoneNumber: String,
password: String) {
view.showProgress()
interactor.signUp(username, phoneNumber, password, this)
}

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

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

The preceding SignUpPresenterImpl class implements the SignUpPresenter, SignUpInteractor.OnSignUpFinishedListener, and AuthInteractor.onAuthFinishedListener interfaces, and, as such, provides implementations for a number of required functions. These functions are onSuccess(), onError(), onUsernameError(), onPasswordError(), onPhoneNumberError(), executeSignUp(String, String, String), onAuthSuccess(), and onAuthError(). SignUpPresenterImpl takes a single argument as its primary constructor. This argument must be of the SignUpView type.

executeSignUp(String, String, String) is a function that will be invoked by a SignUpView to begin the user registration process. onSuccess() is called when a user's signup request is successful. The function immediately invokes the interactor's getAuthorization() function to get an access token for the newly registered user. In a scenario when a signup request fails, the onError() callback is invoked. This hides the progress bar being shown to the user and displays an appropriate error message.

The onUsernameError(), onPasswordError(), and onPhoneNumberError() methods are callbacks invoked upon the occurrence of an error in a submitted username, password, or phone number, respectively. onAuthSuccess() is a callback invoked when the authorization procedure is successful. On the other hand, onAuthError() is invoked when the authorization fails.

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

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