From 8b2bd0522caf835f1b434b432e55266fc1053779 Mon Sep 17 00:00:00 2001 From: "luca0N\\!" Date: Wed, 17 Mar 2021 16:16:37 -0300 Subject: [PATCH 1/2] Added "Time Elapsed" field --- .../activities/TriviaGameResultsActivity.java | 15 ++ .../joguitos/pluck/trivia/TriviaGame.java | 82 +++++---- .../layout/activity_trivia_game_results.xml | 164 ++++++++++-------- app/src/main/res/values/colors.xml | 7 +- app/src/main/res/values/strings.xml | 5 +- app/src/main/res/values/styles.xml | 71 ++++---- 6 files changed, 204 insertions(+), 140 deletions(-) diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameResultsActivity.java b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameResultsActivity.java index 219c5a2..bdfc09c 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameResultsActivity.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameResultsActivity.java @@ -41,6 +41,8 @@ public class TriviaGameResultsActivity extends BaseActivity { TextView textResultsWrong; @BindView(R.id.text_results_total) TextView textResultsTotal; + @BindView(R.id.text_results_time_elapsed) + TextView textResultsTimeElapsed; @BindView(R.id.button_return_to_menu) Button buttonReturnToMenu; @@ -61,9 +63,22 @@ public class TriviaGameResultsActivity extends BaseActivity { } } + // Calculate the difference between startEpoch and endEpoch + float timeDifference = (game.getEndEpoch() - game.getStartEpoch())/1000F; + textResultsCorrect.setText(String.valueOf(correctTotal)); textResultsWrong.setText(String.valueOf(game.getQuestionsCount() - correctTotal)); textResultsTotal.setText(String.valueOf(game.getQuestionsCount())); + if (timeDifference < 60000L) + // Use the "n seconds" string since the time difference was less than 60 seconds. + textResultsTimeElapsed.setText( + String.format(getString(R.string.ui_results_time_elapsed_format_seconds), timeDifference) + ); + else + // Use the "n minutes" string since the time difference was equal to or more than 60 seconds. + textResultsTimeElapsed.setText( + String.format(getString(R.string.ui_results_time_elapsed_format_minutes), timeDifference / 60F) + ); buttonReturnToMenu.setOnClickListener(v -> finish()); } diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaGame.java b/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaGame.java index c9fbbf0..66d2ef9 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaGame.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaGame.java @@ -27,43 +27,65 @@ import java.io.Serializable; import java.util.List; public class TriviaGame implements Serializable { - private int currentQuestion; - private final boolean[] results; - private final List questions; + private int currentQuestion; + private final boolean[] results; + private final List questions; + private final long startEpoch; + private long endEpoch = -1; - public TriviaGame(List questions) { - this.currentQuestion = 0; - this.questions = questions; - this.results = new boolean[questions.size()]; - } + public TriviaGame(List questions) { + this.currentQuestion = 0; + this.questions = questions; + this.results = new boolean[questions.size()]; + this.startEpoch = System.currentTimeMillis(); + } - public TriviaQuestion getCurrentQuestion() { - return this.questions.get(currentQuestion); - } + /** + * @return The time this trivia game started in a UNIX timestamp format. + * @since 2021-03-17 + */ + public long getStartEpoch(){ + return this.startEpoch; + } + /** + * @return The time this trivia game ended in a UNIX timestamp format. If the player is still playing the game, the return will be equal to -1. + * @since 2021-03-17 + */ + public long getEndEpoch(){ + return this.endEpoch; + } - public int getQuestionProgress() { - return this.currentQuestion + 1; - } + public TriviaQuestion getCurrentQuestion() { + return this.questions.get(currentQuestion); + } - public int getQuestionsCount() { - return this.questions.size(); - } + public int getQuestionProgress() { + return this.currentQuestion + 1; + } - public boolean[] getResults() { - return this.results; - } + public int getQuestionsCount() { + return this.questions.size(); + } - public boolean nextQuestion(String guess) { - TriviaQuestion question = getCurrentQuestion(); - boolean answer = question.checkAnswer(guess); + public boolean[] getResults() { + return this.results; + } - results[currentQuestion] = answer; - currentQuestion++; + public boolean nextQuestion(String guess) { + TriviaQuestion question = getCurrentQuestion(); + boolean answer = question.checkAnswer(guess); - return answer; - } + results[currentQuestion] = answer; + currentQuestion++; - public boolean isDone() { - return (this.currentQuestion == questions.size()); - } + return answer; + } + + public boolean isDone() { + boolean isDone = this.currentQuestion == questions.size(); + // Check isDone and set the trivia game end epoch if the endEpoch field was not yet changed if it's true. + if (isDone && endEpoch == -1) + this.endEpoch = System.currentTimeMillis(); + return isDone; + } } diff --git a/app/src/main/res/layout/activity_trivia_game_results.xml b/app/src/main/res/layout/activity_trivia_game_results.xml index 0d774c0..a79e8cc 100644 --- a/app/src/main/res/layout/activity_trivia_game_results.xml +++ b/app/src/main/res/layout/activity_trivia_game_results.xml @@ -23,95 +23,113 @@ Contact us at . --> - + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:fillViewport="true"> + - + - - + + - + - + - + - - + + - + - + - + - - + + - + - + - + - - + + -