If you haven’t done the previous chapters, then from GitHub you can download the starter project for this chapter.
Step 1 – Extend UserRepository::class
In the chapter of Register with email using coroutine we have created the needed classes for the Firebase’s and Firestore’s queries. We have created for them first an interface, which will contain all the needed functions for the registration and login.
In this chapter we are going to extend it to fetch the currently logged in user from Firebase. This method will give back a FirebaseUser object.
So open the UserRepository::class from the “repository” package and add this line of code to it.
suspend fun checkUserLoggedIn(): FirebaseUser?
That’s all. Now you can close this file.
Step 2 – UserRepositoryImpl::class
Now open the UserRepositoryImpl::class. Because you have added a new function to the UserRepository::class, you should see a red underline at the beginning of this class. It is there, because you have to implement this method here also.
We gonna learn a new way of creating overridable methods. So in Android Studio press Control+o. In the popup window you should have the method of checkUserLoggedIn(). Select it and click on the “Ok” button.
Inside the body of this function we are going to have only one line of code. This will get back for us the logged in user from the Firebase’s authentication. For this we can use the variable of “firebaseAuth”. So add this line there.
Step 3 – FirebaseViewModel
In the FirebaseViewModel::class we are going to call the previously created checkUserLoggedIn() method. In the chapter of the registration we were talking about that the ViewModel class has an own CoroutineScope, called viewModelScope. In this scope we can call suspended functions, just like the checkUserLoggedIn() method is.
Because of the CoroutineScope, we have to create a temporary instance about the FirebaseUser::class, and this method will return this instance.
After this short summary our function should look like this:
Step 4 – SplashActivity
After the implementations of these methods finally we can add the code to the SplashActivity::class.
Add member variables
In this activity we are going to have 3 member variables. One for the FirebaseViewModel::class, one for the FirebaseUser::class and one for a new CoroutineScope, which will be coupled to this activity.
So just add this lines to the activity.
Member variables of the SplashActivity::class
Launch the CoroutineScope
Next is to launch the CoroutineScope inside of the onCreate() method. After the “setContentView” call add these lines.
Then inside of the CoroutineScope first we are going to initialize the “currentFirebaseUser” variable using the checkUserLoggedIn() method, what we can reach on the variable of “firebaseViewModel”.
Furthermore, we will have an if-else-statement, because we have to make sure the user is logged in.
If the currentFirebaseUser variable equals to null, then we are going to start the RegisterActivity::class. If it is not null, then we gonna launch the MainActivity::class.
So add this if-else-statement inside of the coroutineScope.
if (currentFirebaseUser == null)
We are going to add separetly a function for the Intent to start the activities. So our code won’t be boilerplate. The name of this function will be startActivity. It will have only one argument. This will be the activity, what we want to start.
Furthermore, we want to prevent the backwards navigation to the SplashActivity, so we are going to clear the back stack using a flag called “FLAG_ACTIVITY_CLEAR_TASK” and we gonna start the new activity with the flag of “FLAG_ACTIVITY_NEW_TASK”.
So here is the code.
if (currentFirebaseUser == null)
As we have talked about, in case if the currentFirebaseUser equals to null, then we are going to start the RegisterActivity::class. Hence add this line to this part of the if-else-statement.
The else statement
In the else part we are going to use the currentFirebaseUser varaible safely. So on the variable we are going to call the .let lambda then we will start the MainActivity::class.
Delay doesn’t block a thread, but only suspends the coroutine itself. The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool. The main thread must wait until our coroutine completes.
Add this line of code before the previous if-else-statement, but still inside of the “coroutineScope.launch”.
Finally the whole code of the coroutineScope:
Run the app
If you run now the app, then it should show you first the SplashActivity::class and after the delay it will be redirected to the next activity.
The source code is available on GitHub!
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂