Merge pull request #38 from djwesty/MantashaNoyela/27
Restores the previous state when reopened
This commit is contained in:
commit
25dc61006b
@ -1,11 +1,12 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
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 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 ChipDetection from "@/components/ChipDetection";
|
import ChipDetection from "@/components/ChipDetection";
|
||||||
import { saveState, loadState } from "@/components/CalculatorState";
|
import { saveState, loadState } from "@/components/CalculatorState";
|
||||||
|
import { savePersistentState, loadPersistentState } from "@/components/PersistentState";
|
||||||
|
|
||||||
export enum COLORS {
|
export enum COLORS {
|
||||||
"white",
|
"white",
|
||||||
@ -21,52 +22,76 @@ const IndexScreen: React.FC = () => {
|
|||||||
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 loadSavedState = async () => {
|
const loadPersistentData = async () => {
|
||||||
const savedState = await loadState("SLOT1"); // Default loading from SLOT1
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadSavedState();
|
loadPersistentData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Save game state to selected slot
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleLoad = async (slot: "SLOT1" | "SLOT2") => {
|
try {
|
||||||
const loadedState = await loadState(slot);
|
await saveState(slot, state);
|
||||||
if (loadedState) {
|
await savePersistentState(state);
|
||||||
setPlayerCount(loadedState.playerCount);
|
console.log(`Game state saved to ${slot}:`, state);
|
||||||
setBuyInAmount(loadedState.buyInAmount);
|
Alert.alert("Success", `State saved to ${slot}`);
|
||||||
setNumberOfChips(loadedState.numberOfChips);
|
} catch (error) {
|
||||||
setTotalChipsCount(loadedState.totalChipsCount);
|
console.error("Error saving state:", error);
|
||||||
Alert.alert("Success", `State loaded from ${slot}`);
|
Alert.alert("Error", "Failed to save state.");
|
||||||
} else {
|
|
||||||
Alert.alert("Info", "No saved state found");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateChipCount = (chipData: { [color: string]: number }) => {
|
// Load game state from selected slot
|
||||||
const chipCountArray = Object.values(chipData);
|
const handleLoad = async (slot: "SLOT1" | "SLOT2") => {
|
||||||
setTotalChipsCount(chipCountArray);
|
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.");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error loading state:", error);
|
||||||
|
Alert.alert("Error", "Failed to load state.");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ScrollView contentContainerStyle={{ padding: 20, flexGrow: 1 }}>
|
<ScrollView contentContainerStyle={{ padding: 20, flexGrow: 1 }}>
|
||||||
<PlayerSelector playerCount={playerCount} setPlayerCount={setPlayerCount} />
|
<PlayerSelector playerCount={playerCount} setPlayerCount={setPlayerCount} />
|
||||||
<BuyInSelector setBuyInAmount={setBuyInAmount} />
|
<BuyInSelector setBuyInAmount={setBuyInAmount} />
|
||||||
<ChipDetection updateChipCount={updateChipCount} />
|
<ChipDetection updateChipCount={(chipData) => {
|
||||||
|
const chipCountArray = Object.values(chipData);
|
||||||
|
setTotalChipsCount(chipCountArray);
|
||||||
|
}} />
|
||||||
<ChipsSelector
|
<ChipsSelector
|
||||||
totalChipsCount={totalChipsCount}
|
totalChipsCount={totalChipsCount}
|
||||||
setTotalChipsCount={setTotalChipsCount}
|
setTotalChipsCount={setTotalChipsCount}
|
||||||
@ -86,4 +111,4 @@ const IndexScreen: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default IndexScreen;
|
export default IndexScreen;
|
28
components/PersistentState.tsx
Normal file
28
components/PersistentState.tsx
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
53
components/__tests__/PersistentState.test.tsx
Normal file
53
components/__tests__/PersistentState.test.tsx
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user