0

I have a feeling this is a simple misunderstanding of how Dagger works. Alas I'm not able to find the issue. I have a few GotIt like cards that I want to use all over. To do this I'm using a model which includes the icon, body text, action text, and action on-click handler. Various activities use different adapters to represent the state. For now I'm focusing on a card telling the user when they're missing location permissions. What I'd like to do is create a module which provides the various cards, what I have is (using Kotlin):

@Module
class GotItCardModule {

    @Provides @Singleton
    @Named(Manifest.permission.ACCESS_FINE_LOCATION)
    fun provideLocationGotItCard(application: Application): GotItViewHolder.GotItCard {
        val icon = ResourcesCompat.getDrawable(application.resources,
                R.drawable.ic_location_off_black_24dp, null)?.apply {
            DrawableCompat.setTint(this, Color.WHITE)
        }
        return GotItViewHolder.GotItCard(
                iconDrawable =  icon,
                bodyText = application.getString(R.string.location_permission_gotit_body),
                primaryButtonText = application.getString(R.string.location_permission_gotit_action_primary),
                primaryButtonCallback = View.OnClickListener { v ->
                    (v.context as? Activity)?.let { activity ->
                        ActivityCompat.requestPermissions(activity,
                            arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                            RequestCode.Permission.ACCESS_FINE_LOCATION)
                    }
                })
    }
}

I've updated my AppComponent to include GotItCardModule::class and my MainActivity with:

@Inject @Named(Manifest.permission.ACCESS_FINE_LOCATION)
lateinit var locationGotItCard: GotItViewHolder.GotItCard

When I build I get an error:

Error:(6, 1) error: [dagger.android.AndroidInjector.inject(T)] com.sample.test.adapters.holders.GotItViewHolder.GotItCard cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.

What I don't understand is that I have an @Provides and I included this module at the very top level. Why can't it resolve this?

UPDATE:

If I remove @Named everything works (I just can't have more than one GotItCard). I also tried replacing the string with a hard-coded string. I still get the same error.

TheHebrewHammer
  • 3,018
  • 3
  • 28
  • 45

0 Answers0