android: hide keyboard when not needed (#4689)

* android: hide keyboard when not needed

* revert some parts

* more places

* unused

* delay

* comment
This commit is contained in:
Stanislav Dmitrenko
2024-08-14 17:59:53 +00:00
committed by GitHub
parent e9baeba31f
commit 789c762c81
3 changed files with 36 additions and 4 deletions
@@ -36,11 +36,12 @@ import chat.simplex.common.model.ChatController.appPrefs
import chat.simplex.common.model.ChatModel
import chat.simplex.common.ui.theme.CurrentColors
import chat.simplex.common.views.chat.*
import chat.simplex.common.views.helpers.SharedContent
import chat.simplex.common.views.helpers.generalGetString
import chat.simplex.common.views.helpers.*
import chat.simplex.res.MR
import dev.icerock.moko.resources.StringResource
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import java.lang.reflect.Field
import java.net.URI
@@ -84,6 +85,13 @@ actual fun PlatformTextField(
freeFocus = true
}
}
LaunchedEffect(Unit) {
snapshotFlow { ModalManager.start.modalCount.value }
.filter { it > 0 }
.collect {
freeFocus = true
}
}
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
AndroidView(modifier = Modifier, factory = {
@@ -10,7 +10,6 @@ import androidx.compose.material.TextFieldDefaults.indicatorLine
import androidx.compose.material.TextFieldDefaults.textFieldWithLabelPadding
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
@@ -25,10 +24,12 @@ import androidx.compose.ui.text.input.*
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import chat.simplex.common.platform.*
import chat.simplex.res.MR
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun SearchTextField(
modifier: Modifier,
@@ -50,6 +51,25 @@ fun SearchTextField(
keyboard?.show()
}
}
if (appPlatform.isAndroid) {
LaunchedEffect(Unit) {
val modalCountOnOpen = ModalManager.start.modalCount.value
launch {
snapshotFlow { ModalManager.start.modalCount.value }
.filter { it > modalCountOnOpen }
.collect {
keyboard?.hide()
}
}
}
KeyChangeEffect(chatModel.chatId.value) {
if (chatModel.chatId.value != null) {
// Delay is needed here because when ChatView is being opened and keyboard is hiding, bottom sheet (to choose attachment) is visible on a screen
delay(300)
keyboard?.hide()
}
}
}
DisposableEffect(Unit) {
onDispose {
@@ -100,10 +100,14 @@ fun SettingsLayout(
) {
val scope = rememberCoroutineScope()
val closeSettings: () -> Unit = { scope.launch { drawerState.close() } }
val view = LocalMultiplatformView()
if (drawerState.isOpen) {
BackHandler {
closeSettings()
}
LaunchedEffect(Unit) {
hideKeyboard(view)
}
}
val theme = CurrentColors.collectAsState()
val uriHandler = LocalUriHandler.current