Skip to content

Commit

Permalink
Handle Linking.openURL error as well
Browse files Browse the repository at this point in the history
  • Loading branch information
robertying committed Mar 3, 2021
1 parent 63571fb commit 4029baa
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/screens/FileDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ const FileDetail: React.FC<StackScreenProps<ScreenParams, 'FileDetail'>> = ({
await shareFile(route.params);
}, [route.params]);

const handleOpen = async () => {
if (Platform.OS === 'android') {
try {
const handleOpen = useCallback(async () => {
try {
if (Platform.OS === 'android') {
await openFile(path, fileType);
} catch {
toast('文件打开失败', 'error');
} else {
await Linking.openURL(path);
}
} else {
await Linking.openURL(path);
} catch {
toast(
'文件打开失败。请重新下载文件或确保存在可打开此文件类型的应用。',
'error',
);
}
};
}, [fileType, path, toast]);

useLayoutEffect(() => {
if (disableAnimation) {
Expand Down Expand Up @@ -108,7 +111,7 @@ const FileDetail: React.FC<StackScreenProps<ScreenParams, 'FileDetail'>> = ({
{DeviceInfo.isMac() && (
<IconButton
disabled={error || !path}
onPress={() => Linking.openURL(path)}
onPress={handleOpen}
icon={(props) => <Icon {...props} name="open-in-new" />}
/>
)}
Expand All @@ -118,6 +121,7 @@ const FileDetail: React.FC<StackScreenProps<ScreenParams, 'FileDetail'>> = ({
}, [
error,
handleDownload,
handleOpen,
handleShare,
navigation,
path,
Expand Down

0 comments on commit 4029baa

Please sign in to comment.