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

How to access LocalContext or stringResources in a Presenter? #268

Closed
amitkma opened this issue Jul 9, 2023 · 5 comments
Closed

How to access LocalContext or stringResources in a Presenter? #268

amitkma opened this issue Jul 9, 2023 · 5 comments

Comments

@amitkma
Copy link

amitkma commented Jul 9, 2023

I am trying to access Context inside a presenter composable using LocalContext.Current, But I couldn't make it work. I am getting the following error ->

java.lang.IllegalStateException: CompositionLocal LocalContext not present
    at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.noLocalProvidedFor(AndroidCompositionLocals.android.kt:168)
    at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.access$noLocalProvidedFor(AndroidCompositionLocals.android.kt:1)
    at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$LocalContext$1.invoke(AndroidCompositionLocals.android.kt:54)
    at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$LocalContext$1.invoke(AndroidCompositionLocals.android.kt:53)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at androidx.compose.runtime.LazyValueHolder.getCurrent(ValueHolders.kt:29)
    at androidx.compose.runtime.LazyValueHolder.getValue(ValueHolders.kt:31)
    at androidx.compose.runtime.ComposerImpl.resolveCompositionLocal(Composer.kt:2090)
    at androidx.compose.runtime.ComposerImpl.consume(Composer.kt:2058)
    at com.dotmystyle.android.ui.feature.auth.LoginPresenterKt.LoginPresenter(LoginPresenter.kt:133)
    at com.dotmystyle.android.ui.feature.auth.LoginViewModel.models(LoginViewModel.kt:48)
    at com.dotmystyle.android.ui.feature.auth.LoginViewModel.models(LoginViewModel.kt:41)
    at com.dotmystyle.android.ui.core.presentation.BaseViewModel$models$2$1.invoke(BaseViewModel.kt:38)
    at com.dotmystyle.android.ui.core.presentation.BaseViewModel$models$2$1.invoke(BaseViewModel.kt:37)
    at app.cash.molecule.MoleculeKt$launchMolecule$2$3.invoke(molecule.kt:164)
    at app.cash.molecule.MoleculeKt$launchMolecule$2$3.invoke(molecule.kt:163)

Any advise or workaround it?

@JakeWharton
Copy link
Member

You would have to provide it yourself at the entry point to your composable running within Molecule. Molecule is not Android-specific nor does it have to run in a location where a Context is available. If you have one that you want to make available, you can provide it like a normal composition local at the root of your composition.

@JakeWharton JakeWharton closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2023
@winterDroid
Copy link

@amitkma did you get this working? Would be great to get an example as part of this thread as probably others will run into it as well.

@JakeWharton
Copy link
Member

The docs for providing composition local values is here: https://developer.android.com/jetpack/compose/compositionlocal#providing-values. If you have a context instance you provide it as LocalContext at the root (or wherever) of your Molecule-based composition.

@winterDroid
Copy link

I tried multiple variations but none of them seem to work as expected. Can you provide an example?

@JakeWharton
Copy link
Member

I forgot the built-in one returns Unit.

You have to use your own variant that returns the inner lambda value. See https://issuetracker.google.com/issues/271871288 for both the feature request as well as code you can use.

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

3 participants