Colton Idle
01/27/2023, 7:26 AMclass 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?Manuel Vivo
01/27/2023, 7:31 AM@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
Colton Idle
01/27/2023, 7:50 AM@AssistedFactory
interface GetTeamsUseCaseFactory {
fun create(accessToken: String): GetTeamsUseCase
}
Should I put this inside of my GetTeamsUseCase class or outside of it?@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()
Manuel Vivo
01/27/2023, 8:23 AMShould 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
Colton Idle
01/27/2023, 8:30 AM