pogdark-app/app/index.tsx

152 lines
5.4 KiB
TypeScript
Raw Normal View History

2025-02-19 18:33:09 +00:00
import React, { useEffect, useState } from 'react';
import {View, StyleSheet, Text, AppState } from "react-native";
2025-02-19 18:33:09 +00:00
import { useTheme } from "react-native-paper";
import { v4 as uuidv4 } from "uuid";
import AsyncStorage from "@react-native-async-storage/async-storage";
import ProfileScreen from "@/app/ProfileScreen";
import StatusPage from "@/app/StatusPage";
import Nav from "@/app/Nav";
import axios from "axios";
export const API_URL = process.env.EXPO_PUBLIC_API_URL;
2025-02-19 18:33:09 +00:00
const Index = () => {
const { colors } = useTheme();
const [isProfileActive, setProfileActive] = useState(false);
2025-02-22 02:56:54 +00:00
const [userId, setUserId] = useState("");
2025-02-19 18:33:09 +00:00
const [userName, setUserName] = useState("");
const [userImage, setUserImage] = useState("");
const [userStatus, setUserStatus] = useState("none");
2025-02-22 02:56:54 +00:00
const [userDataChanged, setUserDataChanged] = useState(false);
const [isLoading, setIsLoading] = useState(true); // New loading state
const [appState, setAppState] = useState(AppState.currentState);
2025-02-19 18:33:09 +00:00
useEffect(() => {
const loadUserData = async () => {
try {
const storedUserId = await AsyncStorage.getItem("userId");
const storedUserName = await AsyncStorage.getItem("userName");
const storedUserImage = await AsyncStorage.getItem("userImage");
console.log("User Id: ", storedUserId);
2025-02-22 02:56:54 +00:00
if (storedUserId) {
2025-02-19 18:33:09 +00:00
setUserId(storedUserId || uuidv4());
setUserName(storedUserName || "");
setUserImage(storedUserImage || "");
setProfileActive(false);
2025-02-22 02:56:54 +00:00
} else {
2025-02-19 18:33:09 +00:00
setUserId(uuidv4());
setUserName("");
setUserImage("");
setProfileActive(true);
}
2025-02-22 02:56:54 +00:00
console.log("Loading data ", userId);
2025-02-19 18:33:09 +00:00
} catch (error) {
console.error("Error loading user data:", error);
2025-02-22 02:56:54 +00:00
} finally {
setIsLoading(false); // Mark loading as complete
2025-02-19 18:33:09 +00:00
}
};
loadUserData();
}, []);
useEffect(() => {
2025-02-22 02:56:54 +00:00
if (!userDataChanged) return;
2025-02-19 18:33:09 +00:00
const saveUserData = async () => {
try {
2025-02-22 02:56:54 +00:00
console.log("Saving data ", userId);
2025-02-19 18:33:09 +00:00
await AsyncStorage.setItem("userId", userId);
await AsyncStorage.setItem("userName", userName);
await AsyncStorage.setItem("userImage", userImage);
2025-02-22 02:56:54 +00:00
setUserDataChanged(false);
2025-02-19 18:33:09 +00:00
} catch (error) {
console.error("Error saving user data:", error);
}
};
saveUserData();
2025-02-22 02:56:54 +00:00
}, [userDataChanged]);
useEffect(() => {
const handleAppStateChange = (nextAppState: string) => {
if (appState.match(/inactive|background/) && nextAppState === "active") {
// When the app comes to the foreground, fetch the status
if (!isLoading) {
fetchCurrentStatus().then(r => null);
} else {
console.log("Waiting for loading to complete before fetching status...");
}
}
setAppState(AppState.currentState);
};
const listener = AppState.addEventListener("change", handleAppStateChange);
// Cleanup listener on unmount
return () => {
listener.remove();
};
}, [appState]);
const fetchCurrentStatus = async () => {
try {
const response = await axios.post(API_URL + "/get", { id: userId });
console.log("response: ", response);
if (response.data?.status) {
setTimeout(() => {
setUserStatus("none"); // Reset status
}, 0)
}
} catch (error) {
console.error("Error fetching status:", error);
}
};
2025-02-22 02:56:54 +00:00
if (isLoading) {
console.log("Still loading");
return (
<View style={[styles.container, { backgroundColor: colors.background, justifyContent: 'center', alignItems: 'center' }]}>
<Text>Loading...</Text>
</View>
);
}
2025-02-19 18:33:09 +00:00
return (
2025-02-22 02:56:54 +00:00
<View style={[styles.container, { backgroundColor: colors.background }]}>
<Nav
2025-02-19 18:33:09 +00:00
toggleProfile={() => setProfileActive(true)}
/>
<StatusPage
2025-02-19 18:33:09 +00:00
id={userId}
name={userName}
image={userImage}
currentStatus={userStatus}
setStatus={setUserStatus}
2025-02-19 18:33:09 +00:00
isProfileActive={isProfileActive}
/>
<ProfileScreen
visible={isProfileActive}
id={userId}
name={userName}
setName={setUserName}
image={userImage}
setImage={setUserImage}
2025-02-22 02:56:54 +00:00
setChanged={setUserDataChanged}
2025-02-19 18:33:09 +00:00
onClose={() => setProfileActive(false)}
/>
</View>
);
};
const styles = StyleSheet.create({
container: { flex: 1, alignItems: "stretch" },
imageBackground: {
position: "absolute", // Allows child elements to layer on top
width: "100%", // Ensure full coverage of the column
height: "100%", // Fully stretches to column height
resizeMode: "cover", // Ensures it fits well
opacity: 0.3, // Fades the image
},
2025-02-19 18:33:09 +00:00
});
export default Index;