2024-10-15 15:53:05 -05:00
|
|
|
import * as SecureStore from "expo-secure-store";
|
|
|
|
import type {
|
|
|
|
Countdown,
|
|
|
|
InitialData,
|
|
|
|
Relationship,
|
|
|
|
RelationshipData,
|
|
|
|
User,
|
|
|
|
} from "@/constants/Types";
|
|
|
|
|
|
|
|
export const saveUser = async (user: User) => {
|
|
|
|
try {
|
|
|
|
await SecureStore.setItemAsync('user', JSON.stringify(user));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving user to SecureStore:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export const getUser = async () => {
|
|
|
|
try {
|
|
|
|
const user = await SecureStore.getItemAsync('user');
|
|
|
|
return user ? JSON.parse(user) as User : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error getting user from SecureStore:', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-16 16:50:26 -05:00
|
|
|
export const updateUser = async (updatedFields: Partial<any>) => {
|
|
|
|
try {
|
|
|
|
const currentUser: User = await getUser() as User;
|
|
|
|
if (!currentUser) return null;
|
|
|
|
const updatedUser: User = { ...currentUser, ...updatedFields };
|
|
|
|
await saveUser(updatedUser);
|
|
|
|
return updatedUser as User;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error updating user data: ', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-15 15:53:05 -05:00
|
|
|
export const savePartner = async (partner: User) => {
|
|
|
|
try {
|
|
|
|
await SecureStore.setItemAsync('partner', JSON.stringify(partner));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving partner to SecureStore:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export const getPartner = async () => {
|
|
|
|
try {
|
|
|
|
const partner = await SecureStore.getItemAsync('partner');
|
|
|
|
return partner ? JSON.parse(partner) as User : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error getting partner from SecureStore:', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-16 16:50:26 -05:00
|
|
|
export const updatePartner = async (updatedFields: Partial<any>) => {
|
|
|
|
try {
|
|
|
|
const currentPartner: User = await getPartner() as User;
|
|
|
|
if (!currentPartner) return null;
|
|
|
|
const updatedPartner: User = { ...currentPartner, ...updatedFields };
|
|
|
|
await saveUser(updatedPartner);
|
|
|
|
return updatedPartner as User;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error updating user data: ', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-15 15:53:05 -05:00
|
|
|
export const saveRelationship = async (relationship: Relationship) => {
|
|
|
|
try {
|
|
|
|
await SecureStore.setItemAsync('relationship', JSON.stringify(relationship));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving relationship to SecureStore:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export const getRelationship = async () => {
|
|
|
|
try {
|
|
|
|
const relationship = await SecureStore.getItemAsync('relationship');
|
|
|
|
return relationship ? JSON.parse(relationship) as Relationship : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error getting relationship from SecureStore:', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-16 16:50:26 -05:00
|
|
|
export const updateRelationship = async (updatedFields: Partial<any>) => {
|
|
|
|
try {
|
|
|
|
const currentRelationship: Relationship = await getRelationship() as Relationship;
|
|
|
|
if (!currentRelationship) return null;
|
|
|
|
const updatedRelationship: Relationship = { ...currentRelationship, ...updatedFields };
|
|
|
|
await saveRelationship(updatedRelationship);
|
|
|
|
return updatedRelationship as Relationship;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error updating relationship data: ', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-15 15:53:05 -05:00
|
|
|
export const saveCountdown = async (countdown: Countdown) => {
|
|
|
|
try {
|
|
|
|
await SecureStore.setItemAsync('countdown', JSON.stringify(countdown));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving countdown to SecureStore:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export const getCountdown = async () => {
|
|
|
|
try {
|
|
|
|
const countdown = await SecureStore.getItemAsync('countdown');
|
|
|
|
return countdown ? JSON.parse(countdown) as Countdown : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error getting countdown from SecureStore:', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-16 16:50:26 -05:00
|
|
|
export const updateCountdown = async (updatedFields: Partial<any>) => {
|
|
|
|
try {
|
|
|
|
const currentCountdown: Countdown = await getCountdown() as Countdown;
|
|
|
|
if (!currentCountdown) return null;
|
|
|
|
const updatedCountdown: Countdown = { ...currentCountdown, ...updatedFields };
|
|
|
|
await saveCountdown(updatedCountdown);
|
|
|
|
return updatedCountdown as Countdown;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error updating countdown data: ', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-15 15:53:05 -05:00
|
|
|
export const saveRelationshipData = async (relationshipData: RelationshipData) => {
|
|
|
|
try {
|
2024-10-18 12:39:54 -05:00
|
|
|
await SecureStore.setItemAsync('partner', JSON.stringify(relationshipData.partner));
|
2024-10-15 15:53:05 -05:00
|
|
|
await SecureStore.setItemAsync('relationship', JSON.stringify(relationshipData.relationship));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving partner & relationship to SecureStore:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export const saveInitialData = async (initialData: InitialData) => {
|
|
|
|
try {
|
|
|
|
await SecureStore.setItemAsync('user', JSON.stringify(initialData.user));
|
2024-10-18 12:39:54 -05:00
|
|
|
if (initialData.relationshipData) {
|
|
|
|
await SecureStore.setItemAsync(
|
|
|
|
'relationship', JSON.stringify(initialData.relationshipData.relationship)
|
|
|
|
);
|
|
|
|
await SecureStore.setItemAsync(
|
|
|
|
'partner', JSON.stringify(initialData.relationshipData.partner)
|
|
|
|
);
|
2024-10-21 10:49:20 -05:00
|
|
|
if (initialData.countdown)
|
|
|
|
await SecureStore.setItemAsync('countdown', JSON.stringify(initialData.countdown));
|
2024-10-18 12:39:54 -05:00
|
|
|
}
|
2024-10-15 15:53:05 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving initial data to SecureStore:', error);
|
|
|
|
}
|
|
|
|
};
|