diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java index a1eb727..2007448 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java @@ -31,6 +31,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.Looper; import android.net.Uri; import android.view.View; import android.view.MenuItem; @@ -293,7 +294,7 @@ public class TriviaGameActivity extends BaseActivity SoundUtil.playSound(this, guess ? SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG); - new Handler().postDelayed(() -> { + new Handler(Looper.getMainLooper()).postDelayed(() -> { if (game.isDone()) { Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class); intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game); 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"> + - + - - + + - + - + - + - - + + - + - + - + - - + + - + - + - + - - + + -