-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
69 lines (58 loc) · 1.99 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, { useState } from "react";
import { StatusBar } from 'expo-status-bar';
import { SafeAreaView } from 'react-native';
import PersonalInfo from './components/PersonalInfo';
import Styles from './components/Styles';
import Chat from "./components/Chat";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as SplashScreen from 'expo-splash-screen';
export default function App() {
const storageUserNameKey = "chatapp-username";
const storageImageKey = "chatapp-image";
const [name, setName] = useState("");
const [image, setImage] = useState("");
const [isLoading, setIsLoading] = useState(true);
const fetchPersonalData = async () => {
let fetchedUsername = await AsyncStorage.getItem(storageUserNameKey);
let userName = fetchedUsername == null ? "" : fetchedUsername;
let fetchedImage = await AsyncStorage.getItem(storageImageKey);
let image = fetchedImage == null ? "" : fetchedImage;
setName(userName);
setImage(image);
};
AsyncStorage.clear();
const onSubmitPersonalInfo = async (name: string, image: string) => {
setName(name);
await AsyncStorage.setItem(storageUserNameKey, name);
setImage(image);
await AsyncStorage.setItem(storageImageKey, image);
}
if (isLoading) {
// Prevent auto-hiding of the splash screen
SplashScreen.preventAutoHideAsync();
// Start your data fetching process
fetchPersonalData()
.then(() => {
// Once data fetching is complete, hide the splash screen
SplashScreen.hideAsync();
setIsLoading(false);
})
.catch((error) => {
// Handle errors
console.warn(error);
});
// Return null during loading
return null;
}
let activeComponent = name !== "" ? (
<Chat userName={name} avatarImg={image} />
) : (
<PersonalInfo onClosed={onSubmitPersonalInfo} />
)
return (
<SafeAreaView style={Styles.container}>
{activeComponent}
<StatusBar style="auto" />
</SafeAreaView>
);
}