diff --git a/app/index.tsx b/app/index.tsx index e80c05c..2da8b34 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -1,10 +1,11 @@ -import React, { useState } from "react"; -import { ScrollView, Text, Alert, Button } from "react-native"; +import React, { useState, useEffect } from "react"; +import { ScrollView, Alert, Button } from "react-native"; 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 { saveState, loadState } from "@/components/CalculatorState"; export enum COLORS { "white", @@ -20,32 +21,50 @@ const IndexScreen: React.FC = () => { const [numberOfChips, setNumberOfChips] = useState(5); const [totalChipsCount, setTotalChipsCount] = useState([]); - const handleSave = () => { + useEffect(() => { + const loadSavedState = async () => { + const savedState = await loadState("SLOT1"); // Default loading from SLOT1 + if (savedState) { + setPlayerCount(savedState.playerCount); + setBuyInAmount(savedState.buyInAmount); + setNumberOfChips(savedState.numberOfChips); + setTotalChipsCount(savedState.totalChipsCount); + } + }; + loadSavedState(); + }, []); + + const handleSave = async (slot: "SLOT1" | "SLOT2") => { if (buyInAmount === null) { Alert.alert("Error", "Please select a valid buy-in amount"); + return; + } + const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount }; + const result = await saveState(slot, state); + Alert.alert(result.success ? "Success" : "Error", result.message); + }; + + const handleLoad = async (slot: "SLOT1" | "SLOT2") => { + const loadedState = await loadState(slot); + if (loadedState) { + setPlayerCount(loadedState.playerCount); + setBuyInAmount(loadedState.buyInAmount); + setNumberOfChips(loadedState.numberOfChips); + setTotalChipsCount(loadedState.totalChipsCount); + Alert.alert("Success", `State loaded from ${slot}`); } else { - Alert.alert( - "Success", - `Buy-in amount set to ${buyInAmount} for ${playerCount} players` - ); + Alert.alert("Info", "No saved state found"); } }; - // Update chip count based on detection or manual edit const updateChipCount = (chipData: { [color: string]: number }) => { - // Convert the chip data from the API response or manual edit to a count array - const chipCountArray = Object.entries(chipData).map( - ([color, count]) => count - ); - setTotalChipsCount(chipCountArray); // Update the parent component's state + const chipCountArray = Object.values(chipData); + setTotalChipsCount(chipCountArray); }; return ( - + { buyInAmount={buyInAmount} totalChipsCount={totalChipsCount} /> -