Skip to content

Commit

Permalink
fix: 修复授权弹窗的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chj-damon committed Apr 23, 2024
1 parent 57f5fb6 commit 5707f68
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-turtles-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native-image-picker': minor
---

fix: 修复授权弹窗的bug
21 changes: 1 addition & 20 deletions packages/react-native-image-picker/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { forwardRef, useImperativeHandle } from 'react';
import { Image, Rationale, TouchableWithoutFeedback } from 'react-native';
import { Image, TouchableWithoutFeedback } from 'react-native';
import { CameraOptions } from 'react-native-image-picker';

import { ActionSheet, Box, helpers, Indicator, Modal, Pressable, Theme, useTheme } from '@td-design/react-native';
Expand All @@ -16,21 +16,6 @@ const cameraOptions: CameraOptions = {
quality: 1,
saveToPhotos: false,
durationLimit: 15,
videoQuality: 'high',
};
const cameraRationaleOptions: Rationale = {
title: '获取摄像头权限',
message: '若不允许,您将无法使用摄像头功能',
buttonPositive: '同意',
buttonNegative: '取消',
buttonNeutral: '下次再说',
};
const libraryRationaleOptions: Rationale = {
title: '获取读取文件权限',
message: '若不允许,您将无法访问图库',
buttonPositive: '同意',
buttonNegative: '取消',
buttonNeutral: '下次再说',
};

const ImagePicker = forwardRef<ImagePickerRef, ImagePickerProps>(
Expand All @@ -41,8 +26,6 @@ const ImagePicker = forwardRef<ImagePickerRef, ImagePickerProps>(
height = px(100),
options = cameraOptions,
showUploadImg = true,
cameraRationale = cameraRationaleOptions,
libraryRationale = libraryRationaleOptions,
launchLibraryText = '打开相册',
launchCameraText = '打开摄像头',
previewImgText = '预览图片',
Expand Down Expand Up @@ -85,8 +68,6 @@ const ImagePicker = forwardRef<ImagePickerRef, ImagePickerProps>(
onCancel,
onFail,
onGrantFail,
cameraRationale,
libraryRationale,
});

useImperativeHandle(ref, () => ({}));
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-image-picker/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ export type HookProps = Pick<
ImagePickerProps,
'value' | 'onBeforeUpload' | 'onUpload' | 'onAfterUpload' | 'onCancel' | 'onFail' | 'onGrantFail'
> &
Required<Pick<ImagePickerProps, 'options' | 'showUploadImg' | 'cameraRationale' | 'libraryRationale'>>;
Required<Pick<ImagePickerProps, 'options' | 'showUploadImg'>>;
14 changes: 1 addition & 13 deletions packages/react-native-image-picker/src/useImagePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export default function useImagePicker({
onCancel,
onFail,
onGrantFail,
cameraRationale,
libraryRationale,
}: HookProps) {
/** 打开相册或者摄像头的ActionSheet */
const [launchVisible, { setTrue: setLaunchVisibleTrue, setFalse: setLaunchVisibleFalse }] = useBoolean(false);
Expand All @@ -46,24 +44,14 @@ export default function useImagePicker({

/** 打开相册 */
const launchLibrary = async () => {
if (Platform.OS === 'android') {
const result = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
libraryRationale
);
if (result !== 'granted') {
onGrantFail?.();
return;
}
}
const response = await launchImageLibrary(options);
handleCallback(response);
};

/** 打开摄像头 */
const launchCamera = async () => {
if (Platform.OS === 'android') {
const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, cameraRationale);
const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
if (result !== 'granted') {
onGrantFail?.();
return;
Expand Down

0 comments on commit 5707f68

Please sign in to comment.