Skip to content

Commit

Permalink
Add ContactUtils and lock version of react-native-fs
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamkmr04 committed Jan 18, 2024
1 parent 83d2797 commit 49383b5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 86 deletions.
2 changes: 2 additions & 0 deletions ios/zeus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,7 @@
"-ld_classic",
"-Wl",
"-ld_classic",
"-Wl -ld_classic ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -2211,6 +2212,7 @@
"-ld_classic",
"-Wl",
"-ld_classic",
"-Wl -ld_classic ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
83 changes: 83 additions & 0 deletions utils/ContactUtils.ts
Original file line number Diff line number Diff line change
@@ -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 };
88 changes: 4 additions & 84 deletions views/NostrContacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
});
}
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 49383b5

Please sign in to comment.