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

Allow adding sounds to existing observations #1303

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
6 changes: 5 additions & 1 deletion src/components/ObsEdit/EvidenceSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Body3, Body4, Heading4, INatIcon
} from "components/SharedComponents";
import LocationPermissionGate from "components/SharedComponents/LocationPermissionGate";
import { MAX_SOUNDS_ALLOWED } from "components/SoundRecorder/SoundRecorder";
import { Pressable, View } from "components/styledComponents";
import type { Node } from "react";
import React from "react";
Expand Down Expand Up @@ -58,6 +59,7 @@ const EvidenceSection = ( {
const { t } = useTranslation( );
const theme = useTheme( );
const obsPhotos = currentObservation?.observationPhotos;
const obsSounds = currentObservation?.observationSounds;
const navigation = useNavigation( );

const navToLocationPicker = ( ) => {
Expand Down Expand Up @@ -96,7 +98,9 @@ const EvidenceSection = ( {
return (
<View className="mx-6">
<AddEvidenceSheet
disableAddingMoreEvidence={obsPhotos?.length >= MAX_PHOTOS_ALLOWED}
disableAddingMoreEvidence={
obsPhotos?.length >= MAX_PHOTOS_ALLOWED ||
obsSounds?.length >= MAX_SOUNDS_ALLOWED}
hidden={!showAddEvidenceSheet}
onClose={( ) => setShowAddEvidenceSheet( false )}
/>
Expand Down
6 changes: 4 additions & 2 deletions src/components/ObsEdit/Sheets/AddEvidenceSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { View } from "components/styledComponents";
import type { Node } from "react";
import React, { useState } from "react";
import { Alert } from "react-native";
import useTranslation from "sharedHooks/useTranslation";

type Props = {
Expand Down Expand Up @@ -49,7 +48,10 @@ const AddEvidenceSheet = ( {
params: { skipGroupPhotos: true }
} );
} else if ( choice === "sound" ) {
Alert.alert( "TODO", "Still need to implement sound recording" );
navigation.navigate(
"CameraNavigator",
{ screen: "SoundRecorder", params: { addEvidence: true } }
);
}
}}
>
Expand Down
38 changes: 33 additions & 5 deletions src/components/SoundRecorder/SoundRecorder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import { useNavigation } from "@react-navigation/native";
import { useNavigation, useRoute } from "@react-navigation/native";
import MediaViewerModal from "components/MediaViewer/MediaViewerModal";
import {
Body1,
Expand All @@ -24,6 +24,7 @@ import React, {
import { StatusBar } from "react-native";
import AudioRecorderPlayer from "react-native-audio-recorder-player";
import Observation from "realmModels/Observation";
import ObservationSound from "realmModels/ObservationSound";
import useTranslation from "sharedHooks/useTranslation";
import useStore from "stores/useStore";
import colors from "styles/tailwindColors";
Expand All @@ -38,19 +39,26 @@ const NOT_STARTED = "notStarted";
const RECORDING = "recording";
const STOPPED = "stopped";

export const MAX_SOUNDS_ALLOWED = 20;

const SoundRecorder = (): Node => {
const audioRecorderPlayerRef = useRef( new AudioRecorderPlayer( ) );
const audioRecorderPlayer = audioRecorderPlayerRef.current;
const [mediaViewerVisible, setMediaViewerVisible] = useState( false );
const setObservations = useStore( state => state.setObservations );
const navigation = useNavigation();
const { params } = useRoute();
const { t } = useTranslation();
const [sound, setSound] = useState( INITIAL_SOUND );
const [uri, setUri] = useState( null );
const [helpShown, setHelpShown] = useState( false );
const [exitWarningShown, setExitWarningShown] = useState( false );
const [resetWarningShown, setResetWarningShown] = useState( false );
const meteringHistory = useRef( [] );
const currentObservation = useStore( state => state.currentObservation );
const observations = useStore( state => state.observations );
const currentObservationIndex = useStore( state => state.currentObservationIndex );
const updateObservations = useStore( state => state.updateObservations );

const [
status,
Expand All @@ -63,8 +71,20 @@ const SoundRecorder = (): Node => {
audioRecorderPlayer.setSubscriptionDuration( 0.09 ); // optional. Default is 0.1

const addSound = async ( ) => {
const newObservation = await Observation.createObsWithSoundPath( uri );
setObservations( [newObservation] );
if ( !params.addEvidence ) {
// New observation with sound
const newObservation = await Observation.createObsWithSoundPath( uri );
setObservations( [newObservation] );
} else {
// Add new sounds to existing observation
let updatedCurrentObservation = currentObservation;

const obsSound = await ObservationSound.new( { file_url: uri } );
updatedCurrentObservation = Observation
.appendObsSounds( [obsSound], updatedCurrentObservation );
observations[currentObservationIndex] = updatedCurrentObservation;
updateObservations( observations );
}
};

const resetRecording = useCallback( ( ) => {
Expand Down Expand Up @@ -178,6 +198,14 @@ const SoundRecorder = (): Node => {
helpText = t( "Press-record-to-start" );
}

const onBack = () => {
if ( !params.addEvidence ) {
navigation.goBack( );
} else {
navigation.navigate( "ObsEdit" );
}
};

return (
<ViewWrapper wrapperClassName="bg-black justify-between">
<StatusBar barStyle="light-content" backgroundColor="black" />
Expand Down Expand Up @@ -238,7 +266,7 @@ const SoundRecorder = (): Node => {
if ( uri ) {
setExitWarningShown( true );
} else {
navigation.goBack( );
onBack();
}
}}
mediaCaptured={uri}
Expand Down Expand Up @@ -278,7 +306,7 @@ const SoundRecorder = (): Node => {
hidden={!exitWarningShown}
headerText={t( "DISCARD-SOUND-header" )}
text={t( "By-exiting-your-recorded-sound-will-not-be-saved" )}
confirm={( ) => navigation.goBack( )}
confirm={onBack}
handleClose={( ) => setExitWarningShown( false )}
buttonText={t( "DISCARD-RECORDING" )}
/>
Expand Down
11 changes: 11 additions & 0 deletions src/realmModels/Observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ class Observation extends Realm.Object {
return updatedObs;
};

static appendObsSounds = ( obsSounds, currentObservation ) => {
const updatedObs = currentObservation;

// need empty case for when a user creates an observation with no sounds,
// then tries to add sounds to observation later
const currentObservationSounds = updatedObs?.observationSounds || [];

updatedObs.observationSounds = [...currentObservationSounds, ...obsSounds];
return updatedObs;
};

static schema = {
name: "Observation",
primaryKey: "uuid",
Expand Down
Loading