Skip to content

Commit

Permalink
chore: permissions refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanFnz committed Dec 6, 2024
1 parent a2f6d97 commit bd5769d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
import { useState } from 'react';
import { PermissionsAndroid, Platform } from 'react-native';
import AudioRecorderPlayer from 'react-native-audio-recorder-player';

import { usePermissions } from '@root/src/utils/hooks/usePermissions';

const audioRecorderPlayer = new AudioRecorderPlayer();

export const useAudioRecorder = () => {
const [isRecording, setIsRecording] = useState(false);
const [recordingPath, setRecordingPath] = useState<string | null>(null);
const { requestAudioPermissions } = usePermissions();

const startRecording = async () => {
try {
// TODO: move this to permissions helper
if (Platform.OS === 'android') {
try {
const grants = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
]);

console.log('write external storage', grants);

if (
grants['android.permission.WRITE_EXTERNAL_STORAGE'] ===
PermissionsAndroid.RESULTS.GRANTED &&
grants['android.permission.READ_EXTERNAL_STORAGE'] ===
PermissionsAndroid.RESULTS.GRANTED &&
grants['android.permission.RECORD_AUDIO'] ===
PermissionsAndroid.RESULTS.GRANTED
) {
console.log('Permissions granted');
} else {
console.log('All required permissions not granted');
return;
}
} catch (err) {
console.warn(err);
return;
}
const hasPermissions = await requestAudioPermissions();
if (!hasPermissions) {
console.log('Permissions not granted');
return;
}

setIsRecording(true);
Expand Down
33 changes: 33 additions & 0 deletions src/utils/hooks/usePermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { PermissionsAndroid, Platform } from 'react-native';

export const usePermissions = () => {
const requestAudioPermissions = async (): Promise<boolean> => {
if (Platform.OS === 'android') {
try {
const grants = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
]);

console.log('Permissions status:', grants);

return (
grants['android.permission.WRITE_EXTERNAL_STORAGE'] ===
PermissionsAndroid.RESULTS.GRANTED &&
grants['android.permission.READ_EXTERNAL_STORAGE'] ===
PermissionsAndroid.RESULTS.GRANTED &&
grants['android.permission.RECORD_AUDIO'] ===
PermissionsAndroid.RESULTS.GRANTED
);
} catch (err) {
console.warn('Permission request failed:', err);
return false;
}
}

return true;
};

return { requestAudioPermissions };
};
28 changes: 1 addition & 27 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
function formatTimestampToDate(timestamp: number): string {
const date = new Date(timestamp);
return new Intl.DateTimeFormat('en-US', {
month: 'short', // "Jan"
day: 'numeric', // "6"
year: 'numeric', // "2023"
}).format(date);
}

function formatTimestampToDateTime(timestamp: number): string {
const date = new Date(timestamp);
const formattedDate = new Intl.DateTimeFormat('en-US', {
month: 'short', // "Jan"
day: 'numeric', // "6"
year: 'numeric', // "2023"
}).format(date);

const formattedTime = new Intl.DateTimeFormat('en-US', {
hour: 'numeric', // "6"
minute: 'numeric', // "49"
hour12: true, // "AM/PM"
}).format(date);

return `${formattedDate} at ${formattedTime}`;
}

export { formatTimestampToDate, formatTimestampToDateTime };
export { formatTimestampToDate, formatTimestampToDateTime } from './time';
27 changes: 27 additions & 0 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function formatTimestampToDate(timestamp: number): string {
const date = new Date(timestamp);
return new Intl.DateTimeFormat('en-US', {
month: 'short', // "Jan"
day: 'numeric', // "6"
year: 'numeric', // "2023"
}).format(date);
}

function formatTimestampToDateTime(timestamp: number): string {
const date = new Date(timestamp);
const formattedDate = new Intl.DateTimeFormat('en-US', {
month: 'short', // "Jan"
day: 'numeric', // "6"
year: 'numeric', // "2023"
}).format(date);

const formattedTime = new Intl.DateTimeFormat('en-US', {
hour: 'numeric', // "6"
minute: 'numeric', // "49"
hour12: true, // "AM/PM"
}).format(date);

return `${formattedDate} at ${formattedTime}`;
}

export { formatTimestampToDate, formatTimestampToDateTime };

0 comments on commit bd5769d

Please sign in to comment.