I feel like I'm getting some conflicting info with...
# dagger
c
I feel like I'm getting some conflicting info with hilt and assisted injection. I currently have this
Copy code
class GetTeamsUseCase @Inject constructor(
  val apiService: ApiService, <---- provided by a @Provides method
  val accessToken: String <---- I'd like to "inject" this at runtime since I get it once I login
) {
Is this possible to do or do I need some other lirbary?
m
It’s possible to do with Assisted Injection Dagger’s https://dagger.dev/dev-guide/assisted-injection.html
Instead of
@Inject
you need to use
@AssistedInject
, specify the
@Assisted
params and create a factory for it. However, you need to manually construct that type by injecting the factory and creating an instance of it in the class where you have access to
accessToken
c
Cool! First time trying assistedInject out so lets see what happens
I created
Copy code
@AssistedFactory
interface GetTeamsUseCaseFactory {
  fun create(accessToken: String): GetTeamsUseCase
}
Should I put this inside of my GetTeamsUseCase class or outside of it?
Is this basically how I should do it?
Copy code
@Inject lateinit var teamsUseCaseFactory: GetTeamsUseCaseFactory
lateinit var teamsUseCase: GetTeamsUseCase

fun setupService(): GetTeamsUseCase {
  val service = teamsUseCaseFactory.create(//my access token)
  return service
}

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  teamsUseCase = setupService()
m
Should I put this inside of my GetTeamsUseCase class or outside of it?
It’s fine to leave it in or at least in the same file
Is this basically how I should do it?
Yup, that looks good to me
c
Interesting. pretty basic then. nothing too crazy.
awesome. now i have tests working.
❤️ thanks @Manuel Vivo