https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Suresh Maidaragi

10/30/2023, 8:07 AM
Hi Everyone, is there dependency available in Kotlin multiplatform for compose multiplatform preview
p

Pablichjenkov

10/30/2023, 8:24 AM
Yes,
s

Suresh Maidaragi

10/30/2023, 8:24 AM
Source link please
p

Pablichjenkov

10/30/2023, 8:29 AM
I swear the official get-started templates on GitHub had these but don't see it. Let me check in my repos
Check this gradle file, you need
uiTooling
and
Preview
in Android target and also in jvm target if interested in Desktop previews. https://github.com/pablichjenkov/component-toolkit/blob/master/component-toolkit/build.gradle.kts#L245 Also for Android you need this: https://github.com/pablichjenkov/component-toolkit/blob/master/component-toolkit/build.gradle.kts#L314
l

Landry Norris

10/30/2023, 2:34 PM
You can't use previews in commonMain yet. You'll have to define your previews in androidMain/jvmMain depending on which frame you want (mobile vs desktop).
p

Pablichjenkov

10/30/2023, 2:47 PM
s

Suresh Maidaragi

10/30/2023, 2:48 PM
@Pablichjenkov didn't help in commonMain We Want to have in commonMain as @Landry Norris saying it currently not available,
l

Landry Norris

10/30/2023, 2:51 PM
It's possible to typealias the jvm desktop preview using OptionalExpectation and use it in commonMain, but you'll get a desktop preview, not mobile (and you need a jvm module). People have had issues doing this with the android preview annotation. Personally, I usually have a Previews.kt file in androidMain.
s

Suresh Maidaragi

10/30/2023, 2:52 PM
Yes desktop is possible but we wanted to have both in iOS and Android using commonMain()
👍 1
p

Pablichjenkov

10/30/2023, 2:54 PM
But remember commonMain is really not a target, is basically where all your project targets intersect. In other words, commonMain doesn't have an emulator or output to render the code. It relies on real actual targets for that. The closest is desktop since it seats in the same Mac you are. Have you tried using the previews in
shared/androidMain
or
shared/jvmMain
l

Landry Norris

10/30/2023, 2:55 PM
See the following for an example of the optional expectation. You'll be able to preview Composables for android/ios, but they'll render as a desktop preview. You'll also need a jvm source set. https://github.com/arkivanov/Decompose/blob/master/sample/shared/compose/src/commonMain/kotlin/androidx/compose/desktop/ui/tooling/preview/Preview.kt
The ideal solution would be if JetBrains and Google unified the preview engines, then in commonMain, had a dropdown to select android/desktop, but that would require a lot of time that could be better spent stabilizing Compose on iOS.
1
p

Pablichjenkov

10/30/2023, 2:58 PM
Yeah that was my point, seeing the iOS preview in Android Studio is basically copying the buffer output from an iOS emulator and displaying it in AS. But the real rendering is happening in iOS target not common. So yeah it is more comfortable in the sense that you don't have to switch to Xcode but nothing else than that
l

Landry Norris

10/30/2023, 3:00 PM
Getting an iOS preview at all in AS/IJ is unfortunately likely a while away. They'd probably be much better off starting with a desktop/android toggle for previews.
2
p

Pablichjenkov

10/30/2023, 3:04 PM
In case I really need it, I am fine with embedding the composable in a swift UI preview. It requires more writing but in case you want to be completely assured how it looks. For most cases I just use the Android preview and bet 🤞for it to be the same in the other platforms 🤷🏻‍♂️
💯 2
s

Suresh Maidaragi

10/30/2023, 3:08 PM
Yes ideally Google or Jetbrain should come up with the standard solution for preview, rather than we doing tweaks or work arounds
👍 2
3 Views