Prepared trivia file support implementation

Based on #10.
This commit is contained in:
luca0N! 2021-03-14 15:10:52 -03:00
parent d40b0709a5
commit 081291c4d7
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
4 changed files with 162 additions and 49 deletions

View File

@ -24,67 +24,92 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.activities;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import androidx.appcompat.app.AlertDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.luca0n.joguitos.pluck.R;
import com.luca0n.joguitos.pluck.trivia.TriviaSource;
import com.luca0n.joguitos.pluck.trivia.TriviaCategory;
import com.luca0n.joguitos.pluck.trivia.TriviaDifficulty;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
public class MainActivity extends BaseActivity {
@BindView(R.id.button_play)
Button buttonPlay;
@BindView(R.id.spinner_number)
Spinner spinnerNumber;
@BindView(R.id.spinner_category)
Spinner spinnerCategory;
@BindView(R.id.spinner_difficulty)
Spinner spinnerDifficulty;
@BindView(R.id.button_play)
Button buttonPlay;
@BindView(R.id.spinner_source)
Spinner spinnerSource;
@BindView(R.id.spinner_number)
Spinner spinnerNumber;
@BindView(R.id.spinner_category)
Spinner spinnerCategory;
@BindView(R.id.spinner_difficulty)
Spinner spinnerDifficulty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
buttonPlay.setOnClickListener(v -> {
int amount = (int) spinnerNumber.getSelectedItem();
TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem();
TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem();
buttonPlay.setOnClickListener(v -> {
int sourcePos = spinnerSource.getSelectedItemPosition();
if (TriviaSource.values()[sourcePos] == TriviaSource.FILE){
// Check the Android version.
if (Build.VERSION.SDK_INT >= 19){
// TODO: Show the open document dialog and read the trivia file.
return;
} else {
// Unsupported version.
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.error)
.setMessage(R.string.error_source_file_android_version_unsupported)
.setPositiveButton(android.R.string.ok, null)
.show();
}
return;
}
int amount = (int) spinnerNumber.getSelectedItem();
TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem();
TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem();
TriviaQuery query = new TriviaQuery.Builder(amount)
.category(category)
.difficulty(difficulty)
.build();
TriviaQuery query = new TriviaQuery.Builder(amount)
.category(category)
.difficulty(difficulty)
.build();
Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class);
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_QUERY, query);
startActivity(intent);
});
Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class);
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_QUERY, query);
startActivity(intent);
});
Integer[] numbers = new Integer[50];
for (int i = 0; i < 50; ) {
numbers[i] = ++i;
}
spinnerSource.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, TriviaSource.values())
);
spinnerNumber.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, numbers)
);
spinnerNumber.setSelection(9);
Integer[] numbers = new Integer[50];
for (int i = 0; i < 50; ) {
numbers[i] = ++i;
}
spinnerNumber.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, numbers)
);
spinnerNumber.setSelection(9);
spinnerCategory.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, TriviaCategory.values()));
spinnerCategory.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, TriviaCategory.values()));
spinnerDifficulty.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, TriviaDifficulty.values()));
}
spinnerDifficulty.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, TriviaDifficulty.values()));
}
}

View File

@ -95,9 +95,23 @@ public class TriviaGameActivity extends BaseActivity
progressBar.setVisibility(View.VISIBLE);
DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask();
task.setReceiver(this);
task.execute(query);
// Gather information about the trivia location.
/*String triviaLocation;
switch (triviaLocation){
case "GET_FROM_URL":*/
DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask();
task.setReceiver(this);
task.execute(query);/*
break;
case "GET_FROM_FILE":
break;
default:
// Unknown specified trivia location.
// TODO: Show error message to the user about this internal error.
break;
}*/
}
}

View File

@ -0,0 +1,66 @@
/*
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.trivia;
import java.util.HashMap;
import java.util.Map;
import com.luca0n.joguitos.pluck.PluckApplication;
import com.luca0n.joguitos.pluck.R;
public enum TriviaSource {
SERVER("server", R.string.source_server),
FILE("file", R.string.source_file);
// Name of source
private final String name;
// Display name of the source
private final int displayName;
private static final Map<String, TriviaSource> lookup = new HashMap<>();
static {
for (TriviaSource source : TriviaSource.values()) {
lookup.put(source.getName(), source);
}
}
TriviaSource(String name, int displayName) {
this.name = name;
this.displayName = displayName;
}
public String getName() {
return this.name;
}
public static TriviaSource get(String name) {
return lookup.get(name);
}
@Override
public String toString() {
return PluckApplication.getAppContext().getResources().getString(this.displayName);
}
}

View File

@ -61,6 +61,10 @@ Contact us at <joguitos+pluck@luca0n.com>.
<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>
<!-- UI -->
<string name="ui_settings">Settings</string>
<string name="ui_about">About</string>
@ -80,13 +84,17 @@ 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_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_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>
<!-- Error Strings -->
<!-- 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="title_activity_settings">Settings</string>
@ -98,6 +106,6 @@ Contact us at <joguitos+pluck@luca0n.com>.
<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. Leave empty to use the default server.</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>
</resources>