Skip to content

Commit

Permalink
Replace LaunchedEffect with DisposableEffect and LifecycleObserver to…
Browse files Browse the repository at this point in the history
… check isKeyboardEnabled
  • Loading branch information
bingzheung committed Sep 1, 2024
1 parent 9172190 commit 4da6e7d
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions app/src/main/java/org/jyutping/jyutping/mainapp/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.NavHostController
import org.jyutping.jyutping.R
import org.jyutping.jyutping.extensions.convertedS2T
Expand Down Expand Up @@ -80,9 +83,18 @@ fun HomeScreen(navController: NavHostController) {
return helper.gwongWanMatch(traditionalChar)
}
val isKeyboardEnabled: MutableState<Boolean> = remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
val manager = navController.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
isKeyboardEnabled.value = manager.enabledInputMethodList.any { it.packageName == PresetConstant.KEYBOARD_ID }
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
val manager = navController.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
isKeyboardEnabled.value = manager.enabledInputMethodList.any { it.packageName == PresetConstant.KEYBOARD_ID }
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
SelectionContainer {
LazyColumn(
Expand Down

0 comments on commit 4da6e7d

Please sign in to comment.