Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const App = () => {

useEffect(() => {
SplashScreen.hide();
})
});
return (
<Provider store={store}>
<StateProvider initialState={initialState} reducer={reducer}>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@babel/preset-typescript": "^7.23.3",
"@bam.tech/react-native-image-resizer": "^3.0.11",
"@ethersproject/shims": "^5.8.0",
"@notifee/react-native": "^9.1.8",
"@react-native-async-storage/async-storage": "^2.1.2",
"@react-native-clipboard/clipboard": "^1.16.1",
"@react-native-community/blur": "^4.4.0",
Expand Down
103 changes: 103 additions & 0 deletions src/components/PermissionRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import React, {useState, useEffect} from 'react';
import {View, Text, TouchableOpacity, Alert, StyleSheet} from 'react-native';
import PermissionManager from '../services/PermissionManager';

const PermissionRequest = () => {
const [permissions, setPermissions] = useState({
location: {granted: false, reason: ''},
camera: {granted: false, reason: ''},
notifications: {granted: false, reason: ''},
});

useEffect(() => {
checkPermissions();
}, []);

const checkPermissions = async () => {
const result = await PermissionManager.checkAllPermissions();
setPermissions(result);
};

const requestPermission = async type => {
let result;

switch (type) {
case 'location':
result = await PermissionManager.requestLocationPermission();
break;
case 'camera':
result = await PermissionManager.requestCameraPermission();
break;
case 'notifications':
result = await PermissionManager.requestNotificationPermission();
break;
}

if (result.granted) {
Alert.alert('Success', `${type} permission granted!`);
} else if (result.reason === 'blocked') {
Alert.alert(
'Permission Required',
`${type} permission is required. Please enable it in Settings.`,
[
{text: 'Cancel', style: 'cancel'},
{text: 'Settings', onPress: PermissionManager.openSettings},
],
);
}

checkPermissions(); // Refresh permissions
};

const renderPermissionButton = (type, permission) => (
<TouchableOpacity
style={[
styles.permissionButton,
permission.granted && styles.grantedButton,
]}
onPress={() => requestPermission(type)}
disabled={permission.granted}>
<Text style={styles.buttonText}>
{permission.granted ? `${type} Granted` : `Request ${type}`}
</Text>
</TouchableOpacity>
);

return <></>;
};

const styles = StyleSheet.create({
container: {
padding: 20,
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
textAlign: 'center',
},
permissionButton: {
backgroundColor: '#007AFF',
padding: 15,
borderRadius: 8,
marginBottom: 10,
},
grantedButton: {
backgroundColor: '#34C759',
},
buttonText: {
color: 'white',
textAlign: 'center',
fontSize: 16,
fontWeight: '600',
},
successText: {
color: '#34C759',
textAlign: 'center',
fontSize: 18,
fontWeight: '600',
marginTop: 20,
},
});

export default PermissionRequest;
216 changes: 216 additions & 0 deletions src/components/PermissionTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import React, {useState, useEffect} from 'react';
import {
View,
Text,
TouchableOpacity,
StyleSheet,
Alert,
Platform,
} from 'react-native';
import PermissionManager from '../services/PermissionManager';

const PermissionTest = () => {
const [permissions, setPermissions] = useState({
location: {granted: false, reason: ''},
camera: {granted: false, reason: ''},
notifications: {granted: false, reason: ''},
});

useEffect(() => {
checkAllPermissions();
}, []);

const checkAllPermissions = async () => {
try {
const result = await PermissionManager.checkAllPermissions();
setPermissions(result);
console.log('Permission check result:', result);
} catch (error) {
console.error('Error checking permissions:', error);
}
};

const initializeNotifications = async () => {
try {
const result = await PermissionManager.initializeNotifications();
console.log('Notification initialization result:', result);
checkAllPermissions(); // Refresh permissions after initialization
} catch (error) {
console.error('Error initializing notifications:', error);
}
};

const requestSpecificPermission = async type => {
try {
let result;

switch (type) {
case 'location':
result = await PermissionManager.requestLocationPermission();
break;
case 'camera':
result = await PermissionManager.requestCameraPermission();
break;
case 'notifications':
result = await PermissionManager.requestNotificationPermission();
break;
}

console.log(`${type} permission result:`, result);

if (result.granted) {
Alert.alert('Success', `${type} permission granted!`);
} else {
Alert.alert('Permission Result', `${type}: ${result.reason}`, [
{text: 'OK', style: 'default'},
...(result.reason === 'blocked'
? [{text: 'Settings', onPress: PermissionManager.openSettings}]
: []),
]);
}

// Refresh permissions
checkAllPermissions();
} catch (error) {
console.error(`Error requesting ${type} permission:`, error);
Alert.alert('Error', `Failed to request ${type} permission`);
}
};

const getPermissionStatus = permission => {
if (permission.granted) return '✅ Granted';
if (permission.reason === 'blocked') return '🚫 Blocked';
if (permission.reason === 'denied') return '❌ Denied';
if (permission.reason === 'unavailable') return '⚠️ Unavailable';
return `❓ ${permission.reason}`;
};

return (
<View style={styles.container}>
<Text style={styles.title}>Permission Test</Text>
<Text style={styles.subtitle}>Platform: {Platform.OS}</Text>

<View style={styles.permissionSection}>
<Text style={styles.sectionTitle}>Location</Text>
<Text style={styles.statusText}>
{getPermissionStatus(permissions.location)}
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => requestSpecificPermission('location')}>
<Text style={styles.buttonText}>Request Location</Text>
</TouchableOpacity>
</View>

<View style={styles.permissionSection}>
<Text style={styles.sectionTitle}>Camera</Text>
<Text style={styles.statusText}>
{getPermissionStatus(permissions.camera)}
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => requestSpecificPermission('camera')}>
<Text style={styles.buttonText}>Request Camera</Text>
</TouchableOpacity>
</View>

<View style={styles.permissionSection}>
<Text style={styles.sectionTitle}>Notifications</Text>
<Text style={styles.statusText}>
{getPermissionStatus(permissions.notifications)}
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => requestSpecificPermission('notifications')}>
<Text style={styles.buttonText}>Request Notifications</Text>
</TouchableOpacity>
</View>

<TouchableOpacity
style={[styles.button, styles.refreshButton]}
onPress={checkAllPermissions}>
<Text style={styles.buttonText}>Refresh All</Text>
</TouchableOpacity>

<TouchableOpacity
style={[styles.button, styles.settingsButton]}
onPress={PermissionManager.openSettings}>
<Text style={styles.buttonText}>Open Settings</Text>
</TouchableOpacity>

<TouchableOpacity
style={[styles.button, styles.initButton]}
onPress={initializeNotifications}>
<Text style={styles.buttonText}>Initialize Notifications</Text>
</TouchableOpacity>
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#f5f5f5',
},
title: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 10,
color: '#333',
},
subtitle: {
fontSize: 16,
textAlign: 'center',
marginBottom: 30,
color: '#666',
},
permissionSection: {
backgroundColor: 'white',
padding: 20,
borderRadius: 10,
marginBottom: 15,
shadowColor: '#000',
shadowOffset: {width: 0, height: 2},
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
sectionTitle: {
fontSize: 18,
fontWeight: '600',
marginBottom: 10,
color: '#333',
},
statusText: {
fontSize: 16,
marginBottom: 15,
color: '#666',
},
button: {
backgroundColor: '#007AFF',
padding: 12,
borderRadius: 8,
alignItems: 'center',
},
buttonText: {
color: 'white',
fontSize: 16,
fontWeight: '600',
},
refreshButton: {
backgroundColor: '#34C759',
marginTop: 10,
},
settingsButton: {
backgroundColor: '#FF9500',
marginTop: 10,
},
initButton: {
backgroundColor: '#AF52DE',
marginTop: 10,
},
});

export default PermissionTest;
Loading