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

Issue 125 #128

Merged
57 changes: 30 additions & 27 deletions lib/components/audio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
import { Ionicons } from "@expo/vector-icons";
import { AudioType } from "../models/media_class";
import Slider from "@react-native-community/slider";
// Checkout Audio for expo av!
import { Audio} from "expo-av";
import { Audio } from "expo-av";
import uuid from "react-native-uuid";
import { uploadAudio } from "../utils/S3_proxy";

Expand Down Expand Up @@ -38,7 +37,9 @@ function AudioContainer({
const [currentIndex, setCurrentIndex] = useState(-1);
const [sliderValues, setSliderValues] = useState<number[]>([]);
const [pausedPosition, setPausedPosition] = useState<number | null>(null);
const [pausedAudioIndex, setPausedAudioIndex] = useState<number | null>(null);
const [pausedAudioIndex, setPausedAudioIndex] = useState<number | null>(
null
);
const [isLoaded, setIsLoaded] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -76,7 +77,6 @@ function AudioContainer({
}
};


async function startRecording() {
setIsRecording(true);
try {
Expand All @@ -87,22 +87,24 @@ function AudioContainer({
playsInSilentModeIOS: true,
allowsRecordingIOS: true,
});

const { recording } = await Audio.Recording.createAsync(Audio.RecordingOptionsPresets.HIGH_QUALITY);

setRecording(recording);
} else {
alert("Please grant permission to app to access microphone");
}
} catch (err) {
console.error("Failed to start recording", err);
const { recording } = await Audio.Recording.createAsync(
Audio.RecordingOptionsPresets.HIGH_QUALITY
);

setRecording(recording);
} else {
alert("Please grant permission to the app to access the microphone");
}
console.log("Start recording");
} catch (err) {
console.error("Failed to start recording", err);
}
}

async function stopRecording() {
async function stopRecording() {
setIsRecording(false);

await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
playsInSilentModeIOS: true,
Expand Down Expand Up @@ -144,7 +146,7 @@ function AudioContainer({
console.error("Failed to stop recording", err);
}
}

async function playAudio(index: number) {
console.log("entered audio player");
const current = newAudio[index];
Expand All @@ -160,9 +162,8 @@ function AudioContainer({
const newPlayer = new Audio.Sound();

console.log("play uri===", current.getUri());
await newPlayer.loadAsync({ uri:current.getUri() });
await newPlayer.loadAsync({ uri: current.getUri() });
newPlayer.setOnPlaybackStatusUpdate((status) => {

setIsLoaded(status.isLoaded);

if (status.didJustFinish) {
Expand Down Expand Up @@ -262,23 +263,25 @@ function AudioContainer({
width: "100%",
}}
>
<Ionicons name={"mic-outline"} size={60} color="#111111" />
{isRecording ? (
<Ionicons name={"mic-outline"} size={60} color="red" />
) : (
<Ionicons name={"mic-outline"} size={60} color="#111111" />
)}
<Text style={{ fontSize: 24, fontWeight: "600" }}>Recordings</Text>
{isRecording ? (

<TouchableOpacity
onPress={ () => stopRecording() }
<TouchableOpacity
onPress={() => stopRecording()}
testID="stopRecordingButton"
>
<Ionicons name={"stop-circle-outline"} size={45} color="#111111" />
<Ionicons name={"stop-circle-outline"} size={45} color="#111111" />
</TouchableOpacity>
) : (
<TouchableOpacity
onPress={ () => startRecording() }
<TouchableOpacity
onPress={() => startRecording()}
testID="startRecordingButton"
>
<Ionicons name={"radio-button-on-outline"} size={45} color="red" />

</TouchableOpacity>
)}
</View>
Expand Down Expand Up @@ -334,7 +337,7 @@ function AudioContainer({
<Ionicons name={"pause-outline"} size={25} color="#111111" />
</TouchableOpacity>
) : (
<TouchableOpacity onPress={() => playAudio(index)}testID="playButton">
<TouchableOpacity onPress={() => playAudio(index)} testID="playButton">
<Ionicons name={"play-outline"} size={25} color="#111111" />
</TouchableOpacity>
)}
Expand Down Expand Up @@ -387,4 +390,4 @@ const styles = StyleSheet.create({
width: 200,
textAlign: "center",
},
});
});
63 changes: 35 additions & 28 deletions lib/components/location.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ async function getLocation() {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
console.log("Permission to access location was denied");
return null; // Return null to indicate that location access is denied
return null;
}
return await Location.getCurrentPositionAsync({});
} catch (error) {
console.error("Error getting location:", error);
return null; // Return null to indicate an error occurred while getting location
return null;
}
}

Expand Down Expand Up @@ -73,6 +73,7 @@ export default function LocationWindow({
location?.longitude?.toString() || ""
);
const [distanceFromEvent, setDistanceFromEvent] = useState<string>("");
const [isLocationShown, setIsLocationShown] = useState(true);

useEffect(() => {
const updateDistance = async () => {
Expand Down Expand Up @@ -103,33 +104,36 @@ export default function LocationWindow({
setLongitude(location?.longitude?.toString() || "");
}, [location]);

const setShownLocation = async () => {
try {
let userLocation = await getLocation();

if (userLocation?.coords?.latitude !== undefined && userLocation?.coords?.longitude !== undefined) {
setLocation({
latitude: userLocation.coords.latitude,
longitude: userLocation.coords.longitude,
});

setLatitude(userLocation.coords.latitude.toString());
setLongitude(userLocation.coords.longitude.toString());
} else {
console.log("Location data is not available.");
const toggleLocationVisibility = async () => {
if (isLocationShown) {
// Hide Location
setLocation({
latitude: 0,
longitude: 0,
});
setLatitude("0");
setLongitude("0");
} else {
// Show Location
try {
let userLocation = await getLocation();

if (userLocation?.coords?.latitude !== undefined && userLocation?.coords?.longitude !== undefined) {
setLocation({
latitude: userLocation.coords.latitude,
longitude: userLocation.coords.longitude,
});

setLatitude(userLocation.coords.latitude.toString());
setLongitude(userLocation.coords.longitude.toString());
} else {
console.log("Location data is not available.");
}
} catch (error) {
console.error("Error setting location:", error);
}
} catch (error) {
console.error("Error setting location:", error);
}
};

const getDistance = async () => {
const lat1 = location?.latitude || 0;
const lon1 = location?.longitude || 0;
const currentLocation = await getLocation();
const lat2 = currentLocation?.coords.latitude || 0;
const lon2 = currentLocation?.coords.longitude || 0;
return getDistanceFrom(lat1, lon1, lat2, lon2);
setIsLocationShown((prev) => !prev);
};

return (
Expand All @@ -151,7 +155,10 @@ export default function LocationWindow({
value={latitude}
onChangeText={handleLatitudeChange}
/>
<Button title="Use Current Location" onPress={setShownLocation} />
<Button
title={isLocationShown ? "Hide Location" : "Show Location"}
onPress={toggleLocationVisibility}
/>
</View>
);
}
Expand Down
106 changes: 60 additions & 46 deletions lib/screens/AddNoteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ 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 @@ -179,25 +195,34 @@ 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, 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.");
// 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 }
);
return false;
}

// Location permission not yet granted
return false;
}

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

const saveNote = async () => {
const locationPermissionGranted = await checkLocationPermission();
if (titleText === "") {
if (titleText.trim() === "") {
Alert.alert(
"Title is empty",
"Please enter a title to save the note.",
);
return;
}
if (!locationPermissionGranted) {
Alert.alert(
"Delete Note",
"Location permissions required to save. Are you sure you want to delete this note?",
"Empty Title",
"Please enter a title to save the note or delete the note.",
[
{
text: "Cancel",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
{
text: "Delete",
onPress: () => navigation.goBack()
}
{ text: "Delete Note", onPress: () => navigation.goBack() },
{ text: "OK", onPress: () => console.log("OK Pressed") }
],
{ cancelable: false }
);
return;
}
else {

const locationPermissionGranted = await checkLocationPermission();
if (!locationPermissionGranted) {
return; // Stop saving the note if location permission is not granted
} else {
try {
const userLocation = await Location.getCurrentPositionAsync({});
const userID = await user.getId();
let latitude, longitude;

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


// 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 newNote = {
title: titleText,
text: bodyText,
Expand All @@ -260,10 +271,10 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
time: time,
};
const response = await ApiService.writeNewNote(newNote);

const obj = await response.json();
const id = obj["@id"];

route.params.refreshPage();
navigation.goBack();
} catch (error) {
Expand All @@ -272,6 +283,9 @@ const AddNoteScreen: React.FC<AddNoteScreenProps> = ({ navigation, route }) => {
}
};




return (
<SafeAreaView style={{ flex: 1 }}>
<View style={NotePageStyles().topContainer}>
Expand Down
Loading