Cloud DB Using with Hilt. Hi Guys! Today, We are going to talk… | by BegumAvci | May, 2022

Hi Guys! Today, We are going to talk about Cloud DB and Hilt. First of all, we need to learn what are these? Let`s start…

What is “Cloud DB”?

Cloud DB is a device-cloud synergy database product that provides data synergy management capabilities between the device and cloud, unified data models, and various data management APIs. In addition to ensuring data availability, reliability, consistency, and security, CloudDB enables seamless data synchronization between the device and cloud, and supports offline application operations, helping developers quickly develop device-cloud and multi-device synergy applications. As a part of the AppGallery Connect solution, Cloud DB builds the Mobile Backend as a Service (MBaaS) capability for the AppGallery Connect platform. In this way, application developers can focus on application services, greatly improving production efficiency.

What is “Dependency Injection”?

Dependency Injection is a widely used technique in professional Android application development. “Hilt” is a library that allows us to inject dependency. If we want a class to be able to act independently of the object it is bound to, we should use dependency injection. In this way, if the project grows, we can continue the project without changing too much code.
This project is basically an example of saving input from the user to Huawei Cloud DB using Hilt.

Now, we are starting the code example part.

  1. For Cloud DB integration, please; refer to this link
  2. Once Cloud DB is successfully integrated, we can start adding Hilt on our app-level build.gradle files.

3. After that we need to add another dependency in our project-level build.gradle file.

4. After these steps are completed, we first need to create an Application Class and add the “ @HiltAndroidApp” annotation that we will use hilt in this class.

Then we add the Application class we created to our manifest.

5. You can start enabling members injection in your other Android classes using the @AndroidEntryPoint annotation. You can use @AndroidEntryPoint on the following types:

  • Activity
  • Fragment
  • View
  • Service
  • BroadcastReceiver

In this project, we will inject activity.

Then we need to create our UserViewModel and inject it into the activity. We need to collect all the classes that we will use under the Hilt umbrella, because we cannot use the outside classes in Hilt, and this will create errors in our application.

Now we inject UserViewModel in activity. It is now defined in this scope. Since we will perform Cloud DB operations on the manager and call it from the view model, we need to specify this in the view model. I mean In order for Instances to be created automatically, that is, to use Hilt, we must make our existing classes Injectable.

We need to inject Cloud DB manager in the repository in accordance with the MVVM architecture. What we need to pay attention to here is to inject all connected classes into each other.

One of the most beautiful features of dependency injection is that we can install the things we will initialize only once and then relax without looking back.

Hilt modules are standard Dagger modules that have an additional @InstallIn annotation that determines which Hilt component(s) to install the module into.

For interfaces or classes that we cannot constructor-inject (Classes from some outside library), we have to create a Hilt module, where we can tell Hilt how to provide instances for those classes. In our case, we will create a few modules: RepositoryModule, DataSourceModule, NetworkModule and DatabaseModule, where we will provide all of the dependencies that cannot be constructor-injected.

Here I showed the first initialization of CloudDB. The @Singleton annotation here says it will only be installed once.

We do this in the CloudDBManager section like this:

When all these installations are finished, we call the userViewModel.saveUser() function from the button. In that function, it calls the saveUser() function from the repository we injected earlier.

In accordance with the MVVM architecture, we call the Cloud DB from the repository.

Finally, we can trigger our function via CloudDB manager as follows.

Note: If we want to add objects to Cloud DB, we have to do this manually since the auto increase feature is not currently supported.

In this article, I have explained the installation of Hilt and its use with Cloud DB through a sample project with usecase. And I gave information about how to add objects to Cloud DB.

References :

https://developer.android.com/training/dependency-injection/hilt-android

If any questions you can directly contact me, have a nice day !:)


BegumAvci

Source link