From bfd2bdad45ac994007b35eacc5c42cdd018b717e Mon Sep 17 00:00:00 2001 From: Tim Glasgow Date: Sat, 6 Aug 2022 17:52:06 -0500 Subject: [PATCH] load all scores on highscore screen --- squarenotsquare/src/realm/DbAPI.js | 4 ++++ squarenotsquare/src/realm/repos/ScoreRepo.js | 9 +++++++++ squarenotsquare/src/redux/actions/UserActions.js | 2 +- squarenotsquare/src/screens/HighScore.js | 4 +++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/squarenotsquare/src/realm/DbAPI.js b/squarenotsquare/src/realm/DbAPI.js index fc6e746..5e14479 100644 --- a/squarenotsquare/src/realm/DbAPI.js +++ b/squarenotsquare/src/realm/DbAPI.js @@ -30,6 +30,10 @@ class DatabaseAPI { return true; } + getHighScores() { + return this.scoreRepo.getHighScores(); + } + async initDB() { let repos = await initDB(); this.systemRepo = repos.systemRepo; diff --git a/squarenotsquare/src/realm/repos/ScoreRepo.js b/squarenotsquare/src/realm/repos/ScoreRepo.js index d1e70d7..60fe8b6 100644 --- a/squarenotsquare/src/realm/repos/ScoreRepo.js +++ b/squarenotsquare/src/realm/repos/ScoreRepo.js @@ -24,4 +24,13 @@ export default class ScoreRepo { ); }); }; + + getHighScores = () => { + let score = this.realmDB.objects(ScoreEntity.name); + let highScores = []; + score.forEach((row) => { + highScores.push({user: row.user, value: row.value}); + }); + return highScores; + }; } \ No newline at end of file diff --git a/squarenotsquare/src/redux/actions/UserActions.js b/squarenotsquare/src/redux/actions/UserActions.js index 66d0c40..4bc9444 100644 --- a/squarenotsquare/src/redux/actions/UserActions.js +++ b/squarenotsquare/src/redux/actions/UserActions.js @@ -21,7 +21,7 @@ export function squareFinished(answers, finalTime) { newUser.lastGameAnswers = answers; let finalScore = calculateSquareScore(answers, finalTime); - DbAPI.createScore(userState.username, finalScore); + DbAPI.createScore(userState.username, finalScore.toString()); dispatch({ type: SQUARE_FINISHED, user: newUser diff --git a/squarenotsquare/src/screens/HighScore.js b/squarenotsquare/src/screens/HighScore.js index c251e56..b2ce8d7 100644 --- a/squarenotsquare/src/screens/HighScore.js +++ b/squarenotsquare/src/screens/HighScore.js @@ -1,12 +1,14 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useRef } from "react"; import { BackHandler, SafeAreaView, Text, TouchableOpacity, View } from "react-native"; import { styles } from './styles/AppStyles'; import Slider from "../components/Slider"; import { useDispatch } from "react-redux"; import {goHome} from '../redux/actions/SystemActions'; +import DbAPI from "../realm/DbAPI"; function HighScore(){ const dispatch = useDispatch(); + const scores = useRef(DbAPI.getHighScores()); useEffect(() => { BackHandler.addEventListener('hardwareBackPress', () => {return true});