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(