From 9952ea3687dbb181451b25607f45276fa04d3293 Mon Sep 17 00:00:00 2001 From: MantashaNoyela Date: Tue, 25 Feb 2025 20:33:33 -0800 Subject: [PATCH] Ability to save and load calculator states into two slots --- app/index.tsx | 105 +++++++++--------- components/CalculatorState.tsx | 31 ++++++ components/__tests__/CalculatorState.test.tsx | 52 +++++++++ package-lock.json | 39 ++++++- package.json | 3 +- 5 files changed, 176 insertions(+), 54 deletions(-) create mode 100644 components/CalculatorState.tsx create mode 100644 components/__tests__/CalculatorState.test.tsx diff --git a/app/index.tsx b/app/index.tsx index e80c05c..1620b18 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -1,71 +1,76 @@ import React, { useState } from "react"; -import { ScrollView, Text, Alert, Button } from "react-native"; +import { ScrollView, Text, Alert, Button, View, StyleSheet } 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", - "red", - "green", - "blue", - "black", -} - -const IndexScreen: React.FC = () => { +const IndexScreen = () => { const [playerCount, setPlayerCount] = useState(2); - const [buyInAmount, setBuyInAmount] = useState(20); + const [buyInAmount, setBuyInAmount] = useState(null); const [numberOfChips, setNumberOfChips] = useState(5); const [totalChipsCount, setTotalChipsCount] = useState([]); - const handleSave = () => { + 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 - }; - return ( - - - - - - -