Skip to content

Commit

Permalink
create new function to navigate on shakes which does not depends on n…
Browse files Browse the repository at this point in the history
…avController #ANDROID-15102
  • Loading branch information
juangardi21 committed Sep 11, 2024
1 parent b0658bf commit 756d57f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<uses-permission android:name="android.permission.VIBRATE" />
Expand Down
22 changes: 18 additions & 4 deletions library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 756d57f

Please sign in to comment.