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
48 changes: 47 additions & 1 deletion app/(tabs)/new-dose.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import { View, Text, StyleSheet, TouchableOpacity, Alert } from 'react-native';
import { CameraView, useCameraPermissions } from 'expo-camera';
import OpenAI from 'openai';
import Constants from 'expo-constants';
Expand Down Expand Up @@ -151,6 +151,51 @@ export default function NewDoseScreen() {

doseCalculator.setScreenStep(step);
}, [doseCalculator, setNavigatingFromIntro]);

const handleUseLastDose = useCallback(async () => {
try {
const history = await getDoseLogHistory();
if (!history || history.length === 0) {
Alert.alert('No Saved Doses', 'You have no previous doses to reuse.');
return;
}

const last = history[0];

setNavigatingFromIntro(true);

resetFullForm('finalResult');

setDose(last.doseValue.toString());
setDoseValue(last.doseValue);
setUnit(last.unit as any);
setSubstanceName(last.substanceName);
setCalculatedVolume(last.calculatedVolume);
setRecommendedMarking(last.recommendedMarking || null);

setManualSyringe({
type: last.syringeType || 'Standard',
volume: last.syringeType === 'Insulin' ? '1 ml' : '3 ml',
});

if (last.calculatedVolume) {
const conc = last.doseValue / last.calculatedVolume;
setConcentration(conc);
setCalculatedConcentration(conc);
setConcentrationAmount(conc.toString());
setMedicationInputType('concentration');
if (last.unit === 'mg') setConcentrationUnit('mg/ml');
else if (last.unit === 'mcg') setConcentrationUnit('mcg/ml');
else if (last.unit === 'units') setConcentrationUnit('units/ml');
}

setManualStep('finalResult');
doseCalculator.setScreenStep('manualEntry');
} catch (error) {
console.error('Error applying last dose', error);
Alert.alert('Error', 'Failed to load last dose');
}
}, [getDoseLogHistory, resetFullForm, setNavigatingFromIntro, setDose, setDoseValue, setUnit, setSubstanceName, setCalculatedVolume, setRecommendedMarking, setManualSyringe, setConcentration, setCalculatedConcentration, setConcentrationAmount, setMedicationInputType, setConcentrationUnit, setManualStep, doseCalculator]);

// Handle screen focus events to ensure state is properly initialized after navigation
useFocusEffect(
Expand Down Expand Up @@ -809,6 +854,7 @@ export default function NewDoseScreen() {
setScreenStep={handleSetScreenStep}
resetFullForm={resetFullForm}
setNavigatingFromIntro={setNavigatingFromIntro}
onUseLastDose={handleUseLastDose}
/>
)}
{screenStep === 'scan' && (
Expand Down
18 changes: 18 additions & 0 deletions components/IntroScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
LogOut,
Info,
User,
RotateCcw,
} from 'lucide-react-native';
import Animated, { FadeIn } from 'react-native-reanimated';
import { isMobileWeb } from '../lib/utils';
Expand All @@ -34,12 +35,14 @@ interface IntroScreenProps {
| 'finalResult',
) => void;
setNavigatingFromIntro?: (value: boolean) => void;
onUseLastDose?: () => void;
}

export default function IntroScreen({
setScreenStep,
resetFullForm,
setNavigatingFromIntro,
onUseLastDose,
}: IntroScreenProps) {
const { user, auth, logout, isSigningOut } = useAuth();
const { disclaimerText, profile, isLoading } = useUserProfile();
Expand Down Expand Up @@ -216,6 +219,10 @@ export default function IntroScreen({
setScreenStep('manualEntry');
}, [resetFullForm, setScreenStep, setNavigatingFromIntro]);

const handleUseLastDosePress = useCallback(() => {
onUseLastDose?.();
}, [onUseLastDose]);

/* =========================================================================
RENDER
========================================================================= */
Expand Down Expand Up @@ -296,6 +303,17 @@ export default function IntroScreen({
<Pill color="#fff" size={20} />
<Text style={styles.buttonText}>Manual</Text>
</TouchableOpacity>
<TouchableOpacity
style={[
styles.button,
styles.secondaryButton,
isMobileWeb && styles.buttonMobile,
]}
onPress={handleUseLastDosePress}
>
<RotateCcw color="#fff" size={20} />
<Text style={styles.buttonText}>Use Last Dose</Text>
</TouchableOpacity>
</View>

{/* Plan Reconstitution Link */}
Expand Down