chahinebrini ac605dce33 feat(onboarding,diga): TTS auto-play preference + 90 more DiGA test codes
## TTS Auto-Play Preference

User-Request: wenn Voice einmal aktiviert, soll Lyra auf jeder Slide
automatisch sprechen — nicht jede Slide extra antippen.

- stores/lyraVoice.ts: zustand-store mit AsyncStorage-Persistence
  (@rebreak/lyraVoiceEnabled). Default OFF.
- LyraBubble auto-plays on text-change wenn enabled
- Audio-Button toggled die Preference + stoppt current playback
- Visuell: Button ist orange-filled wenn voice ON, ghost-bordered wenn OFF
- Icon: volume-mute-outline (OFF) / volume-medium / hourglass / stop
- Cleanup beim Unmount (stopLyraSpeech) + bei text-change

Initialisiert via init() in app/_layout.tsx (analog language/theme/appLock).

Locale-keys: audio_play → "Stimme einschalten", neu audio_disable → "Stimme
ausschalten" in 4 Sprachen.

## DiGA Test Codes 011-100

Aktuell 10 Codes (REBREAK-TEST-001..010), aber 100 Android-Tester kommen
morgen onboarding. Migration 20260518_extend_diga_test_codes seeded 90
zusätzliche Codes via generate_series(11, 100) + LPAD-Padding.

- Label: 'test_batch_2026-05-android' für Auditbarkeit (vs '...2026-05'
  für die ersten 10)
- grants_plan: 'legend' wie die ersten 10
- ON CONFLICT DO NOTHING — idempotent

Distribution-Pattern: Tester N kriegt Code REBREAK-TEST-<NNN-padded>.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 22:39:18 +02:00

175 lines
5.3 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { Animated, Easing, Text, TouchableOpacity, View } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import i18n from '../../lib/i18n';
import { useTranslation } from 'react-i18next';
import { RiveAvatar, type Emotion } from '../RiveAvatar';
import { useColors } from '../../lib/theme';
import {
playLyraSpeech,
stopLyraSpeech,
type LyraSpeechStatus,
} from '../../lib/lyraSpeech';
import { useLyraVoiceStore } from '../../stores/lyraVoice';
/**
* Lyra-Mascot (animiertes Rive-Avatar) + Speech-Bubble + TTS-Audio-Button.
*
* Auto-Play-Pattern (User-Preference, persistent via useLyraVoiceStore):
* - Voice OFF (default): kein Auto-Play, Audio-Button-Tap aktiviert Voice
* + spielt aktuellen Text.
* - Voice ON: Bubble spielt sich auf jeder Slide automatisch ab beim
* Text-Change. Audio-Button-Tap stoppt aktuelles Playback + disabled
* die Preference (= kein Auto-Play mehr).
*
* Cleanup beim Unmount + bei text-change (Slide-Switch → vorheriger Sound
* wird abgebrochen).
*/
export function LyraBubble({
text,
emotion = 'idle',
}: {
text: string;
emotion?: Emotion;
}) {
const { t } = useTranslation();
const colors = useColors();
const opacity = useRef(new Animated.Value(0)).current;
const translateX = useRef(new Animated.Value(-12)).current;
const [speech, setSpeech] = useState<LyraSpeechStatus>('idle');
const voiceEnabled = useLyraVoiceStore((s) => s.enabled);
const voiceReady = useLyraVoiceStore((s) => s.ready);
const toggleVoice = useLyraVoiceStore((s) => s.toggle);
// Fade-in beim Mount + text-change
useEffect(() => {
opacity.setValue(0);
translateX.setValue(-12);
Animated.parallel([
Animated.timing(opacity, {
toValue: 1,
duration: 400,
useNativeDriver: true,
easing: Easing.out(Easing.cubic),
}),
Animated.spring(translateX, {
toValue: 0,
useNativeDriver: true,
friction: 7,
tension: 80,
}),
]).start();
}, [text, opacity, translateX]);
// Auto-Play wenn voice enabled + Bubble-Text wechselt (= neue Slide).
// Stoppt vorheriges Playback automatisch via stopLyraSpeech() in playLyraSpeech.
useEffect(() => {
if (!voiceReady) return; // warte bis AsyncStorage geladen
if (!voiceEnabled) {
// Falls voice grad disabled wurde während etwas spielte: stoppen.
setSpeech('idle');
void stopLyraSpeech();
return;
}
void playLyraSpeech(text, i18n.language || 'de', setSpeech);
}, [text, voiceEnabled, voiceReady]);
// Cleanup beim Unmount — sonst spielt der Sound weiter wenn /onboarding entladen wird
useEffect(() => {
return () => {
void stopLyraSpeech();
};
}, []);
async function togglePlayback() {
// Wenn voice OFF: enable (auto-trigger Auto-Play-Effect oben)
// Wenn voice ON: disable + stop current
if (voiceEnabled) {
setSpeech('idle');
await stopLyraSpeech();
}
await toggleVoice();
// Wenn jetzt enabled: useEffect oben startet das playback automatisch
}
const a11yLabel = voiceEnabled
? speech === 'playing'
? t('onboarding.lyra.audio_stop')
: t('onboarding.lyra.audio_disable')
: t('onboarding.lyra.audio_play');
const iconName: keyof typeof Ionicons.glyphMap = !voiceEnabled
? 'volume-mute-outline'
: speech === 'playing'
? 'stop'
: speech === 'loading'
? 'hourglass'
: 'volume-medium';
return (
<View style={{ flexDirection: 'row', alignItems: 'flex-start', gap: 12 }}>
<View style={{ marginTop: 4 }}>
<RiveAvatar emotion={emotion} size="md" />
</View>
<Animated.View
style={{
flex: 1,
opacity,
transform: [{ translateX }],
}}
>
<View
style={{
backgroundColor: colors.surfaceElevated,
borderRadius: 16,
paddingVertical: 14,
paddingLeft: 16,
paddingRight: 50, // Platz für Audio-Button rechts
position: 'relative',
}}
>
<Text
style={{
fontFamily: 'Nunito_600SemiBold',
fontSize: 16,
lineHeight: 23,
color: colors.text,
}}
>
{text}
</Text>
{/* Audio-Toggle rechts oben — bestimmt globale Voice-Preference */}
<TouchableOpacity
onPress={togglePlayback}
hitSlop={8}
activeOpacity={0.7}
accessibilityLabel={a11yLabel}
accessibilityRole="button"
style={{
position: 'absolute',
top: 8,
right: 8,
width: 34,
height: 34,
borderRadius: 17,
backgroundColor: voiceEnabled ? colors.brandOrange : colors.surface,
borderWidth: voiceEnabled ? 0 : 1,
borderColor: colors.border,
alignItems: 'center',
justifyContent: 'center',
}}
>
<Ionicons
name={iconName}
size={16}
color={voiceEnabled ? '#ffffff' : colors.textMuted}
/>
</TouchableOpacity>
</View>
</Animated.View>
</View>
);
}