import { Ionicons } from "@expo/vector-icons"; import { useRouter } from "expo-router"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { ActivityIndicator, Alert, Pressable, ScrollView, Switch, Text, TextInput, View, } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { useHouseholdSettings, useUpdateHouseholdSettings } from "@/src/hooks/useHouseholdSettings"; import { useHouseholdMembers } from "@/src/hooks/useHouseholdMembers"; const ACCENT = "#2563EB"; const SHARE_PRESETS = [50, 60, 75, 100]; function SettingsRow({ label, value, onPress, }: { label: string; value: string; onPress: () => void; }) { return ( {label} {value} ); } function EditModal({ title, initialValue, keyboardType, onSave, onClose, }: { title: string; initialValue: string; keyboardType?: "default" | "decimal-pad"; onSave: (value: string) => void; onClose: () => void; }) { const [value, setValue] = useState(initialValue); const { t } = useTranslation(); return ( {title} {t('common.cancel')} { onSave(value); onClose(); }} className="flex-1 py-3 rounded-xl items-center active:opacity-70" style={{ backgroundColor: ACCENT }} > {t('common.save')} ); } type EditingField = "ownerName" | "partnerName" | "monthlyBudget" | "userSharePercent" | null; export default function HouseholdSettingsScreen() { const insets = useSafeAreaInsets(); const router = useRouter(); const { t } = useTranslation(); const { data: settings, isLoading } = useHouseholdSettings(); const { data: membersData } = useHouseholdMembers(); const { mutate: update, isPending } = useUpdateHouseholdSettings(); const [editing, setEditing] = useState(null); const members = membersData?.members ?? []; function save(input: Parameters[0]) { update(input, { onError: () => Alert.alert(t('common.error'), t('settings.saveError')), }); } if (isLoading || !settings) { return ( ); } return ( router.push("/(app)/settings")} className="mr-3 p-1"> {t('settings.household.title')} {isPending && } {/* Namen */} {t('settings.household.namesSection')} setEditing("ownerName")} /> setEditing("partnerName")} /> {/* Wer zahlt die Ausgaben vor? */} {members.length > 1 && ( {t('settings.household.payerSection')} {t('settings.household.payerHint')} {members.map((m) => { const isSelected = settings.payerUserId === m.userId; return ( save({ payerUserId: m.userId })} className="flex-1 py-2.5 rounded-xl items-center" style={{ backgroundColor: isSelected ? ACCENT : "#f3f4f6" }} > {m.name} ); })} )} {/* Kostenaufteilung */} {t('settings.household.costSplitSection')} {t('settings.household.costSplitHint')} {SHARE_PRESETS.map((p) => ( save({ userSharePercent: p })} className="flex-1 py-2.5 rounded-xl items-center" style={{ backgroundColor: settings.userSharePercent === p ? ACCENT : "#f3f4f6" }} > {p}% ))} {t('settings.household.sharePreview', { own: settings.userSharePercent, partner: settings.partnerName, rest: 100 - settings.userSharePercent })} setEditing("monthlyBudget")} /> {t('settings.household.splitChildren')} save({ splitChildCosts: v })} trackColor={{ false: "#d1d5db", true: ACCENT }} thumbColor="#fff" /> {/* Währung */} {t('settings.household.settingsSection')} Alert.alert(t('settings.household.currency'), t('settings.household.currencyOnlyEur')) } /> {/* Inline Edit Modals */} {editing === "ownerName" && ( save({ ownerName: v.trim() || "Ich" })} onClose={() => setEditing(null)} /> )} {editing === "partnerName" && ( save({ partnerName: v.trim() || "Partner" })} onClose={() => setEditing(null)} /> )} {editing === "monthlyBudget" && ( save({ monthlyBudget: parseFloat(v.replace(",", ".")) || 400 })} onClose={() => setEditing(null)} /> )} ); }