From 49383b5611d2385b00658dbaf65fb7c8b9df1337 Mon Sep 17 00:00:00 2001 From: shubham Date: Thu, 18 Jan 2024 14:28:07 +0530 Subject: [PATCH] Add ContactUtils and lock version of react-native-fs --- ios/zeus.xcodeproj/project.pbxproj | 2 + package.json | 2 +- utils/ContactUtils.ts | 83 ++++++++++++++++++++++++++++ views/NostrContacts.tsx | 88 ++---------------------------- yarn.lock | 2 +- 5 files changed, 91 insertions(+), 86 deletions(-) create mode 100644 utils/ContactUtils.ts diff --git a/ios/zeus.xcodeproj/project.pbxproj b/ios/zeus.xcodeproj/project.pbxproj index cf025e61d..17ebd3d04 100644 --- a/ios/zeus.xcodeproj/project.pbxproj +++ b/ios/zeus.xcodeproj/project.pbxproj @@ -2134,6 +2134,7 @@ "-ld_classic", "-Wl", "-ld_classic", + "-Wl -ld_classic ", ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; @@ -2211,6 +2212,7 @@ "-ld_classic", "-Wl", "-ld_classic", + "-Wl -ld_classic ", ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; diff --git a/package.json b/package.json index 81c7e2a9d..25d383f50 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "react-native-draglist": "3.5.1", "react-native-elements": "3.4.3", "react-native-encrypted-storage": "4.0.3", - "react-native-fs": "^2.20.0", + "react-native-fs": "2.20.0", "react-native-gesture-handler": "2.12.1", "react-native-get-random-values": "1.9.0", "react-native-hce": "0.1.2", diff --git a/utils/ContactUtils.ts b/utils/ContactUtils.ts new file mode 100644 index 000000000..b01d2b82a --- /dev/null +++ b/utils/ContactUtils.ts @@ -0,0 +1,83 @@ +import { v4 as uuidv4 } from 'uuid'; +import { nip19 } from 'nostr-tools'; +import RNFS from 'react-native-fs'; + +const transformContactData = async (contact: any) => { + try { + const name = contact?.display_name || contact?.name || ''; + const transformedContact = { + photo: '', + name, + description: contact?.about || '', + lnAddress: contact?.lud16 + ? [contact?.lud16] + : contact?.lud06 + ? [contact?.lud06] + : contact?.lud16 && contact?.lud06 + ? [contact?.lud06, contact?.lud16] + : [], + onchainAddress: [''], + pubkey: [''], + nip05: contact?.nip05 ? [contact?.nip05] : [], + nostrNpub: contact?.npub + ? [contact?.npub] + : contact?.pubkey + ? [nip19.npubEncode(contact.pubkey)] + : [], + contactId: uuidv4(), + isFavourite: false, + banner: '', + isSelected: false + }; + + if (contact?.banner) { + console.log('Downloading banner...'); + const bannerFileName = + 'nostrContactBanner_' + transformedContact.name + '.png'; + const bannerFilePath = + RNFS.DocumentDirectoryPath + '/' + bannerFileName; + + try { + // Download the banner and save it locally + await RNFS.downloadFile({ + fromUrl: contact?.banner, + toFile: bannerFilePath + }).promise; + + console.log('Banner download successful!'); + transformedContact.banner = 'file://' + bannerFilePath; + } catch (bannerError) { + console.error('Error downloading banner:', bannerError); + } + } + + console.log('Transformed contact:', transformedContact); + + if (contact?.picture) { + console.log('Downloading image...'); + const fileName = + 'nostrContactPhoto_' + transformedContact.contactId + '.png'; + const filePath = RNFS.DocumentDirectoryPath + '/' + fileName; + + try { + // Download the image and save it locally + await RNFS.downloadFile({ + fromUrl: contact?.picture, + toFile: filePath + }).promise; + + console.log('Download successful!'); + transformedContact.photo = 'file://' + filePath; + } catch (photoError) { + console.error('Error downloading photo:', photoError); + } + } + + return transformedContact; + } catch (error) { + console.error('Error transforming contact:', error); + return null; + } +}; + +export default { transformContactData }; diff --git a/views/NostrContacts.tsx b/views/NostrContacts.tsx index 304c8d28f..9701cc7a2 100644 --- a/views/NostrContacts.tsx +++ b/views/NostrContacts.tsx @@ -9,11 +9,9 @@ import { Animated, Easing } from 'react-native'; -import { v4 as uuidv4 } from 'uuid'; import { CheckBox, Icon } from 'react-native-elements'; import EncryptedStorage from 'react-native-encrypted-storage'; import { relayInit, nip05, nip19 } from 'nostr-tools'; -import RNFS from 'react-native-fs'; import Button from '../components/Button'; import Header from '../components/Header'; @@ -24,6 +22,7 @@ import { ErrorMessage } from '../components/SuccessErrorMessage'; import { Row } from '../components/layout/Row'; import AddressUtils from '../utils/AddressUtils'; +import ContactUtils from '../utils/ContactUtils'; import { localeString } from '../utils/LocaleUtils'; import { themeColor } from '../utils/ThemeUtils'; @@ -197,86 +196,6 @@ export default class NostrContacts extends React.Component< }); } - transformContactData = async (contact: any) => { - try { - const name = contact?.display_name || contact?.name || ''; - const transformedContact = { - photo: '', - name, - description: contact?.about || '', - lnAddress: contact?.lud16 - ? [contact?.lud16] - : contact?.lud06 - ? [contact?.lud06] - : contact?.lud16 && contact?.lud06 - ? [contact?.lud06, contact?.lud16] - : [], - onchainAddress: [''], - pubkey: [''], - nip05: contact?.nip05 ? [contact?.nip05] : [], - nostrNpub: contact?.npub - ? [contact?.npub] - : contact?.pubkey - ? [nip19.npubEncode(contact.pubkey)] - : [], - contactId: uuidv4(), - isFavourite: false, - banner: '', - isSelected: false - }; - - if (contact?.banner) { - console.log('Downloading banner...'); - const bannerFileName = - 'nostrContactBanner_' + transformedContact.name + '.png'; - const bannerFilePath = - RNFS.DocumentDirectoryPath + '/' + bannerFileName; - - try { - // Download the banner and save it locally - await RNFS.downloadFile({ - fromUrl: contact?.banner, - toFile: bannerFilePath - }).promise; - - console.log('Banner download successful!'); - transformedContact.banner = 'file://' + bannerFilePath; - } catch (bannerError) { - console.error('Error downloading banner:', bannerError); - } - } - - console.log('Transformed contact:', transformedContact); - - if (contact?.picture) { - console.log('Downloading image...'); - const fileName = - 'nostrContactPhoto_' + - transformedContact.contactId + - '.png'; - const filePath = RNFS.DocumentDirectoryPath + '/' + fileName; - - try { - // Download the image and save it locally - await RNFS.downloadFile({ - fromUrl: contact?.picture, - toFile: filePath - }).promise; - - console.log('Download successful!'); - transformedContact.photo = 'file://' + filePath; - } catch (photoError) { - console.error('Error downloading photo:', photoError); - } - } - - return transformedContact; - } catch (error) { - console.error('Error transforming contact:', error); - return null; - } - }; - toggleContactSelection = (contact: any) => { this.setState((prevState) => { const selectedContacts = [...prevState.selectedContacts]; @@ -345,7 +264,8 @@ export default class NostrContacts extends React.Component< this.toggleContactSelection(item); } else { navigation.navigate('ContactDetails', { - nostrContact: await this.transformContactData(item), + nostrContact: + await ContactUtils.transformContactData(item), isNostrContact: true }); } @@ -453,7 +373,7 @@ export default class NostrContacts extends React.Component< // Transform Nostr contacts data to match the format in AddContact const transformedContactsPromises = contactsToImport.map( - (contact) => this.transformContactData(contact) + (contact) => ContactUtils.transformContactData(contact) ); const transformedContacts = await Promise.all( diff --git a/yarn.lock b/yarn.lock index 1e721d3f5..3b8071954 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8098,7 +8098,7 @@ react-native-encrypted-storage@4.0.3: resolved "https://registry.yarnpkg.com/react-native-encrypted-storage/-/react-native-encrypted-storage-4.0.3.tgz#2a4d65459870511e8f4ccd22f02433dab7fa5e91" integrity sha512-0pJA4Aj2S1PIJEbU7pN/Qvf7JIJx3hFywx+i+bLHtgK0/6Zryf1V2xVsWcrD50dfiu3jY1eN2gesQ5osGxE7jA== -react-native-fs@^2.20.0: +react-native-fs@2.20.0: version "2.20.0" resolved "https://registry.yarnpkg.com/react-native-fs/-/react-native-fs-2.20.0.tgz#05a9362b473bfc0910772c0acbb73a78dbc810f6" integrity sha512-VkTBzs7fIDUiy/XajOSNk0XazFE9l+QlMAce7lGuebZcag5CnjszB+u4BdqzwaQOdcYb5wsJIsqq4kxInIRpJQ==