Add files via upload
This commit is contained in:
parent
235427ce97
commit
73ed266994
21 changed files with 17879 additions and 1046 deletions
28
hooks/useShakeToShuffle.ts
Normal file
28
hooks/useShakeToShuffle.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* useShakeToShuffle — accelerometer-based shake detection
|
||||
* Triggers callback when device is shaken hard enough
|
||||
*/
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { Accelerometer } from 'expo-sensors';
|
||||
import * as Haptics from 'expo-haptics';
|
||||
|
||||
const SHAKE_THRESHOLD = 2.2;
|
||||
const SHAKE_COOLDOWN = 1500; // ms
|
||||
|
||||
export function useShakeToShuffle(onShake: () => void) {
|
||||
const lastShake = useRef(0);
|
||||
|
||||
useEffect(() => {
|
||||
Accelerometer.setUpdateInterval(100);
|
||||
const sub = Accelerometer.addListener(({ x, y, z }) => {
|
||||
const acc = Math.sqrt(x * x + y * y + z * z);
|
||||
const now = Date.now();
|
||||
if (acc > SHAKE_THRESHOLD && now - lastShake.current > SHAKE_COOLDOWN) {
|
||||
lastShake.current = now;
|
||||
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
|
||||
onShake();
|
||||
}
|
||||
});
|
||||
return () => sub.remove();
|
||||
}, [onShake]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue