Skip to content

Commit

Permalink
Add android permission function and lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
saad-abid-crowdbotics committed Oct 16, 2023
1 parent aa3728e commit b152e25
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useContext} from "react";
import React, { useContext } from "react";
import { View, StyleSheet, FlatList, Text, TouchableNativeFeedback, Image } from "react-native";
import { OptionsContext } from "@options";

Expand All @@ -19,7 +19,7 @@ import cool from "../../assets/cool1.jpg";
import bright from "../../assets/bright1.jpg";

export default function Filter({ selectFilter }) {
const {FILTERS} = useContext(OptionsContext)
const { FILTERS } = useContext(OptionsContext);
const warmUri = Image.resolveAssetSource(Warm).uri;
const classicUri = Image.resolveAssetSource(classic).uri;
const sharpUri = Image.resolveAssetSource(sharp).uri;
Expand Down
44 changes: 38 additions & 6 deletions modules/react-native-live-camera-filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,50 @@ export const App = () => {
script.takePicture(cameraRef, [fileName]);
};

const hasAndroidPermission = async () => {
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
const hasPermission = await PermissionsAndroid.check(permission);
async function hasAndroidPermission() {
const getCheckPermissionPromise = () => {
if (Platform.Version >= 33) {
return Promise.all([
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES),
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO)
]).then(
([hasReadMediaImagesPermission, hasReadMediaVideoPermission]) =>
hasReadMediaImagesPermission && hasReadMediaVideoPermission
);
} else {
return PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE);
}
};

const hasPermission = await getCheckPermissionPromise();
if (hasPermission) {
return true;
}
const status = await PermissionsAndroid.request(permission);
return status === "granted";
};
const getRequestPermissionPromise = () => {
if (Platform.Version >= 33) {
return PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES,
PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO
]).then(
(statuses) =>
statuses[PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES] ===
PermissionsAndroid.RESULTS.GRANTED &&
statuses[PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO] ===
PermissionsAndroid.RESULTS.GRANTED
);
} else {
return PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE).then((status) => status === PermissionsAndroid.RESULTS.GRANTED);
}
};

return await getRequestPermissionPromise();
}

const onPictureTaken = async (pic) => {
const picUri = pic?.nativeEvent?.uri;
if (Platform.OS === "android" && !(await hasAndroidPermission())) {
return;
}
await CameraRoll.save(picUri, { type: "photo" });
setIsLoading(false);
Alert.alert(
Expand Down
4 changes: 2 additions & 2 deletions modules/react-native-live-camera-filters/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ export const FILTERS = [

export default {
styles: styles,
FILTERS:FILTERS
}
FILTERS: FILTERS
};

0 comments on commit b152e25

Please sign in to comment.