https://kotlinlang.org logo
Title
k

Kensuke Sano

04/15/2023, 12:49 PM
Hi folks! I usually develop Android apps using Jetpack Compose and am currently investigating whether it can be ported to Compose for iOS. We sometimes use LocalContext in Jetpack Compose for Android, but I think that Context depends on android.content. What should we do in Compose for iOS? Should we not use Context?
a

agrosner

04/15/2023, 12:55 PM
I would ask - why do you need it? If you need resources I think compose mpp has solutions. If you need to launch android specific things, I might make abstractions for each platform around that usage rather than rely on context
I can see why original compose has support for it, but at same time it could propagate usage of that god object
k

Kensuke Sano

04/15/2023, 1:22 PM
I looked into the code and found that it was used in PSharedreference. I believe this is an Android-dependent feature, so should we consider implementing it differently?
And using in DatePicker.
MaterialDatePicker.Builder.datePicker()
                .setSelection(date?.toEpochMilli() ?: Instant.now().toEpochMilli())
                .build()
                .apply {
                    show(context.supportFragmentManager, "DatePicker")
                    addOnPositiveButtonClickListener {
                        setDate(Instant.ofEpochMilli(it))
                    }
                }
        }
d

Dima Avdeev

04/15/2023, 2:04 PM
Hello, we have a solution for this right in our main sample, called imageviewer:
on common:
expect class PlatformContext
@Composable
expect fun getPlatformContext(): PlatformContext

on Android:
actual class PlatformContext(val androidContext: Context)
@Composable
actual fun getPlatformContext(): PlatformContext = PlatformContext(LocalContext.current)
https://github.com/JetBrains/compose-multiplatform/blob/1d955a2e6da60f695aee1efb0f[…]dMain/kotlin/example/imageviewer/filter/BitmapFilter.android.kt
After that you can also create
@Composable 
expect fun PlatformDatePicker
@Composable 
actual fun PlatfromDatePicker {
    with android implementation
}
and some different one for iOS
k

Kensuke Sano

04/16/2023, 3:42 AM
Thanks! I'd like to try it later! I believe there are differences due to the OS, but are there any efforts to bridge these gaps in Kotlin Multiplatform? In other words, I'm talking about Jetpack Compose becoming independent of Android. It would be nice if DatePicker could achieve this. I understand that running SharedPreference in Jetpack Compose might be difficult since it's impossible to avoid the OS differences, but it would be nice if Kotlin Multiplatform could provide a solution.
m

michaelv

04/29/2023, 7:50 AM
@Kensuke Sano did you try it? I also noticed that it’s available now with the exception of the TimePickerDialog but there’s some code for that on Stackoverflow. However I couldn’t find a way to select dates in UI tests. Anyone know if it’s possible or do they still need to add the semantics? Talkback can read the dates just fine so I would have thought that at least something like this should work but it doesn’t. Nothing is visible in the layout inspector inside the DatePicker so I think it’s not quite ready yet. so close though. also clearly it’s in progress but the material design page just says “planned”.
composeTestRule.onNode(hasContentDescription(value = "Tuesday, 12 October 2021")).performClick()