Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddNote and EditNote Screen Scroller Adjuster #139

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
},
"updates": {
"url": "https://u.expo.dev/801029ef-db83-4668-a97a-5adcc4c333e2"

},
"extra": {

Expand Down
42 changes: 42 additions & 0 deletions lib/components/LoadingModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import { View, Modal, Text, StyleSheet } from 'react-native';

const LoadingModal = ({ visible }) => (
<Modal
animationType="fade"
transparent={true}
visible={visible}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text>Please wait, saving changes to the note</Text>
</View>
</View>
</Modal>
);

const styles = StyleSheet.create({
centeredView: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 20,
padding: 35,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
});

export default LoadingModal;
2 changes: 1 addition & 1 deletion lib/components/loadingImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ const styles = StyleSheet.create({
position: "absolute",
alignSelf: "center",
},
});
});
2 changes: 1 addition & 1 deletion lib/components/photoScroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const PhotoScroller = forwardRef(
});
setNewMedia([...newMedia, newMediaItem]);
if (insertImageToEditor) {
insertImageToEditor(uploadedUrl, 'Captured Image');
insertImageToEditor(uploadedUrl);
}
} else if (
uri.endsWith(".MOV") ||
Expand Down
112 changes: 47 additions & 65 deletions lib/screens/AddNoteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from "react-native-pell-rich-editor";
import NotePageStyles from "../../styles/pages/NoteStyles";
import { useTheme } from "../components/ThemeProvider";
import LoadingModal from "../components/LoadingModal";

const user = User.getInstance();

Expand All @@ -53,6 +54,7 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
const [keyboardHeight, setKeyboardHeight] = useState(0);
const scrollViewRef = useRef<ScrollView | null>(null);
const [initialLoad, setInitialLoad] = useState(true);
const [isUpdating, setIsUpdating] = useState(false);
const [promptedMissingTitle, setPromptedMissingTitle] = useState(false);
const [location, setLocation] = useState<{
latitude: number;
Expand Down Expand Up @@ -88,22 +90,6 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
};
}, []);

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

const grabLocation = async () => {
try {
const userLocation = await Location.getCurrentPositionAsync({});
setLocation({
latitude: userLocation.coords.latitude,
longitude: userLocation.coords.longitude,
});
} catch (error) {
console.error("Error grabbing location:", error);
}
};

const handleCursorPosition = (position) => {
if (scrollViewRef.current && keyboardOpen) {
const editorBottomY = position.absoluteY + position.height;
Expand Down Expand Up @@ -195,34 +181,25 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
const checkLocationPermission = async () => {
try {
let { status } = await Location.getForegroundPermissionsAsync();

if (status !== 'granted') {
// Location permission not granted, request it
const requestResult = await Location.requestForegroundPermissionsAsync();

if (requestResult.status === 'denied') {
// Location permission denied after requesting, show alert and return false
Alert.alert(
"Location permission denied",
"Please grant location permission to save the note.",
[
{
text: "Delete Note",
onPress: () => navigation.goBack(), // Delete the note and go back
style: "destructive",
},
{ text: "OK", onPress: () => console.log("OK Pressed") },
],
{ cancelable: false }
);
// Location permission denied after requesting, request again
const requestAgainResult = await Location.requestForegroundPermissionsAsync();
status = requestAgainResult.status;
}

if (status !== 'granted') {
// Location permission still not granted
Alert.alert("Location permission denied", "Please grant location permission to save the note or remove the title to not save.");
return false;
}

// Location permission not yet granted
return false;
}
// Location permission already granted

// Location permission already granted or granted after request
return true;
} catch (error) {
console.error("Error checking location permission:", error);
Expand All @@ -231,33 +208,39 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
};

const saveNote = async () => {
if (titleText.trim() === "") {
Alert.alert(
"Empty Title",
"Please enter a title to save the note or delete the note.",
[
{ text: "Delete Note", onPress: () => navigation.goBack() },
{ text: "OK", onPress: () => console.log("OK Pressed") }
],
{ cancelable: false }
);
return;
}

const locationPermissionGranted = await checkLocationPermission();
if (titleText === "") {
if (!promptedMissingTitle) {
setPromptedMissingTitle(true);
Alert.alert(
"Title is empty",
"Please enter a title to save the note, or press back again to confirm not saving the note.",
);
return;
} else {
navigation.goBack();
return;
}
}
if (!locationPermissionGranted) {
return; // Stop saving the note if location permission is not granted
} else {
return;
}
else {
setIsUpdating(true);

try {
const userID = await user.getId();

// Grab user's current location
const userLocation = await Location.getCurrentPositionAsync({});
const latitude = userLocation.coords.latitude.toString();
const longitude = userLocation.coords.longitude.toString();

setTime(new Date()); // force a fresh time date grab on note save

const userID = await user.getId();
let latitude, longitude;

if (Platform.OS === 'ios') {
latitude = location?.latitude.toString();
longitude = location?.longitude.toString();
} else if (Platform.OS === 'android') {
latitude = userLocation.coords.latitude.toString();
longitude = userLocation.coords.longitude.toString();
}

const newNote = {
title: titleText,
text: bodyText,
Expand All @@ -279,15 +262,14 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
navigation.goBack();
} catch (error) {
console.error("An error occurred while creating the note:", error);
} finally {
setIsUpdating(false);
}
}
};




return (
<SafeAreaView style={{ flex: 1 }}>
<SafeAreaView style={{ flex: 1, backgroundColor: '#161A1D' }}>
<View style={NotePageStyles().topContainer}>

<View style={NotePageStyles().topButtonsContainer}>
Expand Down Expand Up @@ -504,7 +486,7 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
</View>
</KeyboardAvoidingView>


<LoadingModal visible={isUpdating} />
</SafeAreaView>
);

Expand Down
Loading
Loading