Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active January 1, 2023 17:05
Show Gist options
  • Select an option

  • Save manuelvicnt/437668cda3a891d347e134b1de29aee1 to your computer and use it in GitHub Desktop.

Select an option

Save manuelvicnt/437668cda3a891d347e134b1de29aee1 to your computer and use it in GitHub Desktop.

Revisions

  1. manuelvicnt revised this gist Jun 22, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    // IMPORTANT! READ THIS FIRST
    // Assisted Injection doesn't work with @HiltViewModel or @ViewModelInject
    // Read more about the issue here: https://github.com/google/dagger/issues/2287
    //
    //
    // AssistedInject and Hilt working together in v2.28-alpha times
    // Example of a ViewModel using AssistedInject injected in a Fragment by Hilt
    // As AssistedInject isn't part of Dagger yet, we cannot use in
  2. manuelvicnt revised this gist Oct 26, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,7 @@ class MyViewModel @AssistedInject constructor(
    }


    // This code uses "com.squareup.inject:assisted-inject-annotations-dagger2:0.5.2"
    // AssistedInject puts all assisted bindings in the same module.
    // We need to make a decision about where to install it.
    // In this case, as we only need it in fragments, we install it there.
  3. manuelvicnt revised this gist Jun 19, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@ data class MyInitParams(private val neededId: String)

    class MyViewModel @AssistedInject constructor(
    ... // Other dependencies injected by Hilt
    // com.squareup.inject.assisted.Assisted annotation
    @Assisted private val initParams: MyInitParams
    ) : ViewModel() {
    ... // ViewModel logic
  4. manuelvicnt revised this gist Jun 19, 2020. 1 changed file with 13 additions and 5 deletions.
    18 changes: 13 additions & 5 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,28 @@
    // AssistedInject and Hilt working together in v2.28-alpha times
    // Example of a ViewModel using AssistedInject injected in a Fragment by Hilt
    // As AssistedInject isn't part of Dagger yet, we cannot use in
    // conjuction with @ViewModelInject

    data class MyInitParams(private val neededId: String)

    class MyViewModel @AssistedInject constructor(
    @Assisted private val assistedId: String
    ... // Other dependencies injected by Hilt
    @Assisted private val initParams: MyInitParams
    ) : ViewModel() {
    ... // ViewModel logic

    @AssistedInject.Factory
    interface AssistedFactory {
    fun create(assistedId: String): MyViewModel
    fun create(initParams: MyInitParams): MyViewModel
    }

    companion object {
    fun provideFactory(
    assistedFactory: AssistedFactory,
    assistedId: String
    initParams: MyInitParams
    ): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
    return assistedFactory.create(assitedId) as T
    return assistedFactory.create(initParams) as T
    }
    }
    }
    @@ -37,19 +42,22 @@ interface AssistedInjectModule {}
    @AndroidEntryPoint // Fragment injected by Hilt
    class MyFragment : Fragment() {

    private val args: MyFragmentArgs by navArgs()

    // Inject the factory generated by AssistedInject
    @Inject lateinit var myViewModelAssistedFactory: MyViewModel.AssistedFactory

    // Initialize the ViewModel using ViewModelProvider.Factory
    private val myViewModel: MyViewModel by viewModels {
    MyViewModel.provideFactory(
    myViewModelAssistedFactory, "assistedId"
    myViewModelAssistedFactory, MyInitParams(args.neededId)
    )
    }

    ... // Fragment logic
    }


    // To disable checking for the InstallIn annotation in some modules,
    // you need to enable the compiler option in your build.gradle file like this:
    android {
  5. manuelvicnt revised this gist Jun 19, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,7 @@
    class MyViewModel @AssistedInject constructor(
    @Assisted private val assistedId: String
    ) : ViewModel() {

    // ViewModel logic
    ... // ViewModel logic

    @AssistedInject.Factory
    interface AssistedFactory {
    @@ -47,6 +46,8 @@ class MyFragment : Fragment() {
    myViewModelAssistedFactory, "assistedId"
    )
    }

    ... // Fragment logic
    }

    // To disable checking for the InstallIn annotation in some modules,
  6. manuelvicnt revised this gist Jun 19, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,10 @@ interface AssistedInjectModule {}
    @AndroidEntryPoint // Fragment injected by Hilt
    class MyFragment : Fragment() {

    // Inject the factory generated by AssistedInject
    @Inject lateinit var myViewModelAssistedFactory: MyViewModel.AssistedFactory

    // Initialize the ViewModel using ViewModelProvider.Factory
    private val myViewModel: MyViewModel by viewModels {
    MyViewModel.provideFactory(
    myViewModelAssistedFactory, "assistedId"
  7. manuelvicnt created this gist Jun 19, 2020.
    59 changes: 59 additions & 0 deletions AnAndroidApp.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // AssistedInject and Hilt working together in v2.28-alpha times
    // Example of a ViewModel using AssistedInject injected in a Fragment by Hilt

    class MyViewModel @AssistedInject constructor(
    @Assisted private val assistedId: String
    ) : ViewModel() {

    // ViewModel logic

    @AssistedInject.Factory
    interface AssistedFactory {
    fun create(assistedId: String): MyViewModel
    }

    companion object {
    fun provideFactory(
    assistedFactory: AssistedFactory,
    assistedId: String
    ): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
    return assistedFactory.create(assitedId) as T
    }
    }
    }
    }


    // AssistedInject puts all assisted bindings in the same module.
    // We need to make a decision about where to install it.
    // In this case, as we only need it in fragments, we install it there.
    @InstallIn(FragmentComponent::class)
    @AssistedModule
    @Module(includes = [AssistedInject_AssistedInjectModule::class])
    // Needed until AssistedInject is incorporated into Dagger
    interface AssistedInjectModule {}


    @AndroidEntryPoint // Fragment injected by Hilt
    class MyFragment : Fragment() {

    @Inject lateinit var myViewModelAssistedFactory: MyViewModel.AssistedFactory
    private val myViewModel: MyViewModel by viewModels {
    MyViewModel.provideFactory(
    myViewModelAssistedFactory, "assistedId"
    )
    }
    }

    // To disable checking for the InstallIn annotation in some modules,
    // you need to enable the compiler option in your build.gradle file like this:
    android {
    defaultConfig {
    javaCompileOptions {
    annotationProcessorOptions {
    arguments["dagger.hilt.disableModulesHaveInstallInCheck"] = "true"
    }
    }
    }
    }