How to actually use Kotlin files? #119
-
I want to have a bottom sheet to upload an image to the app through the Camera or the Gallery, I created a Kotlin file (my experience in Compose if very limited) and added it to the Skip folder alongside the Skip.yml file. However, I cannot find the way to use this file in the import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.runtime.rememberCoroutineScope
import kotlinx.coroutines.launch
@Composable
fun ImagePickerBottomSheet(cameraLauncher: ActivityResultLauncher<Unit>, galleryLauncher: ActivityResultLauncher<String>) {
val coroutineScope = rememberCoroutineScope()
val bottomSheetState = rememberBottomSheetScaffoldState(
bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
)
BottomSheetScaffold(
scaffoldState = bottomSheetState,
sheetContent = {
Column {
TextButton(onClick = { cameraLauncher.launch(null) }) {
Text("Open Camera")
}
TextButton(onClick = { galleryLauncher.launch("image/*") }) {
Text("Open Gallery")
}
}
},
floatingActionButton = {
FloatingActionButton(onClick = {
coroutineScope.launch {
if (bottomSheetState.bottomSheetState.isCollapsed) {
bottomSheetState.bottomSheetState.expand()
} else {
bottomSheetState.bottomSheetState.collapse()
}
}
}) {
Icon(Icons.Default.Add, contentDescription = "Open Image Picker")
}
},
floatingActionButtonPosition = FabPosition.Center
) {
// Your main content goes here
}
} In my View: #if SKIP
.overlay {
if viewModel.isPickerActionSheetPresented {
ComposeView { _ in
// How to call the ImagePicker.kt file here 🤔
}
}
}
#endif |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
UPDATE: I tried this: #if SKIP
.overlay {
if viewModel.isPickerActionSheetPresented {
ComposeView { _ in
ImagePickerBottomSheet(cameraLauncher = androidx.activity.compose.rememberLauncherForActivityResult(contract = androidx.activity.result.contract.ActivityResultContracts.TakePicturePreview()) { bitmap ->
// Handle the returned bitmap
}, galleryLauncher = androidx.activity.compose.rememberLauncherForActivityResult(contract = androidx.activity.result.contract.ActivityResultContracts.GetContent()) { uri ->
// Handle the returned Uri
})
}
}
}
#endif But it shows an |
Beta Was this translation helpful? Give feedback.
-
We mention at https://skip.tools/docs/platformcustomization/#calling-kotlin-api that any Kotlin embedded in Swift code needs to follow Swift syntax conventions. We'll add some concrete examples soon, but the issue causing your error is that you are passing named argument with So the equivalent swift-ified code would look something like: #if SKIP
.overlay {
if viewModel.isPickerActionSheetPresented {
ComposeView { _ in
ImagePickerBottomSheet(cameraLauncher: androidx.activity.compose.rememberLauncherForActivityResult(contract: androidx.activity.result.contract.ActivityResultContracts.TakePicturePreview()) { bitmap in
// Handle the returned bitmap
}, galleryLauncher: androidx.activity.compose.rememberLauncherForActivityResult(contract: androidx.activity.result.contract.ActivityResultContracts.GetContent()) { uri in
// Handle the returned Uri
})
}
}
}
#endif There may be other non-syntax related errors in there, but those are the ones that jump out at me. Also note that your Kotlin file should have a Let us know if you encounter other issues and we will help you work through them. |
Beta Was this translation helpful? Give feedback.
We mention at https://skip.tools/docs/platformcustomization/#calling-kotlin-api that any Kotlin embedded in Swift code needs to follow Swift syntax conventions. We'll add some concrete examples soon, but the issue causing your error is that you are passing named argument with
=
(as Kotlin expects) rather than with:
(as Swift expects), and you are passing the closure argument likebitmap ->
(as Kotlin expects), rather thanbitmap in
(as Swift expectes).So the equivalent swift-ified code would look something like: