Pobieranie i wysyłanie zdjęć i GPS
This commit is contained in:
@@ -6,9 +6,11 @@ import {
|
||||
ScrollView,
|
||||
Image,
|
||||
View,
|
||||
Alert,
|
||||
} from "react-native";
|
||||
import {TextInput, Button, Snackbar, Text} from "react-native-paper";
|
||||
import {TextInput, Button, Snackbar, Text, useTheme} from "react-native-paper";
|
||||
import {requestCameraPermissionsAsync, launchCameraAsync} from 'expo-image-picker';
|
||||
import * as Location from 'expo-location';
|
||||
import useLocationStore from "@/locationStore";
|
||||
|
||||
export default function FormScreen() {
|
||||
@@ -18,11 +20,15 @@ export default function FormScreen() {
|
||||
image: "",
|
||||
area: 0,
|
||||
population: 0,
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
});
|
||||
const [message, setMessage] = useState("");
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [picture, setPicture] = useState(null);
|
||||
const [imageMethod, setImageMethod] = useState(null); // null, 'link' или 'camera'
|
||||
const [imageMethod, setImageMethod] = useState(null);
|
||||
const [location, setLocation] = useState < Location.LocationObject | null > (null);
|
||||
const theme = useTheme();
|
||||
|
||||
const addLocation = useLocationStore((state) => state.addLocation);
|
||||
|
||||
@@ -32,7 +38,9 @@ export default function FormScreen() {
|
||||
formData.description &&
|
||||
formData.image &&
|
||||
formData.area &&
|
||||
formData.population
|
||||
formData.population &&
|
||||
formData.latitude &&
|
||||
formData.longitude
|
||||
) {
|
||||
const newLocation = {
|
||||
id: Date.now(),
|
||||
@@ -41,6 +49,8 @@ export default function FormScreen() {
|
||||
image: formData.image,
|
||||
area: parseFloat(formData.area),
|
||||
population: parseInt(formData.population),
|
||||
latitude: parseFloat(formData.latitude),
|
||||
longitude: parseFloat(formData.longitude),
|
||||
};
|
||||
|
||||
const added = await addLocation(newLocation);
|
||||
@@ -50,6 +60,8 @@ export default function FormScreen() {
|
||||
image: "",
|
||||
area: 0,
|
||||
population: 0,
|
||||
latitude: 0,
|
||||
longitude: 0
|
||||
});
|
||||
setPicture(null);
|
||||
setImageMethod(null);
|
||||
@@ -83,11 +95,36 @@ export default function FormScreen() {
|
||||
if (!result.canceled && result.assets && result.assets.length > 0) {
|
||||
const image = result.assets[0];
|
||||
setPicture(image.uri);
|
||||
console.log(image.base64);
|
||||
setFormData({...formData, image: image.base64});
|
||||
}
|
||||
}
|
||||
|
||||
async function getCurrentLocation() {
|
||||
|
||||
let {status} = await Location.requestForegroundPermissionsAsync();
|
||||
if (status !== 'granted') {
|
||||
console.log('Permission to access location was denied');
|
||||
Alert.alert("Błąd", "Nie udało się uzyskać dostępu do pobierania lokalizacji");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let location = await Location.getCurrentPositionAsync({});
|
||||
setLocation(location);
|
||||
setFormData({
|
||||
...formData,
|
||||
latitude: location.coords.latitude,
|
||||
longitude: location.coords.longitude
|
||||
});
|
||||
Alert.alert("Sukces!", "Pobrano lokalizację\n" +
|
||||
"Szerokość geograficzna: " + location.coords.latitude + "\n" +
|
||||
"Długość geograficzna: " + location.coords.longitude);
|
||||
} catch (error) {
|
||||
Alert.alert("Błąd", "Nie udało się pobrać lokalizacji");
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
const selectImageMethod = (method) => {
|
||||
setImageMethod(method);
|
||||
if (method === 'camera') {
|
||||
@@ -103,7 +140,9 @@ export default function FormScreen() {
|
||||
style={{flex: 1}}
|
||||
behavior={Platform.OS === "ios" ? "padding" : "height"}
|
||||
>
|
||||
<ScrollView contentContainerStyle={styles.container}>
|
||||
<ScrollView contentContainerStyle={styles.container} contentInsetAdjustmentBehavior="automatic"
|
||||
style={[{backgroundColor: theme.colors.background}]}
|
||||
>
|
||||
<Snackbar
|
||||
visible={visible}
|
||||
onDismiss={() => setVisible(false)}
|
||||
@@ -111,7 +150,7 @@ export default function FormScreen() {
|
||||
>
|
||||
{message}
|
||||
</Snackbar>
|
||||
<Text style={styles.label} variant="labelLarge">Jak chcesz dodać zdjęcie?</Text>
|
||||
<Text style={styles.label} variant="labelLarge">Jak chcesz dodać zdjęcie?</Text>
|
||||
<View style={styles.imageMethodButtons}>
|
||||
<Button
|
||||
mode={imageMethod === 'link' ? "contained" : "outlined"}
|
||||
@@ -193,6 +232,14 @@ export default function FormScreen() {
|
||||
>
|
||||
Dodaj
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
style={{margin: 10, width: "100%"}} mode={"contained"}
|
||||
onPress={getCurrentLocation}
|
||||
icon={location ? "check-circle-outline" : "map-marker-outline"}
|
||||
>
|
||||
Pobierz aktualną lokalizację
|
||||
</Button>
|
||||
</ScrollView>
|
||||
</KeyboardAvoidingView>
|
||||
);
|
||||
@@ -200,11 +247,10 @@ export default function FormScreen() {
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
backgroundColor: "#25292e",
|
||||
justifyContent: "flex-start",
|
||||
// backgroundColor: "#25292e",
|
||||
alignItems: "center",
|
||||
padding: 10,
|
||||
flexGrow: 1,
|
||||
},
|
||||
imageContainer: {
|
||||
width: "100%",
|
||||
@@ -215,7 +261,6 @@ const styles = StyleSheet.create({
|
||||
width: 150,
|
||||
height: 150,
|
||||
borderStyle: "solid",
|
||||
borderColor: "#fff",
|
||||
borderWidth: 1,
|
||||
borderRadius: 5,
|
||||
},
|
||||
@@ -232,7 +277,6 @@ const styles = StyleSheet.create({
|
||||
label: {
|
||||
marginTop: 10,
|
||||
marginBottom: 10,
|
||||
color: "#fff",
|
||||
verticalAlign: "middle",
|
||||
textAlign: "center",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user