Firebase authentication – Koin

In the previous chapters we have implemented the user interface for all of our screens. In the very last chapter we have implemented the the repository for the queries of the Firebase authentication, and the ViewModel together with the LiveDatas and MutableLiveDatas. Because we want to have the FirebaseViewModel::class as singleton class in the whole app, we gonna add to our app the Koin library, which is a dependency injection framework.

GitHub

If you haven’t done the previous chapters, then from GitHub you can download the starter project for this chapter.

GitHub – the_viewmodel branch

Dependency Injection

First of all, we have to talk about dependency Injection. In software engineering, dependency injection is a technique, a design pattern whereby one object supplies the dependencies of another object. We can create the dependent object outside of the class and provide those object to class from different ways. DI can help with, moving the creation and binding of the dependent objects outside of the class that depends on them.

Traditional Coding

Dependency Injected

For example, when using an Object Oriented Programming language (such as Java/Kotlin on Android), objects will call methods on other objects that they have references to. A dependency is when one of the objects depends on the concrete implementation of another object.

Koin

Koin is pragmatic lightweight dependency injection framework for Kotlin developers. Written in pure Kotlin, using functional resolution as key concept. Koin is a DSL, a lightweight container and a pragmatic API. So, why use Koin rather than one of the other DI frameworks? The answer: Koin is more concise and straightforward than the others. According to the documentation, we can start using Koin in these three simple steps.

      1. Make a module: It contains the entities which will be injected at some point of our app
      2. Start Koin: Basically it is a single line: startKoin(this, listOf(applicationModule)) It allows us to launch the DI process and mark which modules will be available in our app.
      3. Perform an injection: Just declare a variable anywhere in our app using the lazy injections. val appRepository: AppRepository by inject()

If you aren’t familiar with Koin, then check out our 

Koin tutorial

Step 1 – Create Module

Our app already containes the Koin library, so we can start the implementation with the creation of the Module.

Create new Kotlin file
To create the Module, we gonna create again a new package. Click with the right mouse button on the package of the app, then “New”, and “Package”, and add for it the name “module”.

In this new package we gonna create a new Kotlin file. So click on the “module” package again with the right mouse button, “New”, then “Kotlin file/class”. Name the new file as “AppModuls”.

This module consists of a module block after the = sign. This block is the container in which you will map out the services that this module provides.

The ViewModel module

Currently in our new AppModuls.kt file we gonna have only one variable, which gonna be a module. This module will instantiate our FirebaseViewModel::class. So just add this lines of code to the file.


firebaseViewModelModule

Step 2 – Create the application class

Our moduls and Koin has to be created/started at the startup of our app. To achieve this, we have to create a new class, which will be inherited from the Application::class, and override the onCreate() method.

Create a new Kotlin file

So in the root package of our app we gonna create a new Kotlin file. Do it, as we did in case of the AppModules.kt file and name the new file as “MyApp”.

Extend the MyApp::class with the Application::class and override the onCreate() method. After them our file should look like this.


MyApp::class
Override onCreate

In the onCreate() method we gonna start Koin. So we gonna add startKoin{} lamba. For this lamba expression we need the application context and the list of the implemented modules.

Add this lines of code after the call of the super.onCreate().


startKoin {} 
Add MyApp to the AndriodManifest.xml
Before we can continue the implementation of Koin in our app, we have to add the MyApp::class to the AndroidManifest.xml. You can find this file in the “manifest” folder. Open it.

Inside of the “<application>” tag add this line:

android:name=“.MyApp”

This line will let to know for the Android system, that we have this class, which extends the Application::class.

Step 3 – Refactoring

From now on we can start to refactore our code to use Koin. Our app will be a small one, so we will modify only the usage of the FirebaseViewModel::class.

FirebaseViewModel

So open the FirebaseViewModel::class from the “repository” package.

There we have the variable of the UserRepositoryImpl::class. We gonna move it to the primary constructor of the class, like this.

class FirebaseViewModel(
    var userRepository: UserRepository
): ViewModel()

…and that’s all about the ViewModel. 😊

AppModuls

Because we have added a constructor to the FirebaseViewModel::class, we have to add it in the AppModuls Kotlin file as well, where do we have the “firebaseViewModelModule” module.
The the code should look like this.

val firebaseViewModelModule = module {
    single { FirebaseViewModel(UserRepositoryImpl()) }
}

Note that here we have mentioned the FirebaseViewModel::class using the “single” keyword, which comes from Koin. It means, that Koin gonna create only one instance for the whole app.

You can close this file as well.

RegisterActivity

The next class, what wo are going to modify is the RegisterActivity::class. Currently there we have declared the instance of the FirebaseViewModel::class as a lateinit var and we have initialized it in the “onCreate()” method. Delete both of them.

In place of the member variable we gonna have an instance also, but in this case we are going to use Koin, so just add this line there.

private val firebaseViewModel: FirebaseViewModel by inject()

The rest of the code can stay the same, because we can refer to it as we have done it before.

MainActivity

In the MainActivity::class we will do the same like what we did in the RegisterActivity::class. So remove also the lines of the FirebaseViewModel’s instance and repleace it with this line.

private val firebaseViewModel: FirebaseViewModel by inject()

Run the app

If you run the app, then it should work as before. 😎

GitHub

If it doesn’t run as it should be, then you can check the source code on GitHub.

GitHub – add_koin branch

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *