Using RxAndroid to implement Signing up in the Open Event Android Application

In the Open Event Android Project, we utilise RxAndroid for making network calls. This blog will illustrate about how the process of signing up in the app is done by making a network call using RxAndroid!

In the open event android app, users can sign up in the app, which means that they can create an account and thus their user details can be stored in the server.

The work flow in the app is that at first, the user will be signing up and then he or she will be automatically logged into the app. Thus we make two network calls. One is for signing up and another is for logging in automatically.

Let us proceed to the code of signing up. In this blog we will only handle the backend part. The below code is the model of sign up.

@Type(“user”)

@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy::class)

data class SignUp(

   @Id(IntegerIdHandler::class)

   var firstName: String? = null,

   var lastName: String? = null,

   var email: String? = null,

   var password: String? = null

)

Next, we create a Sign Up fragment where we setup the onClickListener of the sign up button. The below code represents that.

rootView.signUpButton.setOnClickListener {

   signUp.email = usernameSignUp.text.toString()

   signUp.password = passwordSignUp.text.toString()

   signUp.firstName = firstNameText.text.toString()

   signUp.lastName = lastNameText.text.toString()

   confirmPassword = confirmPasswords.text.toString()

   signUpActivityViewModel.signUp(signUp, confirmPassword)

}

We can clearly observe that we are taking information from fields and putting them inside the object of SignUp. After that, we are calling a function from view model to sign up. Let us have a look at the viewmodel file. The signUp function is present below.

fun signUp(signUp: SignUp, confirmPassword: String) {

   email = signUp.email

   password = signUp.password

   if (hasErrors(email, password, confirmPassword)) return

   compositeDisposable.add(authService.signUp(signUp)

           .subscribeOn(Schedulers.io())

           .observeOn(AndroidSchedulers.mainThread())

           .doOnSubscribe {

               progress.value = true

           }.doFinally {

               progress.value = false

           }.subscribe({

               signedUp.value = it

               Timber.d(“Success!”)

           }, {

               error.value = “Unable to SignUp!”

               Timber.d(it, “Failed”)

           }))

}

Here we can clearly observe that we are adding a RxAndroid network call to a composite disposable. This network call is utilising a function present in AuthService. The .subscribeOn represents the thread where the hard work will occur. The .observeOn represents the thread where actual information is to be reflected. Thus, here we can see that once, the user signs up, the value of the MutableLiveData variable is set to the signed up user. After this, we have an observer in the Fragment file, where it calls a function login whenever the signing up is taken care of. Thus both processes occur simultaneously!

References

Tags: GSoC18, FOSSASIA, Open Event, Android, RxJava, RxAndroid, SignUp