From dae7d42f731989025e8923ce0b72ea7716ab757e Mon Sep 17 00:00:00 2001 From: "dima.avdeev" Date: Thu, 24 Aug 2023 14:06:46 +0300 Subject: [PATCH] simplify and adapt iOS examples to Compose 1.5.0 (#3547) --- .../iosApp/ComposeInsideSwiftUIScreen.swift | 5 +- .../chat/shared/src/commonMain/kotlin/Data.kt | 1 - .../compose/demo/widgets/ui/WidgetView.kt | 49 ++++++++++++++----- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/examples/chat/iosApp/iosApp/ComposeInsideSwiftUIScreen.swift b/examples/chat/iosApp/iosApp/ComposeInsideSwiftUIScreen.swift index 11aebef9f9..826d425203 100644 --- a/examples/chat/iosApp/iosApp/ComposeInsideSwiftUIScreen.swift +++ b/examples/chat/iosApp/iosApp/ComposeInsideSwiftUIScreen.swift @@ -38,9 +38,8 @@ struct TextInputLayer: View { textFieldFocused = false textState = "" }) { - HStack { - Image(systemName: "arrow.up.circle.fill") - }.tint(Color(red: 0.671, green: 0.365, blue: 0.792)) + Image(systemName: "arrow.up.circle.fill") + .tint(Color(red: 0.671, green: 0.365, blue: 0.792)) } } }.padding(15).background(RoundedRectangle(cornerRadius: 200).fill(.white).opacity(0.95)).padding(15) diff --git a/examples/chat/shared/src/commonMain/kotlin/Data.kt b/examples/chat/shared/src/commonMain/kotlin/Data.kt index 9f5b3faaf2..78ea0f469b 100644 --- a/examples/chat/shared/src/commonMain/kotlin/Data.kt +++ b/examples/chat/shared/src/commonMain/kotlin/Data.kt @@ -37,7 +37,6 @@ object ColorProvider { if(colors.size == 0) { colors.addAll(allColors) } - println(colors.lastIndex) val idx = Random.nextInt(colors.indices) val color = colors[idx] colors.removeAt(idx) diff --git a/examples/widgets-gallery/shared/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt b/examples/widgets-gallery/shared/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt index c624bff519..07468259f5 100644 --- a/examples/widgets-gallery/shared/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt +++ b/examples/widgets-gallery/shared/src/commonMain/kotlin/org/jetbrains/compose/demo/widgets/ui/WidgetView.kt @@ -1,11 +1,16 @@ package org.jetbrains.compose.demo.widgets.ui +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState import androidx.compose.ui.Modifier +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalFocusManager import org.jetbrains.compose.demo.widgets.ui.screens.* @Composable @@ -13,18 +18,38 @@ fun WidgetsView( widgetsTypeState: MutableState, modifier: Modifier ) { - Column(modifier = modifier.verticalScroll(state = rememberScrollState())) { - @Suppress("UNUSED_VARIABLE") - val exhaustive = when (widgetsTypeState.value) { - WidgetsType.APP_BARS -> AppBars() - WidgetsType.BUTTONS -> Buttons() - WidgetsType.CHIPS -> Chips() - WidgetsType.LOADERS -> Loaders() - WidgetsType.SNACK_BARS -> SnackBars() - WidgetsType.TEXT_VIEWS -> TextViews() - WidgetsType.TEXT_INPUTS -> TextInputs() - WidgetsType.TOGGLES -> Toggles() - WidgetsType.UI_CARDS -> UICards() + ClearFocusBox { + Column(modifier = modifier.verticalScroll(state = rememberScrollState())) { + @Suppress("UNUSED_VARIABLE") + val exhaustive = when (widgetsTypeState.value) { + WidgetsType.APP_BARS -> AppBars() + WidgetsType.BUTTONS -> Buttons() + WidgetsType.CHIPS -> Chips() + WidgetsType.LOADERS -> Loaders() + WidgetsType.SNACK_BARS -> SnackBars() + WidgetsType.TEXT_VIEWS -> TextViews() + WidgetsType.TEXT_INPUTS -> TextInputs() + WidgetsType.TOGGLES -> Toggles() + WidgetsType.UI_CARDS -> UICards() + } } } } + +/** + * This wrapper need to control focus behavior on iOS to hide the keyboard. + */ +@Composable +private fun ClearFocusBox(content: @Composable () -> Unit) { + val focusManager = LocalFocusManager.current + Box( + Modifier.fillMaxSize() + .pointerInput(Unit) { + detectTapGestures { + focusManager.clearFocus(force = true) + } + }, + ) { + content() + } +}