Skip to content

Commit

Permalink
Fix a crash which happens when user shakes the phone #ANDROID-15102 (#43
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
juangardi21 authored Sep 12, 2024
1 parent 6267ff7 commit 4f89d4a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 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
36 changes: 31 additions & 5 deletions library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -171,4 +197,4 @@ fun NavGraphBuilder.addTweakGraph(
}
}

private fun TweakCategory.navigationRoute(): String = "${this.title.replace(" ", "")}-tweak-screen"
private fun TweakCategory.navigationRoute(): String = "${this.title.replace(" ", "")}-tweak-screen"

0 comments on commit 4f89d4a

Please sign in to comment.