From 4f89d4a98950348ba4c48e1698d928a0efd4c987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garc=C3=ADa?= Date: Thu, 12 Sep 2024 16:22:02 +0200 Subject: [PATCH] Fix a crash which happens when user shakes the phone #ANDROID-15102 (#43) * replace launchedEffect by DisposableEffect #ANDROID-15102 * undo last commit, fix crash #ANDROID-15102 * create new function to navigate on shakes which does not depends on navController #ANDROID-15102 --- README.md | 9 ++++- .../java/com/telefonica/tweaks/Tweaks.kt | 36 ++++++++++++++++--- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e50e109..19e7b28 100644 --- a/README.md +++ b/README.md @@ -275,10 +275,17 @@ addTweakGraph( ``` ## Shake gesture support: -The tweaks can be opened when the user shakes the device, to do this you need to add to your navigation controller: + +The tweaks can be opened when the user shakes the device. To achieve this, you can either add the following to your navigation controller: ```kotlin navController.navigateToTweaksOnShake() ``` +or call: +```kotlin +NavigateToTweaksOnShake(onOpenTweaks: () -> Unit) +``` +and handle the navigation action yourself. + And also, optionally ```xml diff --git a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt index 6ea00b8..6419242 100644 --- a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt +++ b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt @@ -5,12 +5,15 @@ import android.annotation.SuppressLint import android.content.Context import android.content.pm.PackageManager import android.hardware.SensorManager -import android.hardware.SensorManager.SENSOR_DELAY_NORMAL import android.os.Build import android.os.VibrationEffect import android.os.Vibrator import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.core.content.ContextCompat import androidx.navigation.NavController @@ -87,24 +90,47 @@ open class Tweaks : TweaksContract { component.inject(reference) } } +} +@Composable +fun NavController.navigateToTweaksOnShake() { + DetectShakeAndNavigate { + navigate(TWEAKS_NAVIGATION_ENTRYPOINT) + } +} +@Composable +fun NavigateToTweaksOnShake(onOpenTweaks: () -> Unit) { + DetectShakeAndNavigate { + onOpenTweaks() + } } @Composable -fun NavController.navigateToTweaksOnShake() { +private fun DetectShakeAndNavigate(onShakeDetected: () -> Unit) { val context = LocalContext.current val sensorManager: SensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager + + var shouldNavigate by rememberSaveable { mutableStateOf(false) } + LaunchedEffect(true) { val shakeDetector = ShakeDetector { vibrateIfAble(context) - navigate(TWEAKS_NAVIGATION_ENTRYPOINT) + shouldNavigate = true + } + shakeDetector.start(sensorManager, SensorManager.SENSOR_DELAY_NORMAL) + } + + LaunchedEffect(shouldNavigate) { + if (shouldNavigate) { + onShakeDetected() + shouldNavigate = false } - shakeDetector.start(sensorManager, SENSOR_DELAY_NORMAL) } } + @SuppressLint("MissingPermission") private fun vibrateIfAble(context: Context) { if (ContextCompat.checkSelfPermission( @@ -171,4 +197,4 @@ fun NavGraphBuilder.addTweakGraph( } } -private fun TweakCategory.navigationRoute(): String = "${this.title.replace(" ", "")}-tweak-screen" \ No newline at end of file +private fun TweakCategory.navigationRoute(): String = "${this.title.replace(" ", "")}-tweak-screen"