Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dagger does not support providing @AssistedInject types #2396

Closed
alizarei95 opened this issue Feb 20, 2021 · 10 comments
Closed

Dagger does not support providing @AssistedInject types #2396

alizarei95 opened this issue Feb 20, 2021 · 10 comments

Comments

@alizarei95
Copy link

I am using Hilt (v2.32-alpha) dependency injection with viewmodel. I have a class with 2 parameters, the first is context and the second is an optional string so if not provide any value this takes null. To achieve this in Hilt I read below doc: https://dagger.dev/dev-guide/assisted-injection.html

This is my code :

class SharedPreferencesHelper @AssistedInject constructor(
    context: Context,
    @Assisted sharedPreferencesName: String? = null
) { ... }

And Hilt Module :

@Module
@InstallIn(ViewModelComponent::class)
object WebServicesModule {

    @Provides
    fun provideSharedPreferencesHelper(@ApplicationContext context: Context): SharedPreferencesHelper =
        SharedPreferencesHelper(context)
}

AssistedFactory interface:

@AssistedFactory
interface MyAssistedFactory {
    fun create(sharedPreferencesName: String): SharedPreferencesHelper
}

And my viewmodel :

@HiltViewModel
class SplashViewModel @Inject constructor(
    application: Application,
    savedStateHandle: SavedStateHandle,
    private val repository: SplashRepository
) : AndroidViewModel(application) {

    @Inject
    lateinit var assistedFactory: MyAssistedFactory

fun callSplashApi() {

        val preferencesHelper = assistedFactory.create("ss")
...
  }
}

But build fails with this error:

Dagger does not support providing @AssistedInject types.

@tbroyer
Copy link

tbroyer commented Feb 20, 2021

Duplicate of #2370?

@alizarei95
Copy link
Author

@tbroyer is there any way to handle this use case at this time?

@bcorso
Copy link

bcorso commented Feb 21, 2021

@alizarei95, note that #2370 will allow you to provide a qualified @AssistedInject type, but we're still not allowing an unqualified @AssistedInject type to be provided to avoid confusion.

Thus, you'll have to provide your default type with a qualifier like, @Default.

You probably also want to provide the default via the factory rather than calling the constructor directly. For example:

@Module
@InstallIn(ViewModelComponent::class)
object WebServicesModule {
    @Provides
    @Default
    fun provideDefault(factory: MyAssistedFactory) = factory.create()
}

@AssistedFactory
interface MyAssistedFactory {
  fun create(sharedPreferencesName: String ?= null): SharedPreferencesHelper
}

**Also, note that the fix for #2370 is not yet included in a release. It should be out with the next Dagger release, 2.33.

@alizarei95
Copy link
Author

@bcorso Thanks
I wrote your codes but again I got error:
Dagger does not support providing @AssistedInject types.

Is my case is an unusual scenario? This is a little bit strange for me there is not a simple solution

@bcorso
Copy link

bcorso commented Feb 22, 2021

@alizarei95 see the last part of my previous comment:

note that the fix for #2370 is not yet included in a release. It should be out with the next Dagger release, 2.33.

@alizarei95
Copy link
Author

@bcorso what about last part of my previous question?

Is my case is an unusual scenario? This is a little bit strange for me there is not a simple solution

@beigirad
Copy link

@bcorso Do you have any time estimation for releasing 2.33?
I have the same issue.

@beigirad
Copy link

@alizarei95

is there any way to handle this use case at this time?

You can use Square's AssistedInject for assisting inject at this time. And when 2.33 is out you change just annotations (May need to remove some lines)

@bcorso
Copy link

bcorso commented Feb 23, 2021

@beigirad we're should have a release out within the next week or two

@bcorso
Copy link

bcorso commented Feb 23, 2021

Closing this as a dupe of #2370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants