Add files via upload

This commit is contained in:
Savsis 2026-05-08 03:53:51 +05:00 • committed by GitHub
parent 6050fed0d8
commit 8439481e34
9 changed files with 580 additions and 317 deletions

View file

@ -1,28 +1,34 @@
/**
* useShakeToShuffle — accelerometer-based shake detection
* Triggers callback when device is shaken hard enough
* Safely handles missing expo-sensors native module
*/
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
const SHAKE_COOLDOWN = 1500;
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();
let sub: { remove: () => void } | null = null;
try {
// Lazily import so missing native module doesn't crash on load
const { Accelerometer } = require('expo-sensors');
Accelerometer.setUpdateInterval(100);
sub = Accelerometer.addListener(({ x, y, z }: { x: number; y: number; z: number }) => {
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).catch(() => {});
onShake();
}
});
} catch {
// expo-sensors not linked — shake disabled, no crash
}
return () => { try { sub?.remove(); } catch {} };
}, [onShake]);
}