Creating the signup view

It is time to work on the SignUpView. First we need to create a SignUpView interface, after which we will make SignUpActivity implement this interface. Note that in our application, a SignUpView is an extension of BaseView and AuthView. The following is the SignUpView interface:

package com.example.messenger.ui.signup

import com.example.messenger.ui.auth.AuthView
import com.example.messenger.ui.base.BaseView

interface SignUpView : BaseView, AuthView {

fun showProgress()
fun showSignUpError()
fun hideProgress()
fun setUsernameError()
fun setPhoneNumberError()
fun setPasswordError()
fun navigateToHome()
}

Now we shall modify the SignUpActivity class in the project to implement the SignUpView and make use of the SignUpPresenter. Add the changes in the following code snippet to SignUpActivity:

package com.example.messenger.ui.signup

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import com.example.messenger.R
import com.example.messenger.data.local.AppPreferences
import com.example.messenger.ui.main.MainActivity

class SignUpActivity : AppCompatActivity(), SignUpView, View.OnClickListener {

private lateinit var etUsername: EditText
private lateinit var etPhoneNumber: EditText
private lateinit var etPassword: EditText
private lateinit var btnSignUp: Button
private lateinit var progressBar: ProgressBar
private lateinit var presenter: SignUpPresenter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_up)
presenter = SignUpPresenterImpl(this)
presenter.preferences = AppPreferences.create(this)
bindViews()
}

override fun bindViews() {
etUsername = findViewById(R.id.et_username)
etPhoneNumber = findViewById(R.id.et_phone)
etPassword = findViewById(R.id.et_password)
btnSignUp = findViewById(R.id.btn_sign_up)
progressBar = findViewById(R.id.progress_bar)
btnSignUp.setOnClickListener(this)
}

override fun showProgress() {
progressBar.visibility = View.VISIBLE
}

override fun hideProgress() {
progressBar.visibility = View.GONE
}

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

override fun onClick(view: View) {
if (view.id == R.id.btn_sign_up) {
presenter.executeSignUp(etUsername.text.toString(),
etPhoneNumber.text.toString(),
etPassword.text.toString())
}
}
}

Now add the setUsernameError(), setPasswordError(), showAuthError(), showSignUpError() and getContext() functions shown below to SignUpActivity:

override fun setUsernameError() {
etUsername.error = "Username field cannot be empty"
}

override fun setPhoneNumberError() {
etPhoneNumber.error = "Phone number field cannot be empty"
}

override fun setPasswordError() {
etPassword.error = "Password field cannot be empty"
}

override fun showAuthError() {
Toast.makeText(this, "An authorization error occurred.
Please try again later."
,
Toast.LENGTH_LONG).show()
}

override fun showSignUpError() {
Toast.makeText(this, "An unexpected error occurred.
Please try again later."
,
Toast.LENGTH_LONG).show()
}

override fun getContext(): Context {
return this
}

Great work thus far! At this point, we are half of the way through the development of the Messenger application. You deserve a round of applause for your efforts. But we still have some work to do—especially with the main UI. We will finish up the remainder of the Messenger application in the next chapter. 

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

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