Initial commit
This commit is contained in:
92
app/src/utils/permissions.ts
Normal file
92
app/src/utils/permissions.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { PermissionsAndroid, Platform, Linking, Alert, NativeModules } from 'react-native';
|
||||
|
||||
const { PermissionsModule } = NativeModules;
|
||||
|
||||
export async function checkStoragePermission(): Promise<boolean> {
|
||||
if (Platform.OS !== 'android') {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const apiLevel = Platform.Version;
|
||||
|
||||
if (apiLevel >= 30) {
|
||||
// Pour Android 11+, utiliser notre module natif
|
||||
const hasModule = PermissionsModule?.checkManageFilesPermission;
|
||||
if (hasModule) {
|
||||
const hasPermission = await hasModule();
|
||||
return hasPermission;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasPermission = await PermissionsAndroid.check(
|
||||
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE
|
||||
);
|
||||
|
||||
return hasPermission;
|
||||
} catch (err) {
|
||||
console.error('Erreur lors de la vérification de permission:', err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function requestStoragePermission(): Promise<boolean> {
|
||||
if (Platform.OS !== 'android') {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const apiLevel = Platform.Version;
|
||||
|
||||
// Android 11+ (API 30+) nécessite MANAGE_EXTERNAL_STORAGE
|
||||
if (apiLevel >= 30) {
|
||||
// Expliquer pourquoi on a besoin de cette permission
|
||||
Alert.alert(
|
||||
'Permission requise',
|
||||
'Pour lire vos modèles GGUF, l\'application a besoin d\'accéder à tous les fichiers.\n\n' +
|
||||
'Activez "Autoriser la gestion de tous les fichiers" dans l\'écran suivant.',
|
||||
[
|
||||
{ text: 'Annuler', style: 'cancel', onPress: () => {} },
|
||||
{
|
||||
text: 'Ouvrir paramètres',
|
||||
onPress: async () => {
|
||||
try {
|
||||
// Utiliser notre module natif
|
||||
const openSettings = PermissionsModule?.openManageFilesSettings;
|
||||
if (openSettings) {
|
||||
await openSettings();
|
||||
} else {
|
||||
await Linking.openSettings();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur ouverture paramètres:', error);
|
||||
// Fallback vers paramètres généraux
|
||||
await Linking.openSettings();
|
||||
}
|
||||
},
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Android 10 et inférieur
|
||||
const granted = await PermissionsAndroid.request(
|
||||
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
|
||||
{
|
||||
title: 'Permission de lecture',
|
||||
message: 'L\'application a besoin d\'accéder à vos fichiers pour charger les modèles.',
|
||||
buttonNeutral: 'Plus tard',
|
||||
buttonNegative: 'Refuser',
|
||||
buttonPositive: 'Autoriser',
|
||||
}
|
||||
);
|
||||
|
||||
return granted === PermissionsAndroid.RESULTS.GRANTED;
|
||||
} catch (err) {
|
||||
console.error('Erreur lors de la demande de permission:', err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user