Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash which happens when user shakes the phone #ANDROID-15102 #43

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set this as default value for onOpenTweaks and leave only one method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I can, because navigate is callable only within the NavController scope

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see, one method depends on the NavController and the other one doesn't

}
}

@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
Loading