Finishing the LoginView

If you recall, we did not finish our implementation of the LoginView earlier. Functions such as navigateToSignUp(), navigateToHome(), and onClick(view: View) were left with empty bodies. In addition, the LoginView did not interact in any way with the LoginPresenter. Let's fix that now, shall we?

Fist things first, to navigate a user to the signup screen and home screen, we need views for these screens to exist. We won't concern ourselves with implementing layouts for them now (that will be done in the following sections). We just need them to exist. Create the signup and main packages under com.example.messenger.ui. Create a new empty activity called SignUpActivity in the signup package and an empty activity called MainActivity within main.

Now open LoginActivity.kt. We need to modify the previously mentioned functions to perform their respective tasks. In addition, we need to add private properties for a LoginPresenter instance and an AppPreferences instance. These changes are made in the following code snippet:

Firstly, add the properties below to the top of the LoginActivity class.

  private lateinit var progressBar: ProgressBar
private lateinit var presenter: LoginPresenter
private lateinit var preferences: AppPreferences

Now modify navigateToSignUp()navigateToHome(), and onClick(view: View) as shown in the following snippet:

override fun navigateToSignUp() {
startActivity(Intent(this, SignUpActivity::class.java))
}

override fun navigateToHome() {
finish()
startActivity(Intent(this, MainActivity::class.java))
}

override fun onClick(view: View) {
if (view.id == R.id.btn_login) {
presenter.executeLogin(etUsername.text.toString(),
etPassword.text.toString())
} else if (view.id == R.id.btn_sign_up) {
navigateToSignUp()
}
}

navigateToSignUp() uses an explicit intent to start SignUpActivity when called. navigateToHome() operates similarly to navigateToSignUp()it starts MainActivity. A major difference between navigateToHome() and navigateToSignUp() is that navigateToHome() destroys the current LoginActivity instance by calling finish() before starting the MainActivity.

The onClick() method uses the LoginPresenter to begin the login process in the scenario that the login button is clicked. Otherwise, if the signup button is clicked, the SignUpActivity is started with navigateToSignUp().

Great job thus far! We have created the necessary view, presenter, and model for login-related application logic. We need to keep in mind that before we can log in a user, we need to have registered a user on the platform first. Thus, we must implement our signup logic. We will do this in the following section.

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

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