notificationStorage.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import AsyncStorage from '@react-native-async-storage/async-storage';
  2. const VIEWED_NOTIFICATIONS_KEY = '@viewed_notifications';
  3. interface ViewedNotification {
  4. id: string;
  5. type: 'reservation' | 'promotion';
  6. viewedAt: string;
  7. }
  8. export const notificationStorage = {
  9. async markAsViewed(id: string, type: 'reservation' | 'promotion'): Promise<void> {
  10. try {
  11. const existing = await this.getViewedNotifications();
  12. const newViewed: ViewedNotification = {
  13. id,
  14. type,
  15. viewedAt: new Date().toISOString()
  16. };
  17. const updated = [...existing, newViewed];
  18. await AsyncStorage.setItem(VIEWED_NOTIFICATIONS_KEY, JSON.stringify(updated));
  19. } catch (error) {
  20. console.error('Error marking notification as viewed:', error);
  21. }
  22. },
  23. async getViewedNotifications(): Promise<ViewedNotification[]> {
  24. try {
  25. const viewed = await AsyncStorage.getItem(VIEWED_NOTIFICATIONS_KEY);
  26. return viewed ? JSON.parse(viewed) : [];
  27. } catch (error) {
  28. console.error('Error getting viewed notifications:', error);
  29. return [];
  30. }
  31. },
  32. async isNotificationViewed(id: string): Promise<boolean> {
  33. const viewed = await this.getViewedNotifications();
  34. return viewed.some((notification) => notification.id === id);
  35. },
  36. async seeAllCurrentStorage() {
  37. const viewed = await this.getViewedNotifications();
  38. return viewed;
  39. },
  40. async clearStorage() {
  41. await AsyncStorage.removeItem(VIEWED_NOTIFICATIONS_KEY);
  42. }
  43. };