From 756d57f77ab113230f686dea378918f056d43342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Garci=CC=81a?= Date: Wed, 11 Sep 2024 14:00:05 +0200 Subject: [PATCH] create new function to navigate on shakes which does not depends on navController #ANDROID-15102 --- README.md | 9 +++++++- .../java/com/telefonica/tweaks/Tweaks.kt | 22 +++++++++++++++---- 2 files changed, 26 insertions(+), 5 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 1d23a0b..6419242 100644 --- a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt +++ b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt @@ -90,12 +90,24 @@ 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 @@ -110,13 +122,15 @@ fun NavController.navigateToTweaksOnShake() { shakeDetector.start(sensorManager, SensorManager.SENSOR_DELAY_NORMAL) } - if (shouldNavigate) { - LaunchedEffect(shouldNavigate) { - navigate(TWEAKS_NAVIGATION_ENTRYPOINT) + LaunchedEffect(shouldNavigate) { + if (shouldNavigate) { + onShakeDetected() + shouldNavigate = false } } } + @SuppressLint("MissingPermission") private fun vibrateIfAble(context: Context) { if (ContextCompat.checkSelfPermission(