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 { fetchApi } from "@/lib/server-api-util";
|
||||||
import { getWebSocketErrorMessage, webSocketManager, WsMessage } from "@/lib/websocket-util";
|
import { getWebSocketErrorMessage, webSocketManager, WsMessage } from "@/lib/websocket-util";
|
||||||
import { Assistant, Message } from "@/types/ask";
|
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 { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import {
|
import {
|
||||||
@ -25,6 +25,7 @@ import { runOnJS } from 'react-native-reanimated';
|
|||||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||||
|
|
||||||
export default function AskScreen() {
|
export default function AskScreen() {
|
||||||
|
const router = useRouter();
|
||||||
const insets = useSafeAreaInsets();
|
const insets = useSafeAreaInsets();
|
||||||
|
|
||||||
const chatListRef = useRef<FlatList>(null);
|
const chatListRef = useRef<FlatList>(null);
|
||||||
@ -242,6 +243,15 @@ export default function AskScreen() {
|
|||||||
}
|
}
|
||||||
}, [isHello]);
|
}, [isHello]);
|
||||||
|
|
||||||
|
// 组件卸载时清理动画
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
// 停止所有可能正在运行的动画
|
||||||
|
fadeAnim.stopAnimation();
|
||||||
|
fadeAnimChat.stopAnimation();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
if (!sessionId) {
|
if (!sessionId) {
|
||||||
@ -254,28 +264,6 @@ export default function AskScreen() {
|
|||||||
return (
|
return (
|
||||||
<GestureDetector gesture={gesture}>
|
<GestureDetector gesture={gesture}>
|
||||||
<View style={[styles.container, { paddingTop: insets.top, paddingBottom: insets.bottom }]}>
|
<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}>
|
<View style={styles.contentContainer}>
|
||||||
{/* 欢迎页面 */}
|
{/* 欢迎页面 */}
|
||||||
|
|||||||
@ -223,14 +223,36 @@ export default function HomeScreen() {
|
|||||||
|
|
||||||
// 组件挂载时启动动画
|
// 组件挂载时启动动画
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsLoading(true);
|
let isMounted = true;
|
||||||
checkAuthStatus(router, () => {
|
|
||||||
router.replace('/ask')
|
const initializeComponent = async () => {
|
||||||
}, false).then(() => {
|
try {
|
||||||
setIsLoading(false);
|
setIsLoading(true);
|
||||||
}).catch(() => {
|
|
||||||
setIsLoading(false);
|
// 添加延迟确保 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图标的淡入动画
|
// IP图标的淡入动画
|
||||||
Animated.timing(fadeAnim, {
|
Animated.timing(fadeAnim, {
|
||||||
toValue: 1,
|
toValue: 1,
|
||||||
@ -249,14 +271,29 @@ export default function HomeScreen() {
|
|||||||
startWaveAnimation();
|
startWaveAnimation();
|
||||||
});
|
});
|
||||||
|
|
||||||
// 组件卸载时清理动画
|
// 组件卸载时清理动画和状态
|
||||||
return () => {
|
return () => {
|
||||||
|
isMounted = false;
|
||||||
|
|
||||||
|
// 清理所有动画
|
||||||
if (buttonLoopAnim.current) {
|
if (buttonLoopAnim.current) {
|
||||||
buttonLoopAnim.current.stop();
|
buttonLoopAnim.current.stop();
|
||||||
}
|
}
|
||||||
if (animationRef.current) {
|
if (animationRef.current) {
|
||||||
animationRef.current.stop();
|
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',
|
fontWeight: 'bold',
|
||||||
fontSize: 18,
|
fontSize: 18,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user