Init commit. Rewrite of old project. Not done
This commit is contained in:
95
components/services/SecureStore.tsx
Normal file
95
components/services/SecureStore.tsx
Normal file
@ -0,0 +1,95 @@
|
||||
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;
|
||||
}
|
||||
};
|
||||
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;
|
||||
}
|
||||
};
|
||||
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;
|
||||
}
|
||||
};
|
||||
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;
|
||||
}
|
||||
};
|
||||
export const saveRelationshipData = async (relationshipData: RelationshipData) => {
|
||||
try {
|
||||
await SecureStore.setItemAsync('partner', JSON.stringify(relationshipData.Partner));
|
||||
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));
|
||||
await SecureStore.setItemAsync(
|
||||
'relationship', JSON.stringify(initialData.relationshipData.relationship)
|
||||
);
|
||||
await SecureStore.setItemAsync(
|
||||
'partner', JSON.stringify(initialData.relationshipData.Partner)
|
||||
);
|
||||
await SecureStore.setItemAsync('countdown', JSON.stringify(initialData.countdown));
|
||||
} catch (error) {
|
||||
console.error('Error saving initial data to SecureStore:', error);
|
||||
}
|
||||
};
|
107
components/services/notifications/PushNotificationManager.tsx
Normal file
107
components/services/notifications/PushNotificationManager.tsx
Normal file
@ -0,0 +1,107 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import { Platform } from 'react-native';
|
||||
import * as Device from 'expo-device';
|
||||
import * as Notifications from 'expo-notifications';
|
||||
import Constants from 'expo-constants';
|
||||
import type { NotificationMessage } from '@/constants/Types';
|
||||
|
||||
Notifications.setNotificationHandler({
|
||||
handleNotification: async () => ({
|
||||
shouldShowAlert: true,
|
||||
shouldPlaySound: true,
|
||||
shouldSetBadge: true,
|
||||
}),
|
||||
});
|
||||
|
||||
export const sendPushNotification = async(expoPushToken: string, notification: NotificationMessage) => {
|
||||
const message = {
|
||||
to: expoPushToken,
|
||||
sound: notification.sound ?? 'default',
|
||||
title: notification.title,
|
||||
body: notification.body,
|
||||
data: notification.data ?? {},
|
||||
};
|
||||
await fetch('https://exp.host/--/api/v2/push/send', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Accept-encoding': 'gzip, deflate',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(message),
|
||||
});
|
||||
};
|
||||
|
||||
const handleRegistrationError = (errorMessage: string) => {
|
||||
alert(errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
};
|
||||
|
||||
const registerforPushNotificationsAsync = async () => {
|
||||
let token;
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
await Notifications.setNotificationChannelAsync('default', {
|
||||
name: 'default',
|
||||
importance: Notifications.AndroidImportance.MAX,
|
||||
vibrationPattern: [0, 250, 250, 250],
|
||||
lightColor: '#FF231F7C',
|
||||
});
|
||||
}
|
||||
|
||||
if (Device.isDevice) {
|
||||
const { status: existingStatus } = await Notifications.getPermissionsAsync();
|
||||
let finalStatus = existingStatus;
|
||||
if (existingStatus !== 'granted') {
|
||||
const { status } = await Notifications.requestPermissionsAsync();
|
||||
finalStatus = status;
|
||||
}
|
||||
if (finalStatus !== 'granted') {
|
||||
alert('Failed to get push token for push notification!');
|
||||
return;
|
||||
}
|
||||
const projectId = Constants.expoConfig?.extra?.eas?.projectId;
|
||||
if (!projectId) {
|
||||
alert('Project ID not found in eas.json');
|
||||
return;
|
||||
}
|
||||
token = (await Notifications.getExpoPushTokenAsync({ projectId })).data;
|
||||
} else {
|
||||
alert('Must use physical device for Push Notifications');
|
||||
}
|
||||
return token;
|
||||
};
|
||||
|
||||
export const PushNotificationManager = ({children}: {children: React.ReactNode}) => {
|
||||
const [expoPushToken, setExpoPushToken] = useState('');
|
||||
const [notification, setNotification] =
|
||||
useState<Notifications.Notification | undefined>(undefined);
|
||||
const notificationListener = useRef<Notifications.Subscription>();
|
||||
const responseListener = useRef<Notifications.Subscription>();
|
||||
|
||||
useEffect(() => {
|
||||
registerforPushNotificationsAsync()
|
||||
.then(token => setExpoPushToken(token ?? ''))
|
||||
.catch((error: any) => {
|
||||
setExpoPushToken('');
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
notificationListener.current = Notifications.addNotificationReceivedListener(
|
||||
notification => {
|
||||
setNotification(notification);
|
||||
});
|
||||
responseListener.current = Notifications.addNotificationResponseReceivedListener(
|
||||
response => {
|
||||
console.log(response);
|
||||
// Handle notification response here
|
||||
});
|
||||
return () => {
|
||||
notificationListener.current &&
|
||||
Notifications.removeNotificationSubscription(notificationListener.current);
|
||||
responseListener.current &&
|
||||
Notifications.removeNotificationSubscription(responseListener.current);
|
||||
};
|
||||
}, []);
|
||||
return ( <> {children} </> );
|
||||
};
|
Reference in New Issue
Block a user