Merge pull request #38 from djwesty/MantashaNoyela/27

Restores the previous state when reopened
This commit is contained in:
Mantasha Altab Noyela 2025-02-26 22:01:48 -08:00 committed by GitHub
commit 25dc61006b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 133 additions and 27 deletions

View File

@ -1,11 +1,12 @@
import React, { useState, useEffect } from "react";
import { ScrollView, Alert, Button } from "react-native";
import { ScrollView, Text, 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";
import { savePersistentState, loadPersistentState } from "@/components/PersistentState";
export enum COLORS {
"white",
@ -21,52 +22,76 @@ const IndexScreen: React.FC = () => {
const [numberOfChips, setNumberOfChips] = useState<number>(5);
const [totalChipsCount, setTotalChipsCount] = useState<number[]>([]);
// Load persistent data on startup
useEffect(() => {
const loadSavedState = async () => {
const savedState = await loadState("SLOT1"); // Default loading from SLOT1
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);
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);
}
};
loadSavedState();
loadPersistentData();
}, []);
// Save game state to selected slot
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);
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") => {
try {
const loadedState = await loadState(slot);
if (loadedState) {
setPlayerCount(loadedState.playerCount);
setBuyInAmount(loadedState.buyInAmount);
setNumberOfChips(loadedState.numberOfChips);
setTotalChipsCount(loadedState.totalChipsCount);
await savePersistentState(loadedState);
console.log(`Game state loaded from ${slot}:`, loadedState);
Alert.alert("Success", `State loaded from ${slot}`);
} else {
Alert.alert("Info", "No saved state found");
Alert.alert("Info", "No saved state found.");
}
} catch (error) {
console.error("Error loading state:", error);
Alert.alert("Error", "Failed to load state.");
}
};
const updateChipCount = (chipData: { [color: string]: number }) => {
const chipCountArray = Object.values(chipData);
setTotalChipsCount(chipCountArray);
};
return (
<ScrollView contentContainerStyle={{ padding: 20, flexGrow: 1 }}>
<PlayerSelector playerCount={playerCount} setPlayerCount={setPlayerCount} />
<BuyInSelector setBuyInAmount={setBuyInAmount} />
<ChipDetection updateChipCount={updateChipCount} />
<ChipDetection updateChipCount={(chipData) => {
const chipCountArray = Object.values(chipData);
setTotalChipsCount(chipCountArray);
}} />
<ChipsSelector
totalChipsCount={totalChipsCount}
setTotalChipsCount={setTotalChipsCount}

View File

@ -0,0 +1,28 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
const STORAGE_KEY = "@poker_calculator_state";
export interface PokerState {
playerCount: number;
buyInAmount: number | null;
numberOfChips: number;
totalChipsCount: number[];
}
export const savePersistentState = async (state: PokerState) => {
try {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(state));
return { success: true, message: "State saved successfully" };
} catch (error) {
return { success: false, message: "Failed to save state" };
}
};
export const loadPersistentState = async (): Promise<PokerState | null> => {
try {
const storedState = await AsyncStorage.getItem(STORAGE_KEY);
return storedState ? JSON.parse(storedState) : null;
} catch (error) {
return null;
}
};

View File

@ -0,0 +1,53 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import { saveState, loadState, PokerState } from "../CalculatorState";
jest.mock("@react-native-async-storage/async-storage", () => ({
setItem: jest.fn(),
getItem: jest.fn(),
}));
describe("CalculatorState.tsx", () => {
const mockState: PokerState = {
playerCount: 4,
buyInAmount: 50,
numberOfChips: 5,
totalChipsCount: [100, 200, 150, 50, 75],
};
beforeEach(() => {
jest.clearAllMocks();
});
it("should save state successfully", async () => {
(AsyncStorage.setItem as jest.Mock).mockResolvedValueOnce(undefined);
const result = await saveState("SLOT1", mockState);
expect(result.success).toBe(true);
expect(result.message).toBe("State saved to SLOT1");
expect(AsyncStorage.setItem).toHaveBeenCalledWith("@poker_state_slot1", JSON.stringify(mockState));
});
it("should fail to save state if an error occurs", async () => {
(AsyncStorage.setItem as jest.Mock).mockRejectedValueOnce(new Error("Failed to save"));
const result = await saveState("SLOT1", mockState);
expect(result.success).toBe(false);
expect(result.message).toBe("Failed to save state");
});
it("should load state successfully", async () => {
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(JSON.stringify(mockState));
const result = await loadState("SLOT1");
expect(result).toEqual(mockState);
});
it("should return null if no state is found", async () => {
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(null);
const result = await loadState("SLOT1");
expect(result).toBeNull();
});
it("should return null if an error occurs while loading state", async () => {
(AsyncStorage.getItem as jest.Mock).mockRejectedValueOnce(new Error("Failed to load"));
const result = await loadState("SLOT1");
expect(result).toBeNull();
});
});