https://kotlinlang.org logo
Title
a

Anastasia Rozovskaya

01/31/2022, 10:25 AM
Hello! Is there a way to provide 3d party context to Composable Preview? Details in the thread.
@Composable
fun DGisMapComposable(dGisContext: Context) { .. }

@Preview
@Composable
fun DGisMapComposablePreview() {
    // provide dGisContext somehow
}
z

Zun

01/31/2022, 12:28 PM
Never heard of dGisContext before, Google also shows 0 results. What is it and how does it differ from the regular “context: Context”?
a

Anastasia Rozovskaya

01/31/2022, 12:39 PM
it’s just a context needed for using a map api https://docs.2gis.com/en/android/sdk/overview (like Google Maps)
z

Zun

01/31/2022, 12:39 PM
LocalContext.current?
a

Anastasia Rozovskaya

01/31/2022, 12:40 PM
Yeah, I tried that, it doesn’t work in my case
z

Zun

01/31/2022, 12:41 PM
What doesn't work
a

Anastasia Rozovskaya

01/31/2022, 12:46 PM
when I put
LocalContext.current
in place of
dGisContext
inside preview I’m getting an error
z

Zun

01/31/2022, 12:47 PM
What error
a

Anastasia Rozovskaya

01/31/2022, 12:50 PM
If you are not making fun of me right now, the error is obviously in a type mismatch. Required: ru.dgis.sdk.Context, but found android.content.Context
dGisContext is an intrinsic context of this 3d party maps library, which is a wrapper of applicationContext with some additional properties
z

Zun

01/31/2022, 12:55 PM
It isn't so obvious because this is a Russian library that might not be so popular for most developers. Anyway val androidContext = LocalContext.current val sdkContext = DGis.initialize( androidContext, ApiKeys( directory = "Directory API key", map = "SDK key" ) )
You should probably create a factory method to retrieve that DGis context somewhere to prevent duplicate code
a

Anastasia Rozovskaya

01/31/2022, 12:57 PM
Ok, thank you. I’ll give it a try
Well, just to be clear. I had initialised context the way Zun proposed. And I use Hilt to inject it in my fragment, where I call the map compose function. The original question was about any 3d party context in compose preview. If anyone had a case like that, please, ping this thread. Thanks