Skip to content

Commit

Permalink
fixed image download
Browse files Browse the repository at this point in the history
  • Loading branch information
doljko committed May 3, 2024
1 parent 45d40f3 commit 682ed31
Showing 1 changed file with 11 additions and 44 deletions.
55 changes: 11 additions & 44 deletions src/features/Core/screens/ChatScreen.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { faAngleLeft, faEdit, faFile, faPaperPlane, faTrash, faUser } from '@fortawesome/free-solid-svg-icons';
import { faAngleLeft, faEdit, faPaperPlane, faTrash, faUser } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { useNavigation } from '@react-navigation/native';
import config from 'config';
import { useDriver, useFleetbase } from 'hooks';
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, Alert, FlatList, Platform, ScrollView, Text, TouchableOpacity, View } from 'react-native';
import { ActivityIndicator, Alert, FlatList, Image, Platform, ScrollView, Text, TouchableOpacity, View } from 'react-native';
import FastImage from 'react-native-fast-image';
import RNFS from 'react-native-fs';
import { Actions, Bubble, GiftedChat, InputToolbar, Send } from 'react-native-gifted-chat';
import { launchImageLibrary } from 'react-native-image-picker';
import Modal from 'react-native-modal';
import { tailwind } from 'tailwind';
import { createSocketAndListen, translate } from 'utils';

const isAndroid = Platform.OS === 'android';
let { FLEETBASE_HOST } = config;

const ChatScreen = ({ route }) => {
const { channel: channelProps } = route.params;
Expand Down Expand Up @@ -80,6 +77,8 @@ const ChatScreen = ({ route }) => {
return chatParticipant.user === driverUser;
});

const channelUsers = channel?.participants.map(item => item.id);

const uploadFile = async url => {
try {
if (!url || !url.uri || !url.type || !url.fileName) {
Expand Down Expand Up @@ -270,45 +269,11 @@ const ChatScreen = ({ route }) => {
);
};

const openMedia = async url => {
const fileNameParts = url?.split('/')?.pop()?.split('?');
console.log('fileNameParts:::', JSON.stringify(fileNameParts));
const fileName = fileNameParts.length > 0 ? fileNameParts[0] : '';

const localFile = `${RNFS.DocumentDirectoryPath}/${fileName}`;

const options = {
fromUrl: url,
toFile: localFile,
};

RNFS.downloadFile(options).promise.then(() => {
RNFS.readDir(RNFS.DocumentDirectoryPath);
FileViewer.open(localFile);
});
};

const checkIsImage = item => {
if (item && item.content_type) {
return item.content_type.startsWith('image/');
}
};

const renderDocumentItem = image => {
const renderImage = async image => {
console.log('item', image);
return (
<View style={tailwind('flex rounded-md bg-white mt-2 mr-3 ')}>
<TouchableOpacity
onPress={() => {
openMedia(`${FLEETBASE_HOST}${image?.url}`);
}}>
{checkIsImage(image?.original_filename) ? (
<FastImage style={tailwind('w-4 h-4 m-1 ')} source={{ uri: `${FLEETBASE_HOST}${image?.url}` }} resizeMode={FastImage.resizeMode.contain} />
) : (
<View style={tailwind('items-center justify-between p-1 ')}>
<FontAwesomeIcon size={70} icon={faFile} style={tailwind('text-gray-400')} />
</View>
)}
</TouchableOpacity>
<Image source={{ uri: image }} style={tailwind('w-6 h-6')} onError={() => console.warn('Image failed to load')} />
</View>
);
};
Expand All @@ -327,7 +292,9 @@ const ChatScreen = ({ route }) => {
color: '#fff',
},
}}
onPress={() => renderDocumentItem(uploadedImageUrl)}
onPress={() => {
renderImage(uploadedImageUrl);
}}
/>
);
};
Expand Down Expand Up @@ -424,7 +391,7 @@ const ChatScreen = ({ route }) => {
messages={messages}
onSend={messages => onSend(messages)}
user={{
_id: 1,
_id: channelUsers?.id,
}}
renderBubble={renderBubble}
alwaysShowSend
Expand Down

0 comments on commit 682ed31

Please sign in to comment.