Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Architecture vision with Dagger2.Android and Architecture Components

Notifications You must be signed in to change notification settings

Ufkoku/Dagger2.Android-Arch.Components

Repository files navigation

Dagger2.Android + Arch.Components

Example of usage of Dagger.Android and Architecture components

Main ideas

  1. Create an architecture with savable to Bundle ViewModels;
  2. Move ViewModel initialization to Dagger2.Android modules;
  3. Totally incapsulate ViewModel managment logic from final Activity/Fragment.

Main classes and key parts

  1. DaggerArchActivity implements Dagger2.Android contracts
  2. DaggerArchFragment same features as DaggerArchActivity has;
  3. DaggerArchDialogFragment same features as DaggerArchActivity has;
  4. DaggerViewModel which provides Factory and supports injections via AndroidInjector;
  5. DaggerSavableViewModel which provides Factory, supports injections via AndroidInjector and accepts SavedStateHandle as the second argument of constructor;
  6. Creating Factory for each ViewModel provides some boilerplate code, so there is an AnnotationProcessor for this purpose. Mark class with GenerateFactory and its constructors with ConstructorPriority, if needed. Usage example is here

Usage as library

You can add dagger2-arch-components module as dependency via maven:

repositories {
    maven { url 'https://dl.bintray.com/ufkoku/maven/' }
}

dependencies {
    def ver_dagger_arch_components = "2.4.0"
    implementation "com.ufkoku:dagger2-arch-components:$ver_dagger_arch_components"
    
    //optional if you want to use factories instead of injection to `ViewModel` 
    compileOnly "com.ufkoku:dagger2-arch-annotations:$ver_dagger_arch_components"
    kapt "com.ufkoku:dagger2-arch-processor:$ver_dagger_arch_components"
}

kapt {
    correctErrorTypes = true
}