Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNFS: account for changing DocumentDirectoryPath #1961

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions models/Contact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { computed } from 'mobx';
import BaseModel from './BaseModel';
import RNFS from 'react-native-fs';

export default class Contact extends BaseModel {
id: string; // deprecated
Expand Down Expand Up @@ -83,4 +84,20 @@ export default class Contact extends BaseModel {
@computed public get hasNpub(): boolean {
return this.nostrNpub?.length > 0 && this.nostrNpub[0] !== '';
}

@computed public get getPhoto(): string {
if (this.photo?.includes('rnfs://')) {
const fileName = this.photo.replace('rnfs://', '');
return `file://${RNFS.DocumentDirectoryPath}/${fileName}`;
}
return this.photo || '';
}

@computed public get getBanner(): string {
if (this.banner?.includes('rnfs://')) {
const fileName = this.banner.replace('rnfs://', '');
return `file://${RNFS.DocumentDirectoryPath}/${fileName}`;
}
return this.banner || '';
}
}
8 changes: 4 additions & 4 deletions utils/ContactUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ const transformContactData = async (contact: any) => {
}).promise;

console.log('Banner download successful!');
transformedContact.banner = 'file://' + bannerFilePath;
transformedContact.banner = 'rnfs://' + bannerFileName;
} catch (bannerError) {
console.error('Error downloading banner:', bannerError);
}
}

console.log('Transformed contact:', transformedContact);

if (contact?.picture) {
console.log('Downloading image...');
const fileName =
Expand All @@ -67,12 +65,14 @@ const transformContactData = async (contact: any) => {
}).promise;

console.log('Download successful!');
transformedContact.photo = 'file://' + filePath;
transformedContact.photo = 'rnfs://' + fileName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should also do such changes in selectPhoto of AddContact.tsx ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call 2e5bebf

} catch (photoError) {
console.error('Error downloading photo:', photoError);
}
}

console.log('Transformed contact:', transformedContact);

return transformedContact;
} catch (error) {
console.error('Error transforming contact:', error);
Expand Down
4 changes: 2 additions & 2 deletions views/ContactDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default class ContactDetails extends React.Component<
>
{contact.banner && (
<Image
source={{ uri: contact.banner }}
source={{ uri: contact.getBanner }}
style={{
width: '100%',
height: 150,
Expand All @@ -348,7 +348,7 @@ export default class ContactDetails extends React.Component<
)}
{contact.photo && (
<Image
source={{ uri: contact.photo }}
source={{ uri: contact.getPhoto }}
style={{
width: 150,
height: 150,
Expand Down
4 changes: 2 additions & 2 deletions views/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ export default class Send extends React.Component<SendProps, SendState> {
alignItems: 'center'
}}
>
{item.photo && (
{contact.photo && (
<Image
source={{ uri: item.photo }}
source={{ uri: contact.getPhoto }}
style={{
width: 40,
height: 40,
Expand Down
15 changes: 12 additions & 3 deletions views/Settings/AddContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export default class AddContact extends React.Component<

// Set the local file path in the state
this.setState({
photo: 'file://' + filePath
photo: 'rnfs://' + fileName
});
} catch (error) {
console.error('Error saving file: ', error);
Expand Down Expand Up @@ -371,6 +371,14 @@ export default class AddContact extends React.Component<
}
}

getPhoto(photo): string {
if (photo?.includes('rnfs://')) {
const fileName = photo.replace('rnfs://', '');
return `file://${RNFS.DocumentDirectoryPath}/${fileName}`;
}
return photo || '';
}

render() {
const { navigation } = this.props;
const {
Expand All @@ -381,6 +389,7 @@ export default class AddContact extends React.Component<
pubkey,
name,
description,
photo,
isValidOnchainAddress,
isValidLightningAddress,
isValidNIP05,
Expand Down Expand Up @@ -487,10 +496,10 @@ export default class AddContact extends React.Component<
justifyContent: 'center'
}}
>
{this.state.photo ? (
{photo ? (
<Image
source={{
uri: this.state.photo
uri: this.getPhoto(photo)
}}
style={styles.photo}
/>
Expand Down
4 changes: 2 additions & 2 deletions views/Settings/Contacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ export default class Contacts extends React.Component<
alignItems: 'center'
}}
>
{item.photo && (
{contact.photo && (
<Image
source={{ uri: item.photo }}
source={{ uri: contact.getPhoto }}
style={{
width: 40,
height: 40,
Expand Down