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

Assisted inject of generic typed inputs isn't supported #454

Open
TheKeeperOfPie opened this issue Nov 17, 2024 · 2 comments
Open

Assisted inject of generic typed inputs isn't supported #454

TheKeeperOfPie opened this issue Nov 17, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@TheKeeperOfPie
Copy link

Basically, the following code doesn't work. I'm trying to create a ViewModel with a type parameter using assisted inject, but it fails with

[ksp] .../MyApplication/app/src/main/java/com/example/myapplication/Component.kt:17: Mismatched @Assisted parameters.
Expected: [savedStateHandle: androidx.lifecycle.SavedStateHandle, provider: TypedProvider]
But got:  [androidx.lifecycle.SavedStateHandle, com.example.myapplication.TypedProvider<kotlin.String>]
@Component
abstract class Component {
    abstract val viewModel: (SavedStateHandle, TypedProvider<String>) -> TypedViewModel<String>
}

@Inject
object Singleton

@Inject
class TypedViewModel<Type>(
    singleton: Singleton,
    @Assisted savedStateHandle: SavedStateHandle,
    @Assisted provider: TypedProvider<Type>,
)

interface TypedProvider<Type> {
    fun generate(): Type
}

There is a workaround by doing an inner factory class instead, but this feels like something that should be supported.

abstract class Component {
    abstract val viewModel: (SavedStateHandle) -> TypedViewModel.Factory
}

class TypedViewModel(...) {
    @Inject
    class Factory(
        private val singleton: Singleton,
        @Assisted private val savedStateHandle: SavedStateHandle
    ) {
        fun <Type> provide(provider: TypedProvider<Type>) =
            TypedViewModel(singleton, savedStateHandle, provider)
    }
}
@evant evant added the bug Something isn't working label Nov 18, 2024
@Hospes
Copy link

Hospes commented Dec 1, 2024

Same happen for me, here is my samples:

@Inject
class HomeViewModel(
    @Assisted handle: SavedStateHandle,
    private val tickersPuller: TickersPullService,
) : ViewModel() {

    companion object {
        private const val TAG = "HomeViewModel"
    }
}

Then I have component to inject view model later:

@Component
interface HomeComponent {
    val homeViewModel: (SavedStateHandle) -> HomeViewModel
}

But when I'm doing actual inject with:

appDiComponent.homeViewModel(SavedStateHandle.createHandle(null, null))

I got this error:

e: [ksp] Mismatched @Assisted parameters.
Expected: [handle: SavedStateHandle]
But got:  [SavedStateHandle]
e: Error occurred in KSP, check log for detail

Is it bug of library or am I do something wrong ?

@Hospes
Copy link

Hospes commented Dec 15, 2024

@evant hi, i'm sorry for botherin you, but is there any update on this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants