Merge branch 'main' into MantashaNoyela/27

This commit is contained in:
Mantasha Altab Noyela 2025-02-25 23:54:00 -08:00 committed by GitHub
commit 1ebdaf5c9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 81 additions and 62 deletions

View File

@ -1,95 +1,114 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { ScrollView, Text, Alert, Button, View, StyleSheet } from "react-native"; import { ScrollView, Text, Alert, Button } from "react-native";
import PlayerSelector from "@/components/PlayerSelector"; import PlayerSelector from "@/components/PlayerSelector";
import BuyInSelector from "@/components/BuyInSelector"; import BuyInSelector from "@/components/BuyInSelector";
import ChipsSelector from "@/components/ChipsSelector"; import ChipsSelector from "@/components/ChipsSelector";
import ChipDistributionSummary from "@/components/ChipDistributionSummary"; import ChipDistributionSummary from "@/components/ChipDistributionSummary";
import { saveState, loadState } from "../components/CalculatorState"; import ChipDetection from "@/components/ChipDetection";
import { savePersistentState, loadPersistentState } from "../components/PersistentState"; import { saveState, loadState } from "@/components/CalculatorState";
import { savePersistentState, loadPersistentState } from "@/components/PersistentState";
const IndexScreen = () => { export enum COLORS {
"white",
"red",
"green",
"blue",
"black",
}
const IndexScreen: React.FC = () => {
const [playerCount, setPlayerCount] = useState(2); const [playerCount, setPlayerCount] = useState(2);
const [buyInAmount, setBuyInAmount] = useState<number | null>(null); const [buyInAmount, setBuyInAmount] = useState<number>(20);
const [numberOfChips, setNumberOfChips] = useState<number>(5); const [numberOfChips, setNumberOfChips] = useState<number>(5);
const [totalChipsCount, setTotalChipsCount] = useState<number[]>([]); const [totalChipsCount, setTotalChipsCount] = useState<number[]>([]);
// Load persistent data on startup
useEffect(() => { useEffect(() => {
const loadPersistentData = async () => { const loadPersistentData = async () => {
const savedState = await loadPersistentState(); try {
if (savedState) { console.log("Loading persistent game state...");
setPlayerCount(savedState.playerCount); const savedState = await loadPersistentState();
setBuyInAmount(savedState.buyInAmount); if (savedState) {
setNumberOfChips(savedState.numberOfChips); console.log("Persistent state restored:", savedState);
setTotalChipsCount(savedState.totalChipsCount); setPlayerCount(savedState.playerCount);
setBuyInAmount(savedState.buyInAmount);
setNumberOfChips(savedState.numberOfChips);
setTotalChipsCount(savedState.totalChipsCount);
} else {
console.log("No persistent state found.");
}
} catch (error) {
console.error("Error loading persistent state:", error);
} }
}; };
loadPersistentData(); loadPersistentData();
}, []); }, []);
useEffect(() => { // Save game state to selected slot
const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount };
savePersistentState(state);
}, [playerCount, buyInAmount, numberOfChips, totalChipsCount]);
const handleSave = async (slot: "SLOT1" | "SLOT2") => { const handleSave = async (slot: "SLOT1" | "SLOT2") => {
if (buyInAmount === null) { if (buyInAmount === null) {
Alert.alert("Error", "Please select a valid buy-in amount"); Alert.alert("Error", "Please select a valid buy-in amount");
return; return;
} }
const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount }; const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount };
const result = await saveState(slot, state);
Alert.alert(result.success ? "Success" : "Error", result.message); try {
await saveState(slot, state);
await savePersistentState(state);
console.log(`Game state saved to ${slot}:`, state);
Alert.alert("Success", `State saved to ${slot}`);
} catch (error) {
console.error("Error saving state:", error);
Alert.alert("Error", "Failed to save state.");
}
}; };
// Load game state from selected slot
const handleLoad = async (slot: "SLOT1" | "SLOT2") => { const handleLoad = async (slot: "SLOT1" | "SLOT2") => {
const loadedState = await loadState(slot); try {
if (loadedState) { const loadedState = await loadState(slot);
setPlayerCount(loadedState.playerCount); if (loadedState) {
setBuyInAmount(loadedState.buyInAmount); setPlayerCount(loadedState.playerCount);
setNumberOfChips(loadedState.numberOfChips); setBuyInAmount(loadedState.buyInAmount);
setTotalChipsCount(loadedState.totalChipsCount); setNumberOfChips(loadedState.numberOfChips);
Alert.alert("Success", `State loaded from ${slot}`); setTotalChipsCount(loadedState.totalChipsCount);
} else { await savePersistentState(loadedState);
Alert.alert("Info", "No saved state found"); console.log(`Game state loaded from ${slot}:`, loadedState);
Alert.alert("Success", `State loaded from ${slot}`);
} else {
Alert.alert("Info", "No saved state found.");
}
} catch (error) {
console.error("Error loading state:", error);
Alert.alert("Error", "Failed to load state.");
} }
}; };
return ( return (
<View style={styles.container}> <ScrollView contentContainerStyle={{ padding: 20, flexGrow: 1 }}>
<ScrollView contentContainerStyle={{ padding: 20, flexGrow: 1 }}> <PlayerSelector playerCount={playerCount} setPlayerCount={setPlayerCount} />
<Text style={styles.title}>Poker Chip Helper</Text> <BuyInSelector setBuyInAmount={setBuyInAmount} />
<PlayerSelector playerCount={playerCount} setPlayerCount={setPlayerCount} /> <ChipDetection updateChipCount={(chipData) => {
<BuyInSelector setBuyInAmount={setBuyInAmount} /> const chipCountArray = Object.values(chipData);
<ChipsSelector setTotalChipsCount(chipCountArray);
totalChipsCount={totalChipsCount} }} />
setTotalChipsCount={setTotalChipsCount} <ChipsSelector
numberOfChips={numberOfChips} totalChipsCount={totalChipsCount}
setNumberOfChips={setNumberOfChips} setTotalChipsCount={setTotalChipsCount}
/> numberOfChips={numberOfChips}
<ChipDistributionSummary setNumberOfChips={setNumberOfChips}
playerCount={playerCount} />
buyInAmount={buyInAmount} <ChipDistributionSummary
totalChipsCount={totalChipsCount} playerCount={playerCount}
/> buyInAmount={buyInAmount}
<Button title="Save to Slot 1" onPress={() => handleSave("SLOT1")} disabled={buyInAmount === null} /> totalChipsCount={totalChipsCount}
<Button title="Save to Slot 2" onPress={() => handleSave("SLOT2")} disabled={buyInAmount === null} /> />
<Button title="Load from Slot 1" onPress={() => handleLoad("SLOT1")} /> <Button title="Save to Slot 1" onPress={() => handleSave("SLOT1")} disabled={buyInAmount === null} />
<Button title="Load from Slot 2" onPress={() => handleLoad("SLOT2")} /> <Button title="Save to Slot 2" onPress={() => handleSave("SLOT2")} disabled={buyInAmount === null} />
</ScrollView> <Button title="Load from Slot 1" onPress={() => handleLoad("SLOT1")} />
</View> <Button title="Load from Slot 2" onPress={() => handleLoad("SLOT2")} />
</ScrollView>
); );
}; };
const styles = StyleSheet.create({
container: {
flex: 1,
},
title: {
fontSize: 24,
marginBottom: 30,
marginTop: 50,
textAlign: "center",
},
});
export default IndexScreen; export default IndexScreen;

View File

@ -22,7 +22,7 @@
"@react-native-async-storage/async-storage": "^2.1.1", "@react-native-async-storage/async-storage": "^2.1.1",
"@react-navigation/bottom-tabs": "7.2.0", "@react-navigation/bottom-tabs": "7.2.0",
"@react-navigation/native": "7.0.14", "@react-navigation/native": "7.0.14",
"expo": "^52.0.37", "expo": "52.0.37",
"expo-blur": "14.0.3", "expo-blur": "14.0.3",
"expo-constants": "17.0.7", "expo-constants": "17.0.7",
"expo-file-system": "18.0.11", "expo-file-system": "18.0.11",