pda_react_native_template/App.tsx

98 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-07-04 10:44:57 +08:00
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
2025-07-09 17:16:24 +08:00
import React, {useState, useEffect, useRef} from 'react';
2025-07-04 10:44:57 +08:00
import Navigation from './src/navigation';
import {ThemeProvider} from './src/contexts/ThemeContext';
2025-07-04 15:16:04 +08:00
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
2025-07-09 14:47:04 +08:00
import {View, Text, ActivityIndicator, StyleSheet} from 'react-native';
2025-07-09 17:16:24 +08:00
import {BeautifulDialog, DialogUtils} from './src/utils/DialogUtils';
2025-07-04 10:44:57 +08:00
function App(): JSX.Element {
2025-07-04 15:16:04 +08:00
const [fontLoaded, setFontLoaded] = useState(false);
const [fontError, setFontError] = useState<string | null>(null);
2025-07-09 17:16:24 +08:00
const dialogRef = useRef<any>(null);
2025-07-04 15:16:04 +08:00
useEffect(() => {
const loadFont = async () => {
try {
// 先检查字体是否已加载
const isLoaded = await MaterialIcons.hasIcon('home');
if (!isLoaded) {
console.log('Material Icons not loaded. Loading now...');
await MaterialIcons.loadFont();
}
// 确认字体是否加载成功
const fontName = MaterialIcons.getFontFamily();
console.log(`Font loaded successfully: ${fontName}`);
setFontLoaded(true);
} catch (error) {
console.error('Font loading failed:', error);
setFontError('Failed to load icon fonts');
}
};
// 加入延迟以解决模拟器启动时的问题
const timeout = setTimeout(() => {
loadFont();
}, 500);
return () => clearTimeout(timeout);
}, []);
2025-07-09 17:16:24 +08:00
// 设置弹窗引用
useEffect(() => {
if (dialogRef.current) {
DialogUtils.setDialogRef(dialogRef.current);
}
}, [fontLoaded]);
2025-07-04 15:16:04 +08:00
// 显示加载状态或错误信息
if (!fontLoaded || fontError) {
return (
<View style={styles.container}>
<ActivityIndicator size="large" />
2025-07-09 14:47:04 +08:00
<Text style={styles.statusText}>{fontError || 'Loading icons...'}</Text>
2025-07-04 15:16:04 +08:00
<Text style={styles.debugText}>
Using MaterialIcons font family: {MaterialIcons.getFontFamily()}
</Text>
</View>
);
}
2025-07-04 10:44:57 +08:00
return (
<ThemeProvider>
<Navigation />
2025-07-09 17:16:24 +08:00
<BeautifulDialog ref={dialogRef} />
2025-07-04 10:44:57 +08:00
</ThemeProvider>
);
}
2025-07-04 15:16:04 +08:00
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f5f5f5',
},
statusText: {
marginTop: 20,
fontSize: 16,
color: '#333',
},
debugText: {
marginTop: 10,
fontSize: 12,
color: '#666',
fontFamily: 'monospace',
},
});
2025-07-09 14:47:04 +08:00
export default App;