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

fix: chat delete and iOS file path issues #4

Merged
merged 2 commits into from
Dec 1, 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
2 changes: 1 addition & 1 deletion react-native/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ android {
applicationId "com.aws.swiftchat"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 8
versionCode 10
versionName "1.5.0"
ndk {
//noinspection ChromeOsAbiSupport
Expand Down
4 changes: 2 additions & 2 deletions react-native/ios/SwiftChat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@
CODE_SIGN_ENTITLEMENTS = SwiftChat/SwiftChat.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 8;
CURRENT_PROJECT_VERSION = 10;
DEVELOPMENT_TEAM = BUA6W9H7T3;
ENABLE_BITCODE = NO;
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES;
Expand Down Expand Up @@ -526,7 +526,7 @@
CODE_SIGN_ENTITLEMENTS = SwiftChat/SwiftChat.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 8;
CURRENT_PROJECT_VERSION = 10;
DEVELOPMENT_TEAM = BUA6W9H7T3;
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES;
INFOPLIST_FILE = SwiftChat/Info.plist;
Expand Down
6 changes: 5 additions & 1 deletion react-native/src/chat/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ function ChatScreen(): React.JSX.Element {
const { id } = event.params;
if (sessionIdRef.current === id) {
sessionIdRef.current = getSessionId() + 1;
setUsage(undefined);
bedrockMessages.current = [];
clearCachedNode();
setMessages([]);
}
Expand Down Expand Up @@ -501,7 +503,9 @@ function ChatScreen(): React.JSX.Element {
/>
)
}
renderMessage={props => <CustomMessageComponent {...props} />}
renderMessage={props => (
<CustomMessageComponent {...props} chatStatus={chatStatus} />
)}
listViewProps={{
contentContainerStyle: styles.contentContainer,
contentInset: { top: 2 },
Expand Down
12 changes: 7 additions & 5 deletions react-native/src/chat/component/CustomFileListComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ImageSource } from 'react-native-image-viewing/dist/@types';
import Share from 'react-native-share';
import FileViewer from 'react-native-file-viewer';
import { isMac } from '../../App.tsx';
import { getFullFileUrl } from '../util/FileUtils.ts';

interface CustomFileProps {
files: FileInfo[];
Expand Down Expand Up @@ -55,6 +56,7 @@ export const CustomFileListComponent: React.FC<CustomFileProps> = ({

const renderFileItem = (file: FileInfo, fileIndex: number) => {
const isImage = file.type === FileType.image;
const fullFileUrl = getFullFileUrl(file.url);
const itemKey = `file-${fileIndex}-${file.url}`;
return (
<View
Expand All @@ -81,7 +83,7 @@ export const CustomFileListComponent: React.FC<CustomFileProps> = ({
try {
const options = {
type: 'text/plain',
url: file.url,
url: fullFileUrl,
showAppsToView: true,
};
Share.open(options).then();
Expand All @@ -91,13 +93,13 @@ export const CustomFileListComponent: React.FC<CustomFileProps> = ({
}}
onPress={() => {
if (isMac || file.type === FileType.document) {
openInFileViewer(file.url);
openInFileViewer(fullFileUrl);
} else {
const images = files
.filter(item => item.type === FileType.image)
.map(item => ({ uri: item.url }));
.map(item => ({ uri: getFullFileUrl(item.url) }));
const currentIndex = images.findIndex(
img => img.uri === file.url
img => img.uri === fullFileUrl
);
setImageUrls(images);
setIndex(currentIndex);
Expand All @@ -106,7 +108,7 @@ export const CustomFileListComponent: React.FC<CustomFileProps> = ({
}}>
{isImage ? (
<Image
source={{ uri: file.url }}
source={{ uri: fullFileUrl }}
style={styles.thumbnail}
resizeMode="cover"
/>
Expand Down
21 changes: 16 additions & 5 deletions react-native/src/chat/component/CustomMessageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { IMessage, MessageProps } from 'react-native-gifted-chat';
import { CustomMarkdownRenderer } from './CustomMarkdownRenderer.tsx';
import { MarkedStyles } from 'react-native-marked/src/theme/types.ts';
import ImageView from 'react-native-image-viewing';
import { PressMode } from '../../types/Chat.ts';
import { ChatStatus, PressMode } from '../../types/Chat.ts';
import { trigger } from '../util/HapticUtils.ts';
import { HapticFeedbackTypes } from 'react-native-haptic-feedback/src/types.ts';
import Clipboard from '@react-native-clipboard/clipboard';
Expand All @@ -33,8 +33,13 @@ import { isMac } from '../../App.tsx';
import { CustomTokenizer } from './CustomTokenizer.ts';
import { State, TapGestureHandler } from 'react-native-gesture-handler';

const CustomMessageComponent: React.FC<MessageProps<IMessage>> = ({
interface CustomMessageProps extends MessageProps<IMessage> {
chatStatus: ChatStatus;
}

const CustomMessageComponent: React.FC<CustomMessageProps> = ({
currentMessage,
chatStatus,
}) => {
const [visible, setIsVisible] = useState(false);
const [imgUrl, setImgUrl] = useState('');
Expand Down Expand Up @@ -79,19 +84,25 @@ const CustomMessageComponent: React.FC<MessageProps<IMessage>> = ({
const customTokenizer = useMemo(() => new CustomTokenizer(), []);
const handleCopy = () => {
if (isEdit) {
setIsEdit(false);
setIsEditValue(false);
return;
}
Clipboard.setString(currentMessage?.text ?? '');
setCopied(true);
};

const handleEdit = () => {
setIsEdit(!isEdit);
setIsEditValue(!isEdit);
};

const onDoubleTap = () => {
setIsEdit(true);
setIsEditValue(true);
};

const setIsEditValue = (value: boolean) => {
if (chatStatus !== ChatStatus.Running) {
setIsEdit(value);
}
};

useEffect(() => {
Expand Down
24 changes: 20 additions & 4 deletions react-native/src/chat/util/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ export const saveFile = async (sourceUrl: string, fileName: string) => {
if (!filesDirExists) {
await RNFS.mkdir(filesDir);
}
const destinationPath = await getUniqueFileName(filesDir, fileName);
const uniqueFileName = await getUniqueFileName(filesDir, fileName);
const destinationPath = `${filesDir}/${uniqueFileName}`;
await RNFS.copyFile(sourceUrl, destinationPath);
return Platform.OS === 'android'
? `file://${destinationPath}`
: destinationPath;
: `files/${uniqueFileName}`;
} catch (error) {
console.warn('Error saving file:', error);
}
Expand All @@ -37,7 +38,8 @@ export const saveFile = async (sourceUrl: string, fileName: string) => {

export const getFileBytes = async (fileUrl: string) => {
try {
return await RNFS.readFile(fileUrl, 'base64');
const fullFileUrl = getFullFileUrl(fileUrl);
return await RNFS.readFile(fullFileUrl, 'base64');
} catch (error) {
console.warn('Error reading image file:', fileUrl, error);
throw error;
Expand All @@ -61,7 +63,21 @@ const getUniqueFileName = async (
finalFileName = `${nameWithoutExt}(${counter})${extension}`;
finalPath = `${basePath}/${finalFileName}`;
}
return finalPath;
return finalFileName;
};

export const getFullFileUrl = (url: string) => {
if (Platform.OS === 'android') {
return url;
} else if (url.startsWith('files/')) {
return `${RNFS.DocumentDirectoryPath}/${url}`;
} else {
return (
RNFS.DocumentDirectoryPath +
'/files' +
url.substring(url.lastIndexOf('/'))
);
}
};

const MAX_IMAGES = 20;
Expand Down