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

Add Jetpack Compose preview option #92

Closed
BioRyajenka opened this issue Nov 12, 2024 · 7 comments · Fixed by #114
Closed

Add Jetpack Compose preview option #92

BioRyajenka opened this issue Nov 12, 2024 · 7 comments · Fixed by #114
Labels
enhancement New feature or request question Further information is requested

Comments

@BioRyajenka
Copy link

Whats the best way to preview components if you use tiamat?

Since the lib encourage users to create a components like that:

val SomeComp by navDestination {
    val args = navArgs()
    val viewModel = ...
    // ... further comps ...
} 

I tried to preview a component with the following code:

@Composable
fun <NavArgs> PreviewComponent(dest: NavDestination<NavArgs>, navArgs: NavArgs? = null) {
    // ... some setup ...
    val navController = rememberNavController(
        startDestination = dest,
        startDestinationNavArgs = navArgs,
        destinations = arrayOf(dest),
    )
    Navigation(navController)
}

But it fails with
java.lang.ClassCastException: class com.android.layoutlib.bridge.android.BridgeContext cannot be cast to class android.content.ContextWrapper (com.android.layoutlib.bridge.android.BridgeContext and android.content.ContextWrapper are in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @7d9d7378)   at com.composegears.tiamat.RootDataStorageKt.rememberRootDataStore(RootDataStorage.kt:31)   at com.composegears.tiamat.Platform_androidKt.rootNavControllersStore(Platform.android.kt:12)
...

@egorikftp
Copy link
Member

Hello, I prefer to separate navDestination logic and pure Compose UI. It allows to use Preview without any restrictions.

Please take a look some example:
https://github.com/ComposeGears/Valkyrie/blob/main/idea-plugin/src/main/kotlin/io/github/composegears/valkyrie/ui/screen/intro/IntroScreen.kt

@egorikftp egorikftp added the question Further information is requested label Nov 12, 2024
@vkatz
Copy link
Contributor

vkatz commented Nov 15, 2024

@BioRyajenka
It is better to use smth like

val SomeComp by navDestination {
    val args = navArgs()
    val viewModel = ...
    val nc = navController()
    ScreenContent(
                       param1 = args.param1,
                       param2 = args.param2, 
                       onClick = viewModel::onClick,
                       onBack = {nc.back()}
                             )
} 

@Composable
fun ScreenContent(
    val param1 : Int,
    val param2: String,
    val onClick : ()->Unit,
    val onBack : ()->Unit, 
    ...
){
  // screen logic without linkage to models, args and so
   ....
}

@Preview
fun ScreenPreview(){
    ScreenContent(stub params)
}

@BioRyajenka
Copy link
Author

Yeah, I did it this way, but with complex logic/structure you also probably don't want to pass lambdas multiple levels down in many places.
Plus, some components are always expected to navigate to the same, and by using lamda you will duplicate code in all caller places.

So I ended up with a mix: extracted a function + passing demo data for the preview purposes, but also created custom dummy navController with reflection and pass it down instead of lambdas.

@vkatz
Copy link
Contributor

vkatz commented Nov 20, 2024

We will add some options to perview a screen...

Proposed syntax:

val SomeScreen by navDestination<Input>{...}

@Preview
@Composable
fun SomeScreenPreview(){
        ScreenPreview(SomeScreen, args=Input(), freeArgs = Smth, navResult = Smth)
}

@vkatz vkatz added the enhancement New feature or request label Nov 20, 2024
@vkatz vkatz changed the title Jetpack Compose preview Add Jetpack Compose preview option Nov 20, 2024
@BioRyajenka
Copy link
Author

Hey @vkatz , thanks for reply!
I think the ideal syntax would be not to introduce any syntax and allow existing rememberNavController and Navigation to work in preview mode, wdyt? Idk if thats achievable though

@vkatz
Copy link
Contributor

vkatz commented Nov 22, 2024

Taskt to do so already created, this conversation marked as enhancement and we have it on the road.

@vkatz
Copy link
Contributor

vkatz commented Dec 5, 2024

@BioRyajenka
Image
Coming next release

@egorikftp egorikftp linked a pull request Jan 14, 2025 that will close this issue
2 tasks
@vkatz vkatz closed this as completed in #114 Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants