AnelCC / Dagger2Base_Code

Example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Dagger

Using Dagger in your Android app - Kotlin

Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for a good app architecture.

Implementing dependency injection provides you with the following advantages:

  • Reusability of code.
  • Ease of refactoring.
  • Ease of testing.

Why Dagger 2 is Different?

If the application gets larger, we will start writing a lot of boilerplate code (e.g. with Factories) which can be error-prone. Doing this wrong can lead to subtle bugs and memory leaks in your app.

In the codelab, we will see how to use Dagger to automate this process and generate the same code you would have written by hand otherwise.

Dagger will be in charge of creating the application graph for us. We'll also use Dagger to perform field injection in our Activities instead of creating the dependencies by hand.

Android

This repository contains Develop in android over MVVM, Live Data, Kotlin, etc.

Preview πŸŽ‰

The app consists of 4 different flows (implemented as Activities):

  • Registration: The user can register by introducing username, password and accepting our terms and conditions.
  • Login: The user can log in using the credentials introduced during the registration flow and can also unregister from the app.
  • Home: The user is welcomed and can see how many unread notifications they have.
  • Settings: The user can log out and refresh the number of unread notifications (which produces a random number of notifications).

The project follows a typical MVVM pattern where all the complexity of the View is deferred to a ViewModel. Take a moment to familiarize yourself with the structure of the project.

Dependencies are defined using the @Provides and @Binds annotations.

@Binds annotation to tell Dagger which implementation it needs to use when providing an interface. @Modules are a way to encapsulate how to provide objects in a semantic way. @Component annotation can access the information that by Modules. @BindsInstance. Provide an implementation when providing an interface.

How to create an Application graph using Dagger @Component annotation. How to add information to the graph using @Inject, @Module, @Binds and @BindsInstance annotations. How to create flow containers using @Subcomponent. How to reuse instances of objects in different containers using Scopes. Dagger Qualifiers and @Provides annotation. How to test your application that uses Dagger with unit and instrumentation tests.

Library References

  1. Kotlin
  2. Dagger 2

Package Structure

com.anelcc.name    # Root Package
.
β”œβ”€β”€ di            # Componentes and Modules. In this way, AppComponent can access the information
β”‚                   that StorageModule contains. @module, @binds from Storage Interfaz that cannot
β”‚                   be instantiated directly. we need to tell dagger what implementation of storage use.
β”œβ”€β”€ registration
β”‚   β”‚
β”‚   │── RegistrationActivity    # RegistrationActivity can access the graph to get objects injected (or populated) by Dagger,
β”‚   β”‚                             in this case RegistrationViewModel (because it is a field which is annotated with @Inject).
β”‚   └── RegistrationViewModel
β”‚
β”œβ”€β”€ login          # Views that can be injected by the Component
β”‚   β”‚              # In this case LoginActivity & LoginViewModel
β”‚   │── LoginActivity
β”‚   └── LoginViewModel
β”‚
β”œβ”€β”€ main           # Views that can be injected by the Component
β”‚   β”‚              # In this case MainViewModel
β”‚   └── MainViewModel
β”‚
β”œβ”€β”€ user          # Dagger provided instances of RegistrationViewModel and UserManager
β”‚   └── UserManager
β”‚
β”œβ”€β”€ MainActivity
└── MyApplication # Instance of the AppComponent

Library References

  1. Kotlin Read here
  2. Dependency injection in Android Read here
  3. Manual Dependency Injection in Android Read here
  4. Dagger basics Read here
  5. Dagger 2 by google Read here
  6. MVVM pattern Read here
  7. Latest available versions of Dagger Read here
  8. Kotlin lazy initialization Read here

About

Example


Languages

Language:Kotlin 96.5%Language:PureBasic 3.5%