From 4045f0207ce0c9b2555de123362123813c13a100 Mon Sep 17 00:00:00 2001 From: "luca0N\\!" Date: Tue, 16 Mar 2021 01:23:17 -0300 Subject: [PATCH] Added "All" option on the question amount spinner Added a new option on the question amount spinner that allows players to view all questions on a trivia file. --- .../pluck/activities/MainActivity.java | 32 +++++++++++++------ .../luca0n/joguitos/pluck/util/ApiUtil.java | 3 +- app/src/main/res/values/strings.xml | 4 ++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java b/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java index ea48e47..3cb9f2e 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java @@ -86,10 +86,21 @@ public class MainActivity extends BaseActivity { } return; } - int amount = (int) spinnerNumber.getSelectedItem(); + String amount = (String) spinnerNumber.getSelectedItem(); TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem(); TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem(); + if (amount.equals(getString(R.string.ui_all))){ + // The "All" option is only used for trivia files. + new AlertDialog.Builder(this) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle(R.string.error) + .setMessage(R.string.error_option_all_unavailable) + .setPositiveButton(android.R.string.ok, null) + .show(); + return; + } + // Put the server address in the TriviaQuery object. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String serverKey = getResources().getString(R.string.pref_network_server); @@ -103,7 +114,7 @@ public class MainActivity extends BaseActivity { sp.edit().putString(serverKey, defaultServer).commit(); } - TriviaQuery query = new TriviaQuery.Builder(amount) + TriviaQuery query = new TriviaQuery.Builder(Integer.valueOf(amount)) .category(category) .difficulty(difficulty) .base(base) @@ -114,16 +125,19 @@ public class MainActivity extends BaseActivity { intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_SOURCE, TriviaSource.SERVER.getName()); // Specify the trivia source. intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_QUERY, query); intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_FILTERS, - new TriviaFilters.Builder(amount) + new TriviaFilters.Builder(Integer.valueOf(amount)) .category(category) .difficulty(difficulty) .build()); startActivity(intent); }); - Integer[] numbers = new Integer[50]; - for (int i = 0; i < 50; ) { - numbers[i] = ++i; + // Create a String array that holds all options for spinnerNumber. + String[] numbers = new String[51]; + for (int i = 0; i < 51; i++) { + // The first entry on the spinner should be the value "All", which is used with the + // file trivia source. + numbers[i] = i == 0 ? getString(R.string.ui_all) : String.valueOf(i); } spinnerSource.setAdapter( new ArrayAdapter<>( @@ -133,7 +147,7 @@ public class MainActivity extends BaseActivity { new ArrayAdapter<>( this, android.R.layout.simple_list_item_1, numbers) ); - spinnerNumber.setSelection(9); + spinnerNumber.setSelection(10); spinnerCategory.setAdapter( new ArrayAdapter<>( @@ -154,14 +168,14 @@ public class MainActivity extends BaseActivity { if (extras == null) break; - int amount = (int) spinnerNumber.getSelectedItem(); + String amount = (String) spinnerNumber.getSelectedItem(); TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem(); TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem(); Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class); // Put filters on the extras object. intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_FILTERS, - new TriviaFilters.Builder(amount) + new TriviaFilters.Builder(amount.equals(getString(R.string.ui_all)) ? -1 : Integer.valueOf(amount)) .category(category) .difficulty(difficulty) .build()); diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java b/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java index cc3883b..997865f 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java @@ -131,7 +131,8 @@ public class ApiUtil { int elements = 0; for (JsonElement element : jsonArray) { - if (elements++ == filters.getAmount()) + int amount = filters.getAmount(); + if (amount > 0 && elements++ == amount) break; JsonObject object = element.getAsJsonObject(); TriviaType type = TriviaType.get(object.get("type").getAsString()); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 075fcc1..ed58d52 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -70,7 +70,8 @@ Contact us at . About Play Start Game - Any + Any + All True False Correct @@ -97,6 +98,7 @@ Contact us at . Trivia files are not supported in this version of Android. Invalid trivia file!\n\nTrivia files should be in a plain text JSON format. An unknown error occurred while attempting to read the opened file. + The selected question amount is only available for the "File" trivia game source. Settings