Browse Source

load all scores on highscore screen

pull/8/head
Tim Glasgow 2 years ago
parent
commit
bfd2bdad45
  1. 4
      squarenotsquare/src/realm/DbAPI.js
  2. 9
      squarenotsquare/src/realm/repos/ScoreRepo.js
  3. 2
      squarenotsquare/src/redux/actions/UserActions.js
  4. 4
      squarenotsquare/src/screens/HighScore.js

4
squarenotsquare/src/realm/DbAPI.js

@ -30,6 +30,10 @@ class DatabaseAPI {
return true; return true;
} }
getHighScores() {
return this.scoreRepo.getHighScores();
}
async initDB() { async initDB() {
let repos = await initDB(); let repos = await initDB();
this.systemRepo = repos.systemRepo; this.systemRepo = repos.systemRepo;

9
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;
};
} }

2
squarenotsquare/src/redux/actions/UserActions.js

@ -21,7 +21,7 @@ export function squareFinished(answers, finalTime) {
newUser.lastGameAnswers = answers; newUser.lastGameAnswers = answers;
let finalScore = calculateSquareScore(answers, finalTime); let finalScore = calculateSquareScore(answers, finalTime);
DbAPI.createScore(userState.username, finalScore); DbAPI.createScore(userState.username, finalScore.toString());
dispatch({ dispatch({
type: SQUARE_FINISHED, type: SQUARE_FINISHED,
user: newUser user: newUser

4
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 { BackHandler, SafeAreaView, Text, TouchableOpacity, View } from "react-native";
import { styles } from './styles/AppStyles'; import { styles } from './styles/AppStyles';
import Slider from "../components/Slider"; import Slider from "../components/Slider";
import { useDispatch } from "react-redux"; import { useDispatch } from "react-redux";
import {goHome} from '../redux/actions/SystemActions'; import {goHome} from '../redux/actions/SystemActions';
import DbAPI from "../realm/DbAPI";
function HighScore(){ function HighScore(){
const dispatch = useDispatch(); const dispatch = useDispatch();
const scores = useRef(DbAPI.getHighScores());
useEffect(() => { useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', () => {return true}); BackHandler.addEventListener('hardwareBackPress', () => {return true});

Loading…
Cancel
Save