Untitled

 avatar
unknown
plain_text
a year ago
5.0 kB
6
Indexable
import React, {useEffect, useState} from 'react';
import {
  Text,
  View,
  Image,
  Platform,
  NativeModules,
  LogBox,
  NativeEventEmitter,
} from 'react-native';
import {useNavigation} from '@react-navigation/native';
import {IconButton} from 'react-native-paper';
import {Observer} from 'mobx-react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as signalR from '@microsoft/signalr';
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';

import {styles} from './styles';
import images from '../../assets/images';
import {COLORS} from '../../theme';
import {globalStyles} from '../../utils/styles';
import icons from '../../assets/icons';
import routes from '../../navigation/routes';
import {UserStore} from '../../store/user';
import BackgroundFetch from 'react-native-background-fetch';

export const CustomHeader = ({
  image,
  title,
  arrow,
  plus,
  style,
  pushnotify,
}) => {
  const navigation = useNavigation();
  const [loading, setLoading] = useState(false);
  const [hasUnreadNotifications, setHasUnreadNotifications] = useState(false);
  const [socketConnectionReady, setSocketConnectionReady] = useState(false);
  const [notify, setNotify] = useState(false);
  const [notifyLenght, setNotifyLenght] = useState('');
  const NOTIFICATIONS_SEEN_KEY = 'notifications_seen';

  useEffect(() => {
    const fetchNotifications = async () => {
      setLoading(true);
      UserStore.getNotifications(
        async notifications => {
          setLoading(false);
          setNotifyLenght(notifications.length);
          const seen = await AsyncStorage.getItem(NOTIFICATIONS_SEEN_KEY);
          const seenNumber = seen ? Number(seen) : 0;
          if (notifications.length === seenNumber) {
            setHasUnreadNotifications(false);
          } else {
            setHasUnreadNotifications(true);
          }
        },
        setLoading,
        navigation,
      );
    };

    fetchNotifications();
  }, [navigation, notify]);

  const handleNotificationsPress = async () => {
    navigation.navigate(routes.notifications);
    setHasUnreadNotifications(false);
    await AsyncStorage.setItem(NOTIFICATIONS_SEEN_KEY, notifyLenght.toString());
  };

  useEffect(() => {
    const logEveryMinute = () => {
      console.log("Logging every minute...");
    };

   
    BackgroundFetch.configure(
      {
        minimumFetchInterval: 1,
        stopOnTerminate: false, 
        startOnBoot: true,
        requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
      },
      async (taskId) => {
        console.log("[BackgroundFetch] taskId: ", taskId);
        logEveryMinute();
        BackgroundFetch.finish(taskId);
      },
      (error) => {
        console.error("[BackgroundFetch] Failed to start", error);
      }
    );

    const eventEmitter = new NativeEventEmitter(BackgroundFetch);
    const subscription = eventEmitter.addListener(
      'fetch',
      (taskId) => {
        console.log("[BackgroundFetch] Event received: ", taskId);
        logEveryMinute();
        BackgroundFetch.finish(taskId);
      }
    );

    // Clean up subscription on unmount
    return () => {
      subscription.remove();
    };
  }, []); if (image) {
    return (
      <View style={[globalStyles.row, style, {marginTop: 10}]}>
        <View>
          <Image
            source={images.profile}
            style={{height: 52, width: 52, borderRadius: 50}}
          />
          <View style={styles.online} />
        </View>
        <Observer>
          {() => <Text style={styles.name}>{UserStore.user?.fullName}</Text>}
        </Observer>
        <IconButton
          onPress={handleNotificationsPress}
          style={styles.notification}
          icon={() => (
            <View>
              <Image
                source={icons.notification}
                style={{height: 16, width: 14, tintColor: COLORS.white}}
                resizeMode="contain"
              />
              {hasUnreadNotifications === true && <View style={styles.red} />}
            </View>
          )}
        />
      </View>
    );
  } else {
    return (
      <View style={[globalStyles.row, style, {marginTop: 10}]}>
        {arrow && (
          <IconButton
            icon={'chevron-left'}
            onPress={() => navigation.goBack()}
            iconColor={COLORS.black}
            style={{margin: 0, marginRight: 5}}
            size={30}
          />
        )}
        <Text style={globalStyles.heading}>{title}</Text>
        {plus && (
          <IconButton
            onPress={() => {}}
            style={styles.add}
            icon={'plus'}
            iconColor={COLORS.white}
          />
        )}
      </View>
    );
  }
};
Editor is loading...
Leave a Comment