Added initial multiple theme support

This commit is contained in:
luca0N! 2021-03-17 17:38:52 -03:00
parent a5cb9430a1
commit c7e81eb66b
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
7 changed files with 231 additions and 125 deletions

View File

@ -43,6 +43,7 @@ import com.luca0n.joguitos.pluck.trivia.TriviaCategory;
import com.luca0n.joguitos.pluck.trivia.TriviaDifficulty;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class MainActivity extends BaseActivity {
@ -61,6 +62,7 @@ public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

View File

@ -66,6 +66,7 @@ import com.luca0n.joguitos.pluck.trivia.TriviaSource;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
import com.luca0n.joguitos.pluck.util.ApiUtil;
import com.luca0n.joguitos.pluck.util.SoundUtil;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class TriviaGameActivity extends BaseActivity
implements IDownloadTriviaQuestionReceiver {
@ -92,6 +93,7 @@ public class TriviaGameActivity extends BaseActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game);
ButterKnife.bind(this);

View File

@ -31,55 +31,57 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import com.luca0n.joguitos.pluck.R;
import com.luca0n.joguitos.pluck.trivia.TriviaGame;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class TriviaGameResultsActivity extends BaseActivity {
static final String EXTRA_TRIVIA_GAME = "extra_trivia_game";
static final String EXTRA_TRIVIA_GAME = "extra_trivia_game";
@BindView(R.id.text_results_correct)
TextView textResultsCorrect;
@BindView(R.id.text_results_wrong)
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;
@BindView(R.id.text_results_correct)
TextView textResultsCorrect;
@BindView(R.id.text_results_wrong)
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game_results);
ButterKnife.bind(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game_results);
ButterKnife.bind(this);
Bundle bundle = getIntent().getExtras();
TriviaGame game = (TriviaGame) bundle.get(EXTRA_TRIVIA_GAME);
Bundle bundle = getIntent().getExtras();
TriviaGame game = (TriviaGame) bundle.get(EXTRA_TRIVIA_GAME);
int correctTotal = 0;
int correctTotal = 0;
for (boolean result : game.getResults()) {
if (result) {
correctTotal++;
}
}
for (boolean result : game.getResults()) {
if (result) {
correctTotal++;
}
}
// Calculate the difference between startEpoch and endEpoch
float timeDifference = (game.getEndEpoch() - game.getStartEpoch())/1000F;
// 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)
);
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());
}
buttonReturnToMenu.setOnClickListener(v -> finish());
}
}

View File

@ -0,0 +1,52 @@
/*
Pluck: an open source trivia game for Android
Copyright (C) 2021 Joguitos do luca0N!
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
This game is a fork of LibreTrivia, and its source code is available at
<https://github.com/tryton-vanmeer/LibreTrivia>.
Contact us at <joguitos+pluck@luca0n.com>.
*/
package com.luca0n.joguitos.pluck.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.luca0n.joguitos.pluck.R;
public class UiUtil {
/**
* Applies the theme selected by the user to the context.
* @param context The activity context.
* @since 2021-03-17
*/
public static void setTheme(Context context){
SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String light = context.getString(R.string.pref_ui_theme_entryValues_light),
dark = context.getString(R.string.pref_ui_theme_entryValues_dark),
themeKey = context.getString(R.string.pref_ui_theme),
theme = s.getString(themeKey, light);
if (theme.equals(dark))
context.setTheme(R.style.AppTheme_Dark);
else
context.setTheme(R.style.AppTheme);
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Pluck: an open source trivia game for Android
Copyright (C) 2021 Joguitos do luca0N!
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
This game is a fork of LibreTrivia, and its source code is available at
<https://github.com/tryton-vanmeer/LibreTrivia>.
Contact us at <joguitos+pluck@luca0n.com>.
-->
<resources>
<string-array name="pref_ui_theme_entryValues">
<item>pref_ui_theme_entryValues_light</item>
<item>pref_ui_theme_entryValues_dark</item>
</string-array>
</resources>

View File

@ -23,97 +23,105 @@ Contact us at <joguitos+pluck@luca0n.com>.
-->
<resources>
<string name="app_name" translatable="false">Pluck</string>
<string name="app_description">An open source trivia game.</string>
<string name="app_name" translatable="false">Pluck</string>
<string name="app_description">An open source trivia game.</string>
<!-- Category Names -->
<string name="category_general_knowledge">General Knowledge</string>
<string name="category_entertainment_books">Entertainment: Books</string>
<string name="category_entertainment_film">Entertainment: Film</string>
<string name="category_entertainment_music">Entertainment: Music</string>
<string name="category_entertainment_musicals_theatres">Entertainment: Musicals &amp; Theatres</string>
<string name="category_entertainment_television">Entertainment: Television</string>
<string name="category_entertainment_video_games">Entertainment: Video Games</string>
<string name="category_entertainment_board_games">Entertainment: Board Games</string>
<string name="category_entertainment_japanese_anime_manga">Entertainment: Japanese Anime &amp; Manga</string>
<string name="category_entertainment_cartoon_animations">Entertainment: Cartoons &amp; Animation</string>
<string name="category_entertainment_comics">Entertainment: Comics</string>
<string name="category_science_nature">Science &amp; Nature</string>
<string name="category_science_computers">Science: Computers</string>
<string name="category_science_mathematics">Science: Mathematics</string>
<string name="category_science_gadgets">Science: Gadgets</string>
<string name="category_mythology">Mythology</string>
<string name="category_sports">Sports</string>
<string name="category_geography">Geography</string>
<string name="category_history">History</string>
<string name="category_politics">Politics</string>
<string name="category_art">Art</string>
<string name="category_celebrities">Celebrities</string>
<string name="category_animals">Animals</string>
<string name="category_vehicles">Vehicles</string>
<!-- Category Names -->
<string name="category_general_knowledge">General Knowledge</string>
<string name="category_entertainment_books">Entertainment: Books</string>
<string name="category_entertainment_film">Entertainment: Film</string>
<string name="category_entertainment_music">Entertainment: Music</string>
<string name="category_entertainment_musicals_theatres">Entertainment: Musicals &amp; Theatres</string>
<string name="category_entertainment_television">Entertainment: Television</string>
<string name="category_entertainment_video_games">Entertainment: Video Games</string>
<string name="category_entertainment_board_games">Entertainment: Board Games</string>
<string name="category_entertainment_japanese_anime_manga">Entertainment: Japanese Anime &amp; Manga</string>
<string name="category_entertainment_cartoon_animations">Entertainment: Cartoons &amp; Animation</string>
<string name="category_entertainment_comics">Entertainment: Comics</string>
<string name="category_science_nature">Science &amp; Nature</string>
<string name="category_science_computers">Science: Computers</string>
<string name="category_science_mathematics">Science: Mathematics</string>
<string name="category_science_gadgets">Science: Gadgets</string>
<string name="category_mythology">Mythology</string>
<string name="category_sports">Sports</string>
<string name="category_geography">Geography</string>
<string name="category_history">History</string>
<string name="category_politics">Politics</string>
<string name="category_art">Art</string>
<string name="category_celebrities">Celebrities</string>
<string name="category_animals">Animals</string>
<string name="category_vehicles">Vehicles</string>
<!-- Difficulty Names -->
<string name="difficulty_easy">Easy</string>
<string name="difficulty_medium">Medium</string>
<string name="difficulty_hard">Hard</string>
<!-- Difficulty Names -->
<string name="difficulty_easy">Easy</string>
<string name="difficulty_medium">Medium</string>
<string name="difficulty_hard">Hard</string>
<!-- Question Type Names -->
<string name="question_type_multiple">Multiple Choice</string>
<string name="question_type_boolean">True / False</string>
<!-- Question Type Names -->
<string name="question_type_multiple">Multiple Choice</string>
<string name="question_type_boolean">True / False</string>
<!-- Source Names -->
<string name="source_server">Server</string>
<string name="source_file">File</string>
<!-- Source Names -->
<string name="source_server">Server</string>
<string name="source_file">File</string>
<!-- UI -->
<string name="ui_settings">Settings</string>
<string name="ui_about">About</string>
<string name="ui_play">Play</string>
<string name="ui_start_game">Start Game</string>
<string name="ui_any">Any</string>
<string name="ui_all">All</string>
<string name="ui_true">True</string>
<string name="ui_false">False</string>
<string name="ui_correct">Correct</string>
<string name="ui_wrong">Wrong</string>
<string name="ui_amount">Questions</string>
<string name="ui_category">Category</string>
<string name="ui_difficulty">Difficulty</string>
<string name="ui_question_progress">%1$d%2$d</string>
<string name="ui_quit_game">Quit 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_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>
<string name="ui_network_notice">Notice: when using a server as the game source, this game connects to Open Trivia Database by default to gather trivia questions. Open Trivia Database is a collection of user-contributed questions that are licensed under the Creative Commons Attribution-ShareAlike 4.0 International (CC-BY-SA 4.0) license.\n\nYou can select a custom server in the game settings.\n\nIf you want to open a trivia file instead of getting trivia questions from a server, select "File" as the trivia game source above.</string>
<!-- UI -->
<string name="ui_settings">Settings</string>
<string name="ui_about">About</string>
<string name="ui_play">Play</string>
<string name="ui_start_game">Start Game</string>
<string name="ui_any">Any</string>
<string name="ui_all">All</string>
<string name="ui_true">True</string>
<string name="ui_false">False</string>
<string name="ui_correct">Correct</string>
<string name="ui_wrong">Wrong</string>
<string name="ui_amount">Questions</string>
<string name="ui_category">Category</string>
<string name="ui_difficulty">Difficulty</string>
<string name="ui_question_progress">%1$d%2$d</string>
<string name="ui_quit_game">Quit 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_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>
<string name="ui_network_notice">Notice: when using a server as the game source, this game connects to Open Trivia Database by default to gather trivia questions. Open Trivia Database is a collection of user-contributed questions that are licensed under the Creative Commons Attribution-ShareAlike 4.0 International (CC-BY-SA 4.0) license.\n\nYou can select a custom server in the game settings.\n\nIf you want to open a trivia file instead of getting trivia questions from a server, select "File" as the trivia game source above.</string>
<!-- Error Strings -->
<string name="error">Error</string>
<string name="error_network"><b>Network error!</b>\n\nCould not connect to a network. Check if your custom server address is correct. If Tor is enabled in the game settings, check if Orbot is running and then try again.</string>
<string name="error_no_trivia_results"><b>No trivia results!</b>\n\nWas not able to find trivia questions that satisfied all options.</string>
<string name="error_server_response_invalid"><b>Server response error!</b>\n\nCould not parse the server response. The server response may be in a unsupported version. If you are using a custom server, make sure the server address points to a valid address and then try again.</string>
<string name="error_unknown"><b>An unknown error occurred!</b></string>
<string name="error_source_file_android_version_unsupported">Trivia files are not supported in this version of Android.</string>
<string name="error_trivia_file_invalid"><b>Invalid trivia file!</b>\n\nTrivia files should be in a plain text JSON format.</string>
<string name="error_trivia_file_fetch_unknown">An unknown error occurred while attempting to read the opened file.</string>
<string name="error_option_all_unavailable">The selected question amount is only available for the "File" trivia game source.</string>
<!-- Error Strings -->
<string name="error">Error</string>
<string name="error_network"><b>Network error!</b>\n\nCould not connect to a network. Check if your custom server address is correct. If Tor is enabled in the game settings, check if Orbot is running and then try again.</string>
<string name="error_no_trivia_results"><b>No trivia results!</b>\n\nWas not able to find trivia questions that satisfied all options.</string>
<string name="error_server_response_invalid"><b>Server response error!</b>\n\nCould not parse the server response. The server response may be in a unsupported version. If you are using a custom server, make sure the server address points to a valid address and then try again.</string>
<string name="error_unknown"><b>An unknown error occurred!</b></string>
<string name="error_source_file_android_version_unsupported">Trivia files are not supported in this version of Android.</string>
<string name="error_trivia_file_invalid"><b>Invalid trivia file!</b>\n\nTrivia files should be in a plain text JSON format.</string>
<string name="error_trivia_file_fetch_unknown">An unknown error occurred while attempting to read the opened file.</string>
<string name="error_option_all_unavailable">The selected question amount is only available for the "File" trivia game source.</string>
<string name="title_activity_settings">Settings</string>
<string name="title_activity_settings">Settings</string>
<!-- Setting Strings-->
<string name="pref_category_sound_title">Sound</string>
<string name="pref_sound_answer_title">Answer Sound Effect</string>
<string name="pref_sound_answer_summary">Toggles game sound effects.</string>
<!-- Setting Strings-->
<string name="pref_category_sound_title">Sound</string>
<string name="pref_sound_answer_title">Answer Sound Effect</string>
<string name="pref_sound_answer_summary">Toggles game sound effects.</string>
<string name="pref_category_network_title">Network</string>
<string name="pref_network_tor_title">Connect Using Tor</string>
<string name="pref_network_tor_summary">Retrieves trivia data via the Tor network. Requires Orbot (recommended) or a Tor daemon with a SOCKS5 proxy listening on port 9050.</string>
<string name="pref_network_server_title">Server Address</string>
<string name="pref_network_server_summary">Specifies the server location used by the game to fetch questions when using a server as the trivia source. Leave this setting empty to use the default server.</string>
<string name="pref_category_network_title">Network</string>
<string name="pref_network_tor_title">Connect Using Tor</string>
<string name="pref_network_tor_summary">Retrieves trivia data via the Tor network. Requires Orbot (recommended) or a Tor daemon with a SOCKS5 proxy listening on port 9050.</string>
<string name="pref_network_server_title">Server Address</string>
<string name="pref_network_server_summary">Specifies the server location used by the game to fetch questions when using a server as the trivia source. Leave this setting empty to use the default server.</string>
<string name="pref_category_ui_title">Interface</string>
<string name="pref_ui_theme_title">Theme</string>
<string name="pref_ui_theme_summary">Defines the game theme.</string>
<string-array name="pref_ui_theme_entries">
<item>Light</item>
<item>Dark</item>
</string-array>
</resources>

View File

@ -48,4 +48,14 @@ Contact us at <joguitos+pluck@luca0n.com>.
android:title="@string/pref_network_tor_title"
android:summary="@string/pref_network_tor_summary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_category_ui_title">
<ListPreference
android:defaultValue="@string/pref_ui_theme_default"
android:key="@string/pref_ui_theme"
android:title="@string/pref_ui_theme_title"
android:summary="@string/pref_ui_theme_summary"
android:entries="@array/pref_ui_theme_entries"
android:entryValues="@array/pref_ui_theme_entryValues" />
</PreferenceCategory>
</PreferenceScreen>