fix: 修复闪退问题
This commit is contained in:
parent
5fa25e225f
commit
3bc8dda46f
@ -6,7 +6,7 @@ import { ThemedText } from "@/components/ThemedText";
|
||||
import { fetchApi } from "@/lib/server-api-util";
|
||||
import { getWebSocketErrorMessage, webSocketManager, WsMessage } from "@/lib/websocket-util";
|
||||
import { Assistant, Message } from "@/types/ask";
|
||||
import { router, useFocusEffect, useLocalSearchParams } from "expo-router";
|
||||
import { useFocusEffect, useLocalSearchParams, useRouter } from "expo-router";
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
@ -25,6 +25,7 @@ import { runOnJS } from 'react-native-reanimated';
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
export default function AskScreen() {
|
||||
const router = useRouter();
|
||||
const insets = useSafeAreaInsets();
|
||||
|
||||
const chatListRef = useRef<FlatList>(null);
|
||||
@ -242,6 +243,15 @@ export default function AskScreen() {
|
||||
}
|
||||
}, [isHello]);
|
||||
|
||||
// 组件卸载时清理动画
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
// 停止所有可能正在运行的动画
|
||||
fadeAnim.stopAnimation();
|
||||
fadeAnimChat.stopAnimation();
|
||||
};
|
||||
}, []);
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
if (!sessionId) {
|
||||
@ -254,28 +264,6 @@ export default function AskScreen() {
|
||||
return (
|
||||
<GestureDetector gesture={gesture}>
|
||||
<View style={[styles.container, { paddingTop: insets.top, paddingBottom: insets.bottom }]}>
|
||||
{/* 导航栏 */}
|
||||
<View style={[styles.navbar, isHello && styles.hiddenNavbar]}>
|
||||
<TouchableOpacity
|
||||
style={styles.backButton}
|
||||
onPress={() => {
|
||||
try {
|
||||
if (TextInput.State?.currentlyFocusedInput) {
|
||||
const input = TextInput.State.currentlyFocusedInput();
|
||||
if (input) input.blur();
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('失去焦点失败:', error);
|
||||
}
|
||||
Keyboard.dismiss();
|
||||
router.push('/memo-list');
|
||||
}}
|
||||
>
|
||||
<ReturnArrow />
|
||||
</TouchableOpacity>
|
||||
<ThemedText style={styles.title} onPress={() => { router.push('/owner') }}>MemoWake</ThemedText>
|
||||
<View style={styles.placeholder} />
|
||||
</View>
|
||||
|
||||
<View style={styles.contentContainer}>
|
||||
{/* 欢迎页面 */}
|
||||
|
||||
@ -223,14 +223,36 @@ export default function HomeScreen() {
|
||||
|
||||
// 组件挂载时启动动画
|
||||
useEffect(() => {
|
||||
setIsLoading(true);
|
||||
checkAuthStatus(router, () => {
|
||||
router.replace('/ask')
|
||||
}, false).then(() => {
|
||||
setIsLoading(false);
|
||||
}).catch(() => {
|
||||
setIsLoading(false);
|
||||
});
|
||||
let isMounted = true;
|
||||
|
||||
const initializeComponent = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
// 添加延迟确保 AuthProvider 已经初始化
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
if (!isMounted) return;
|
||||
|
||||
await checkAuthStatus(router, () => {
|
||||
if (isMounted) {
|
||||
router.replace('/ask');
|
||||
}
|
||||
}, false);
|
||||
|
||||
if (!isMounted) return;
|
||||
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error('初始化组件时出错:', error);
|
||||
if (isMounted) {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
initializeComponent();
|
||||
|
||||
// IP图标的淡入动画
|
||||
Animated.timing(fadeAnim, {
|
||||
toValue: 1,
|
||||
@ -249,14 +271,29 @@ export default function HomeScreen() {
|
||||
startWaveAnimation();
|
||||
});
|
||||
|
||||
// 组件卸载时清理动画
|
||||
// 组件卸载时清理动画和状态
|
||||
return () => {
|
||||
isMounted = false;
|
||||
|
||||
// 清理所有动画
|
||||
if (buttonLoopAnim.current) {
|
||||
buttonLoopAnim.current.stop();
|
||||
}
|
||||
if (animationRef.current) {
|
||||
animationRef.current.stop();
|
||||
}
|
||||
|
||||
// 重置所有动画值
|
||||
fadeAnim.setValue(0);
|
||||
shakeAnim.setValue(0);
|
||||
descriptionAnim.setValue(0);
|
||||
buttonAnim.setValue(0);
|
||||
buttonShakeAnim.setValue(0);
|
||||
fadeInAnim.setValue(0);
|
||||
waveAnim.setValue(0);
|
||||
|
||||
// 重置文本动画
|
||||
Object.values(textAnimations).forEach(anim => anim.setValue(0));
|
||||
};
|
||||
|
||||
}, []);
|
||||
@ -532,4 +569,4 @@ const styles = StyleSheet.create({
|
||||
fontWeight: 'bold',
|
||||
fontSize: 18,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user