Fixed themes not saving. Removed unused import

This commit is contained in:
whysman 2025-02-26 11:23:00 -05:00
parent f03a268a39
commit 144b0daaa5
3 changed files with 11 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState, useContext } from 'react'; import React, { useEffect, useState } from 'react';
import { Platform, View, TouchableOpacity, StyleSheet } from "react-native"; import { Platform, View, TouchableOpacity, StyleSheet } from "react-native";
import { Button, TextInput, Dialog, Portal, Avatar, useTheme, Text } from "react-native-paper"; import { Button, TextInput, Dialog, Portal, Avatar, useTheme, Text } from "react-native-paper";
import { Asset } from 'expo-asset'; import { Asset } from 'expo-asset';
@ -16,10 +16,11 @@ interface ProfileScreenProps {
setImage: (image: string) => void; setImage: (image: string) => void;
setChanged: (dataChanged: boolean) => void; setChanged: (dataChanged: boolean) => void;
setTheme: (theme: string) => void; setTheme: (theme: string) => void;
currentTheme: string;
onClose: () => void; onClose: () => void;
} }
const ProfileScreen: React.FC<ProfileScreenProps> = ({ visible, name, setName, image, setImage, setChanged, setTheme, onClose }) => { const ProfileScreen: React.FC<ProfileScreenProps> = ({ visible, name, setName, image, setImage, setChanged, currentTheme, setTheme, onClose }) => {
const theme = useTheme(); const theme = useTheme();
const isNameEmpty = !name.trim(); const isNameEmpty = !name.trim();
const themeColors = ['red', 'blue', 'yellow', 'green', 'orange', 'purple']; const themeColors = ['red', 'blue', 'yellow', 'green', 'orange', 'purple'];
@ -27,11 +28,13 @@ const ProfileScreen: React.FC<ProfileScreenProps> = ({ visible, name, setName, i
// Track the initial values when the component first mounts // Track the initial values when the component first mounts
const [initialName, setInitialName] = useState(name); const [initialName, setInitialName] = useState(name);
const [initialImage, setInitialImage] = useState(image); const [initialImage, setInitialImage] = useState(image);
const [initialTheme, setInitialTheme] = useState(currentTheme);
useEffect(() => { useEffect(() => {
if (visible) { if (visible) {
setInitialName(name); // Store initial name when the profile opens setInitialName(name); // Store initial name when the profile opens
setInitialImage(image); // Store initial image when the profile opens setInitialImage(image);
setInitialTheme(currentTheme)// Store initial image when the profile opens
} }
}, [visible]); // Reset when the dialog is opened }, [visible]); // Reset when the dialog is opened
@ -82,7 +85,7 @@ const ProfileScreen: React.FC<ProfileScreenProps> = ({ visible, name, setName, i
const handleSave = () => { const handleSave = () => {
// Check if the name or image has changed // Check if the name or image has changed
const hasChanged = name !== initialName || image !== initialImage; const hasChanged = name !== initialName || image !== initialImage || currentTheme !== initialTheme;
if (hasChanged) { if (hasChanged) {
setChanged(true); setChanged(true);
} }

View File

@ -21,6 +21,7 @@ const Index = () => {
setUserStatus, setUserStatus,
setUserDataChanged, setUserDataChanged,
setTheme, setTheme,
currentTheme,
isLoading, isLoading,
} = useUser(); } = useUser();
@ -54,6 +55,7 @@ const Index = () => {
image={userImage} image={userImage}
setImage={setUserImage} setImage={setUserImage}
setTheme={setTheme} setTheme={setTheme}
currentTheme={currentTheme}
setChanged={setUserDataChanged} setChanged={setUserDataChanged}
onClose={() => setProfileActive(false)} onClose={() => setProfileActive(false)}
/> />

View File

@ -49,6 +49,7 @@ export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
const storedUserName = await AsyncStorage.getItem("userName"); const storedUserName = await AsyncStorage.getItem("userName");
const storedUserImage = await AsyncStorage.getItem("userImage"); const storedUserImage = await AsyncStorage.getItem("userImage");
const storedUserTheme = await AsyncStorage.getItem("theme"); const storedUserTheme = await AsyncStorage.getItem("theme");
console.log("Stored theme: ", storedUserTheme);
if (storedUserId) { if (storedUserId) {
setUserId(storedUserId); setUserId(storedUserId);
setUserName(storedUserName || ""); setUserName(storedUserName || "");
@ -81,6 +82,7 @@ export const UserProvider: React.FC<UserProviderProps> = ({ children }) => {
await AsyncStorage.setItem("userName", userName); await AsyncStorage.setItem("userName", userName);
await AsyncStorage.setItem("userImage", userImage); await AsyncStorage.setItem("userImage", userImage);
await AsyncStorage.setItem("theme", currentTheme); await AsyncStorage.setItem("theme", currentTheme);
console.log("Current theme: ", currentTheme);
setUserDataChanged(false); setUserDataChanged(false);
} catch (error) { } catch (error) {
console.error("Error saving user data:", error); console.error("Error saving user data:", error);