Added "Time Elapsed" field

This commit is contained in:
luca0N! 2021-03-17 16:16:37 -03:00
parent 49960dcd7b
commit 8b2bd0522c
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
6 changed files with 204 additions and 140 deletions

View File

@ -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());
}

View File

@ -30,11 +30,29 @@ public class TriviaGame implements Serializable {
private int currentQuestion;
private final boolean[] results;
private final List<TriviaQuestion> questions;
private final long startEpoch;
private long endEpoch = -1;
public TriviaGame(List<TriviaQuestion> questions) {
this.currentQuestion = 0;
this.questions = questions;
this.results = new boolean[questions.size()];
this.startEpoch = System.currentTimeMillis();
}
/**
* @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 TriviaQuestion getCurrentQuestion() {
@ -64,6 +82,10 @@ public class TriviaGame implements Serializable {
}
public boolean isDone() {
return (this.currentQuestion == questions.size());
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;
}
}

View File

@ -28,7 +28,7 @@ Contact us at <joguitos+pluck@luca0n.com>.
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
@ -97,6 +97,24 @@ Contact us at <joguitos+pluck@luca0n.com>.
</LinearLayout>
<!-- Time Elapsed -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<TextView
style="@style/ResultText"
android:text="@string/ui_results_time_elapsed"
android:textColor="@color/colorAccentOrange" />
<TextView
android:id="@+id/text_results_time_elapsed"
style="@style/ResultText"
tools:text="4" />
</LinearLayout>
<!-- Buttons -->
<LinearLayout
android:layout_width="match_parent"

View File

@ -23,8 +23,8 @@ Contact us at <joguitos+pluck@luca0n.com>.
-->
<resources>
<color name="colorPrimary">#F44336</color>
<color name="colorPrimaryDark">#D32F2F</color>
<color name="colorPrimary">#14A210</color>
<color name="colorPrimaryDark">#0D7D0A</color>
<color name="colorAccent">#536DFE</color>
<color name="colorTextPrimary">#212121</color>
<color name="colorTextSecondary">#757575</color>
@ -32,4 +32,5 @@ Contact us at <joguitos+pluck@luca0n.com>.
<color name="colorAccentRed">#D50000</color>
<color name="colorAccentGreen">#00C853</color>
<color name="colorAccentOrange">#E8681C</color>
</resources>

View File

@ -85,6 +85,9 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="ui_results_correct">Correct Answers</string>
<string name="ui_results_wrong">Wrong Answers</string>
<string name="ui_results_total">Total Questions</string>
<string name="ui_results_time_elapsed">Time Elapsed</string>
<string name="ui_results_time_elapsed_format_seconds">%.1f seconds</string>
<string name="ui_results_time_elapsed_format_minutes">%.1f minutes</string>
<string name="ui_return_to_menu">Return To Menu</string>
<string name="ui_source">Source</string>
<string name="ui_source_about">This value specifies where the game should retrieve trivia data. By default, the game gathers trivia data from a server, but you can load trivia files with the "File" source option.</string>

View File

@ -31,6 +31,11 @@ Contact us at <joguitos+pluck@luca0n.com>.
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="TextLabel">
<item name="android:layout_width">match_parent</item>