diff --git a/app/contact-import.tsx b/app/contact-import.tsx index c6667f5..b72d343 100644 --- a/app/contact-import.tsx +++ b/app/contact-import.tsx @@ -8,31 +8,17 @@ import { } from '@ui-kitten/components' import { useRouter } from 'expo-router' import { StyledComponent } from 'nativewind' -import { useMemo } from 'react' import ContactImportList from '../src/components/ContactImport/ContactImportSearch' import EmptyView from '../src/components/EmptyView' import SafeAreaView from '../src/components/SafeAreaView' -import { usePalsContacts } from '../src/contexts/PalsContacts' import useDeviceContacts from '../src/hooks/useDeviceContacts' const ContactImport = () => { const deviceContacts = useDeviceContacts( 'to start adding contacts into Pals.', ) - const [palsContacts] = usePalsContacts() const router = useRouter() - // Exclude contacts that are already imported - const contactsToImport = useMemo( - () => - deviceContacts?.filter( - (deviceContact) => - !palsContacts.find( - (palsContact) => palsContact.id === deviceContact.id, - ), - ), - [deviceContacts, palsContacts], - ) return ( @@ -56,8 +42,8 @@ const ContactImport = () => { title="Contact Import" /> - {contactsToImport?.length ? ( - + {deviceContacts?.length ? ( + ) : ( )} diff --git a/app/index.tsx b/app/index.tsx index 57428aa..dd0082d 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -20,14 +20,12 @@ import { } from '../src/contexts/PalsContactsList' import { exportPalsDataToFs, importPalsDataToApp } from '../src/fsHelpers' import useLoadPalsContacts from '../src/hooks/useLoadPalsContacts' -import useSyncPalsContacts from '../src/hooks/useSyncPalsContacts' const MainScreen = () => { const [selectedPalsContactListIndex, setSelectedPalsContactListIndex] = usePalsContactsList() const [palsContacts, setPalsContacts] = usePalsContacts() useLoadPalsContacts() - useSyncPalsContacts() return ( diff --git a/src/contactsHelpers.ts b/src/contactsHelpers.ts index 3a50081..0367d10 100644 --- a/src/contactsHelpers.ts +++ b/src/contactsHelpers.ts @@ -1,5 +1,6 @@ import AsyncStorage from '@react-native-async-storage/async-storage' import * as Contacts from 'expo-contacts' +import * as Crypto from 'expo-crypto' import { Alert } from 'react-native' import { DEFAULT_ALERT_BUTTON_TEXT, PALS_CONTACTS_KEY } from './constants' @@ -60,9 +61,9 @@ export const addPalsContactToStorage = async ( selectedListName: PalsContactListName, ) => { // This is where we pick the fields we want to store - const { id, name, image } = contact + const { name, image } = contact const newPalsContact: PalsContact = { - id, + id: Crypto.randomUUID(), name, image, interactions: [], diff --git a/src/hooks/useSyncPalsContacts.ts b/src/hooks/useSyncPalsContacts.ts deleted file mode 100644 index 21d80dc..0000000 --- a/src/hooks/useSyncPalsContacts.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { useCallback, useEffect } from 'react' -import { Alert, AppState } from 'react-native' - -import { DEFAULT_ALERT_BUTTON_TEXT } from '../constants' -import { getDeviceContacts, setPalsContactsToStorage } from '../contactsHelpers' -import { usePalsContacts } from '../contexts/PalsContacts' - -const useSyncPalsContacts = () => { - const [palsContacts, setPalsContacts] = usePalsContacts() - - const syncPalsContactsData = useCallback(async () => { - try { - const deviceContacts = await getDeviceContacts({ - permissionRequestReason: 'to sync your contacts with Pals.', - }) - - setPalsContacts((prevPalsContacts) => { - let hasUpdated = false - const nextPalsContacts = prevPalsContacts.map((prevPalsContact) => { - const matchingDeviceContact = deviceContacts.find( - (deviceContact) => deviceContact.id === prevPalsContact.id, - ) - - if ( - !matchingDeviceContact || - (matchingDeviceContact.name === prevPalsContact.name && - matchingDeviceContact.image === prevPalsContact.image) - ) - return prevPalsContact - - hasUpdated = true - - // This should mirror properties used in `addPalsContactToStorage` - return { - ...prevPalsContact, - name: matchingDeviceContact.name, - image: matchingDeviceContact.image, - } - }) - - if (hasUpdated) { - // Update stored contacts in the background - setPalsContactsToStorage(nextPalsContacts) - - return nextPalsContacts - } - - return prevPalsContacts - }) - } catch { - Alert.alert( - 'Error', - 'Error syncing your Pals contact with your device contacts.', - [{ text: DEFAULT_ALERT_BUTTON_TEXT }], - { cancelable: true }, - ) - } - }, [setPalsContacts]) - - // Sync Pals contacts with device contacts - useEffect(() => { - if (palsContacts.length) { - const subscription = AppState.addEventListener( - 'change', - (nextAppState) => { - if (nextAppState === 'active') syncPalsContactsData() - }, - ) - - syncPalsContactsData() - - return subscription.remove - } - }, [palsContacts.length, syncPalsContactsData]) -} - -export default useSyncPalsContacts