Skip to content

Commit 756d57f

Browse files
committed
create new function to navigate on shakes which does not depends on navController #ANDROID-15102
1 parent b0658bf commit 756d57f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,17 @@ addTweakGraph(
275275
```
276276

277277
## Shake gesture support:
278-
The tweaks can be opened when the user shakes the device, to do this you need to add to your navigation controller:
278+
279+
The tweaks can be opened when the user shakes the device. To achieve this, you can either add the following to your navigation controller:
279280
```kotlin
280281
navController.navigateToTweaksOnShake()
281282
```
283+
or call:
284+
```kotlin
285+
NavigateToTweaksOnShake(onOpenTweaks: () -> Unit)
286+
```
287+
and handle the navigation action yourself.
288+
282289
And also, optionally
283290
```xml
284291
<uses-permission android:name="android.permission.VIBRATE" />

library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,24 @@ open class Tweaks : TweaksContract {
9090
component.inject(reference)
9191
}
9292
}
93+
}
9394

95+
@Composable
96+
fun NavController.navigateToTweaksOnShake() {
97+
DetectShakeAndNavigate {
98+
navigate(TWEAKS_NAVIGATION_ENTRYPOINT)
99+
}
100+
}
94101

102+
@Composable
103+
fun NavigateToTweaksOnShake(onOpenTweaks: () -> Unit) {
104+
DetectShakeAndNavigate {
105+
onOpenTweaks()
106+
}
95107
}
96108

97109
@Composable
98-
fun NavController.navigateToTweaksOnShake() {
110+
private fun DetectShakeAndNavigate(onShakeDetected: () -> Unit) {
99111
val context = LocalContext.current
100112
val sensorManager: SensorManager =
101113
context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
@@ -110,13 +122,15 @@ fun NavController.navigateToTweaksOnShake() {
110122
shakeDetector.start(sensorManager, SensorManager.SENSOR_DELAY_NORMAL)
111123
}
112124

113-
if (shouldNavigate) {
114-
LaunchedEffect(shouldNavigate) {
115-
navigate(TWEAKS_NAVIGATION_ENTRYPOINT)
125+
LaunchedEffect(shouldNavigate) {
126+
if (shouldNavigate) {
127+
onShakeDetected()
128+
shouldNavigate = false
116129
}
117130
}
118131
}
119132

133+
120134
@SuppressLint("MissingPermission")
121135
private fun vibrateIfAble(context: Context) {
122136
if (ContextCompat.checkSelfPermission(

0 commit comments

Comments
 (0)