import React, { useState } from 'react'; import { View, Text, StyleSheet, TouchableOpacity, TextInput, ScrollView, Image, Alert, KeyboardAvoidingView, Platform, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import * as ImagePicker from 'expo-image-picker'; import { AppColors, Spacing, BorderRadius, FontSizes, FontWeights } from '@/constants/theme'; import { useBeneficiary } from '@/contexts/BeneficiaryContext'; import { Button } from '@/components/ui/Button'; export default function AddBeneficiaryScreen() { const { addLocalBeneficiary } = useBeneficiary(); const [name, setName] = useState(''); const [address, setAddress] = useState(''); const [avatarUri, setAvatarUri] = useState(null); const [isLoading, setIsLoading] = useState(false); const handlePickAvatar = async () => { const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync(); if (status !== 'granted') { Alert.alert('Permission needed', 'Please allow access to your photo library.'); return; } const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ['images'], allowsEditing: true, aspect: [1, 1], quality: 0.5, }); if (!result.canceled && result.assets[0]) { setAvatarUri(result.assets[0].uri); } }; const handleTakePhoto = async () => { const { status } = await ImagePicker.requestCameraPermissionsAsync(); if (status !== 'granted') { Alert.alert('Permission needed', 'Please allow access to your camera.'); return; } const result = await ImagePicker.launchCameraAsync({ allowsEditing: true, aspect: [1, 1], quality: 0.5, }); if (!result.canceled && result.assets[0]) { setAvatarUri(result.assets[0].uri); } }; const handleAvatarPress = () => { Alert.alert( 'Add Photo', 'Choose how to add a photo', [ { text: 'Take Photo', onPress: handleTakePhoto }, { text: 'Choose from Library', onPress: handlePickAvatar }, { text: 'Cancel', style: 'cancel' }, ] ); }; const handleSave = async () => { if (!name.trim()) { Alert.alert('Name Required', 'Please enter a name for your loved one.'); return; } setIsLoading(true); try { const newBeneficiary = await addLocalBeneficiary({ name: name.trim(), address: address.trim() || undefined, avatar: avatarUri || undefined, }); // Navigate to purchase flow for this beneficiary router.replace({ pathname: '/(auth)/purchase', params: { lovedOneName: name.trim(), beneficiaryId: newBeneficiary.id.toString(), }, }); } catch (error) { Alert.alert('Error', 'Failed to add beneficiary. Please try again.'); } finally { setIsLoading(false); } }; const nameInitial = name.trim() ? name.trim().charAt(0).toUpperCase() : '+'; return ( {/* Header */} router.back()}> Add Loved One {/* Avatar Section */} {avatarUri ? ( ) : ( {nameInitial} )} Tap to add photo {/* Form */} Name * Address (optional) {/* Info Box */} Next Step After adding, you can activate sensors to start monitoring wellness data. {/* Save Button */}