Merge pull request 'Add "Time Elapsed" field to TriviaGameResultsActivity' (#18) from dev into master

Reviewed-on: https://git.luca0N.com/luca0N/Pluck/pulls/18
This commit is contained in:
luca0N! 2021-03-17 19:33:47 +00:00
commit 695bd1b1a7
Signed by: luca0N! Git
GPG Key ID: 916186E0CBD7AB60
7 changed files with 206 additions and 141 deletions

View File

@ -31,6 +31,7 @@ import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper;
import android.net.Uri; import android.net.Uri;
import android.view.View; import android.view.View;
import android.view.MenuItem; import android.view.MenuItem;
@ -293,7 +294,7 @@ public class TriviaGameActivity extends BaseActivity
SoundUtil.playSound(this, guess ? SoundUtil.playSound(this, guess ?
SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG); SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG);
new Handler().postDelayed(() -> { new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (game.isDone()) { if (game.isDone()) {
Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class); Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class);
intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game); intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game);

View File

@ -41,6 +41,8 @@ public class TriviaGameResultsActivity extends BaseActivity {
TextView textResultsWrong; TextView textResultsWrong;
@BindView(R.id.text_results_total) @BindView(R.id.text_results_total)
TextView textResultsTotal; TextView textResultsTotal;
@BindView(R.id.text_results_time_elapsed)
TextView textResultsTimeElapsed;
@BindView(R.id.button_return_to_menu) @BindView(R.id.button_return_to_menu)
Button buttonReturnToMenu; 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)); textResultsCorrect.setText(String.valueOf(correctTotal));
textResultsWrong.setText(String.valueOf(game.getQuestionsCount() - correctTotal)); textResultsWrong.setText(String.valueOf(game.getQuestionsCount() - correctTotal));
textResultsTotal.setText(String.valueOf(game.getQuestionsCount())); 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()); buttonReturnToMenu.setOnClickListener(v -> finish());
} }

View File

@ -27,43 +27,65 @@ import java.io.Serializable;
import java.util.List; import java.util.List;
public class TriviaGame implements Serializable { public class TriviaGame implements Serializable {
private int currentQuestion; private int currentQuestion;
private final boolean[] results; private final boolean[] results;
private final List<TriviaQuestion> questions; private final List<TriviaQuestion> questions;
private final long startEpoch;
private long endEpoch = -1;
public TriviaGame(List<TriviaQuestion> questions) { public TriviaGame(List<TriviaQuestion> questions) {
this.currentQuestion = 0; this.currentQuestion = 0;
this.questions = questions; this.questions = questions;
this.results = new boolean[questions.size()]; 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() { public TriviaQuestion getCurrentQuestion() {
return this.currentQuestion + 1; return this.questions.get(currentQuestion);
} }
public int getQuestionsCount() { public int getQuestionProgress() {
return this.questions.size(); return this.currentQuestion + 1;
} }
public boolean[] getResults() { public int getQuestionsCount() {
return this.results; return this.questions.size();
} }
public boolean nextQuestion(String guess) { public boolean[] getResults() {
TriviaQuestion question = getCurrentQuestion(); return this.results;
boolean answer = question.checkAnswer(guess); }
results[currentQuestion] = answer; public boolean nextQuestion(String guess) {
currentQuestion++; TriviaQuestion question = getCurrentQuestion();
boolean answer = question.checkAnswer(guess);
return answer; results[currentQuestion] = answer;
} currentQuestion++;
public boolean isDone() { return answer;
return (this.currentQuestion == questions.size()); }
}
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;
}
} }

View File

@ -23,95 +23,113 @@ Contact us at <joguitos+pluck@luca0n.com>.
--> -->
<ScrollView <ScrollView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fillViewport="true"> android:fillViewport="true">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
tools:context="io.github.trytonvanmeer.libretrivia.activities.TriviaGameResultsActivity"> tools:context="io.github.trytonvanmeer.libretrivia.activities.TriviaGameResultsActivity">
<ImageView <ImageView
android:layout_width="250dp" android:layout_width="250dp"
android:layout_height="250dp" android:layout_height="250dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:scaleType="fitXY" android:scaleType="fitXY"
android:src="@drawable/ic_clipboard_text" android:src="@drawable/ic_clipboard_text"
android:tint="@color/colorTextSecondary" /> android:tint="@color/colorTextSecondary" />
<!-- Total Answers --> <!-- Total Answers -->
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp"> android:layout_marginBottom="16dp">
<TextView <TextView
style="@style/ResultText" style="@style/ResultText"
android:text="@string/ui_results_total" android:text="@string/ui_results_total"
android:textColor="@color/colorAccent" /> android:textColor="@color/colorAccent" />
<TextView <TextView
android:id="@+id/text_results_total" android:id="@+id/text_results_total"
style="@style/ResultText" style="@style/ResultText"
tools:text="10" /> tools:text="10" />
</LinearLayout> </LinearLayout>
<!-- Correct Answers --> <!-- Correct Answers -->
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp"> android:layout_marginBottom="16dp">
<TextView <TextView
style="@style/ResultText" style="@style/ResultText"
android:text="@string/ui_results_correct" android:text="@string/ui_results_correct"
android:textColor="@color/colorAccentGreen" /> android:textColor="@color/colorAccentGreen" />
<TextView <TextView
android:id="@+id/text_results_correct" android:id="@+id/text_results_correct"
style="@style/ResultText" style="@style/ResultText"
tools:text="6" /> tools:text="6" />
</LinearLayout> </LinearLayout>
<!-- Wrong Answers--> <!-- Wrong Answers-->
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp"> android:layout_marginBottom="16dp">
<TextView <TextView
style="@style/ResultText" style="@style/ResultText"
android:text="@string/ui_results_wrong" android:text="@string/ui_results_wrong"
android:textColor="@color/colorAccentRed" /> android:textColor="@color/colorAccentRed" />
<TextView <TextView
android:id="@+id/text_results_wrong" android:id="@+id/text_results_wrong"
style="@style/ResultText" style="@style/ResultText"
tools:text="4" /> tools:text="4" />
</LinearLayout> </LinearLayout>
<!-- Buttons --> <!-- Time Elapsed -->
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center"> android:layout_marginBottom="16dp">
<Button <TextView
android:id="@+id/button_return_to_menu" style="@style/ResultText"
style="@style/Button" android:text="@string/ui_results_time_elapsed"
android:layout_width="wrap_content" android:textColor="@color/colorAccentOrange" />
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/ui_return_to_menu" />
</LinearLayout> <TextView
android:id="@+id/text_results_time_elapsed"
style="@style/ResultText"
tools:text="4" />
</LinearLayout>
<!-- Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/button_return_to_menu"
style="@style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/ui_return_to_menu" />
</LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View File

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

View File

@ -84,7 +84,10 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="ui_quit_game_msg">Are you sure you want to quit this game?</string> <string name="ui_quit_game_msg">Are you sure you want to quit this game?</string>
<string name="ui_results_correct">Correct Answers</string> <string name="ui_results_correct">Correct Answers</string>
<string name="ui_results_wrong">Wrong Answers</string> <string name="ui_results_wrong">Wrong Answers</string>
<string name="ui_results_total">Total Questions</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_return_to_menu">Return To Menu</string>
<string name="ui_source">Source</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> <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

@ -24,43 +24,48 @@ Contact us at <joguitos+pluck@luca0n.com>.
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
</style> </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"> <style name="TextLabel">
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item> <item name="android:layout_height">wrap_content</item>
<item name="android:textSize">18sp</item> <item name="android:textSize">18sp</item>
</style> </style>
<style name="Spinner"> <style name="Spinner">
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item> <item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">16dp</item> <item name="android:layout_marginBottom">16dp</item>
</style> </style>
<style name="Button" parent="Base.Widget.AppCompat.Button"> <style name="Button" parent="Base.Widget.AppCompat.Button">
<item name="backgroundTint">@color/colorAccent</item> <item name="backgroundTint">@color/colorAccent</item>
<item name="android:textColor">@color/colorText</item> <item name="android:textColor">@color/colorText</item>
<item name="android:textAlignment">center</item> <item name="android:textAlignment">center</item>
</style> </style>
<style name="AnswerButton" parent="Button"> <style name="AnswerButton" parent="Button">
<item name="android:width">300dp</item> <item name="android:width">300dp</item>
<item name="android:layout_marginBottom">16dp</item> <item name="android:layout_marginBottom">16dp</item>
</style> </style>
<style name="ResultText"> <style name="ResultText">
<item name="android:layout_width">0dp</item> <item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item> <item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item> <item name="android:layout_weight">1</item>
<item name="android:textAlignment">center</item> <item name="android:textAlignment">center</item>
<item name="android:textStyle">bold</item> <item name="android:textStyle">bold</item>
</style> </style>
</resources> </resources>