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

Strongly typed viewModel getter extensions #1995

Open
chokokatana opened this issue Sep 22, 2024 · 0 comments
Open

Strongly typed viewModel getter extensions #1995

chokokatana opened this issue Sep 22, 2024 · 0 comments

Comments

@chokokatana
Copy link

chokokatana commented Sep 22, 2024

Is your feature request related to a problem? Please describe.

Given the following presumed viewModel:

class CosmosDetailsVm(
    currency: KmpCurrency,
    network: KmpNetwork,
) {
…
}

and the module definition:

val kmpVmModule = module {
    viewModel { (currency: KmpCurrency, network: KmpNetwork) ->
        CosmosDetailsVm(
            currency = currency,
            network = network,
        )
    }
}

We are telling Koin there are two parameters to be passed. Sadly, users have to use the unsafe:

    private val realVm: CosmosDetailsVm by viewModel {
        parametersOf(
            intent.getParcelableExtra(ARG_CURRENCY)!!,
            intent.getParcelableExtra(ARG_NETWORK)!!,
        )
    }

This is frustrating. Using annotation reduces some of this boilerplate for the creator, but still forces consumers to carefully pass the correct parameters.

Describe the solution you'd like

Either through the knowledge provided by the annotation @InjectedParam or some magic compiler that detects the definition of input params, have Koin generate the following extension function:

inline fun ComponentActivity.injectCosmosDetailsVm(
    currency: KmpCurrency,
    network: KmpNetwork,
): CosmosDetailsVm {
    return getViewModel {
        parametersOf(currency, network)
    }
}

This changes the injection caller site to look like:

    private val mRealVm by lazy {
        injectCosmosDetailsVm(
            intent.getParcelableExtra(ARG_CURRENCY)!!,
            intent.getParcelableExtra(ARG_NETWORK)!!,
        )
    }
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

1 participant