import React, { useState, useEffect, useContext, useMemo } from "react"; import { ScrollView, Alert } from "react-native"; import Button from "@/containers/Button"; import PlayerSelector from "@/components/PlayerSelector"; import BuyInSelector from "@/components/BuyInSelector"; import ChipsSelector from "@/components/ChipsSelector"; import ChipDistributionSummary from "@/components/ChipDistributionSummary"; import ChipDetection from "@/components/ChipDetection"; import CurrencySelector from "@/components/CurrencySelector"; import { saveState, loadState } from "@/components/CalculatorState"; import { savePersistentState, loadPersistentState, } from "@/components/PersistentState"; import styles from "@/styles/styles"; import Section from "@/containers/Section"; import AppContext from "@/util/context"; import { Picker } from "@react-native-picker/picker"; import i18n from "@/i18n/i18n"; const IndexScreen: React.FC = () => { const [playerCount, setPlayerCount] = useState(2); const [buyInAmount, setBuyInAmount] = useState(20); const [numberOfChips, setNumberOfChips] = useState(5); const [totalChipsCount, setTotalChipsCount] = useState([]); const [selectedCurrency, setSelectedCurrency] = useState("$"); const [selectedLanguage, setSelectedLanguage] = useState("en"); const context = useContext(AppContext); const isSettingsVisible = useMemo(() => context.showSettings, [context]); useEffect(() => { const loadPersistentData = async () => { try { console.log("Loading persistent game state..."); const savedState = await loadPersistentState(); if (savedState) { console.log("Persistent state restored:", savedState); setPlayerCount(savedState.playerCount || 2); setBuyInAmount(savedState.buyInAmount || 20); setNumberOfChips(savedState.numberOfChips || 5); setTotalChipsCount(savedState.totalChipsCount || []); setSelectedCurrency(savedState.selectedCurrency || "$"); } else { console.log("No persistent state found, using defaults."); } } catch (error) { console.error("Error loading persistent state:", error); } }; loadPersistentData(); }, []); const handleSave = async (slot: "SLOT1" | "SLOT2") => { if (buyInAmount === null) { Alert.alert(i18n.t("error"), i18n.t("please_select_valid_buyin")); return; } const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount, selectedCurrency, }; try { await saveState(slot, state); await savePersistentState(state); console.log(`Game state saved to ${slot}:`, state); Alert.alert(i18n.t("success"), i18n.t("state_saved", { slot })); // Fixed interpolation } catch (error) { console.error("Error saving state:", error); Alert.alert(i18n.t("error"), i18n.t("failed_to_save_state")); } }; const handleLoad = async (slot: "SLOT1" | "SLOT2") => { try { const loadedState = await loadState(slot); if (loadedState) { setPlayerCount(loadedState.playerCount); setBuyInAmount(loadedState.buyInAmount); setNumberOfChips(loadedState.numberOfChips); setTotalChipsCount(loadedState.totalChipsCount); setSelectedCurrency(loadedState.selectedCurrency || "$"); await savePersistentState(loadedState); console.log(`Game state loaded from ${slot}:`, loadedState); Alert.alert(i18n.t("success"), i18n.t("state_loaded_from", { slot })); // Fixed interpolation } else { Alert.alert(i18n.t("info"), i18n.t("no_saved_state_found")); } } catch (error) { console.error("Error loading state:", error); Alert.alert(i18n.t("error"), i18n.t("failed_to_load_state")); } }; const handleLanguageChange = (language: string) => { setSelectedLanguage(language); i18n.changeLanguage(language); }; return ( {isSettingsVisible && (
)} {isSettingsVisible && (
)}
{ const chipCountArray = Object.values(chipData); setTotalChipsCount(chipCountArray); }} />
<>
); }; export default IndexScreen;