Added custom server URL support
Added custom server URL support, changed spaces to tabs and added/changed some preference summary strings. Based on #9.
This commit is contained in:
parent
23e4b4c19d
commit
7fb4ddb79f
4 changed files with 103 additions and 78 deletions
|
@ -23,79 +23,94 @@ Contact us at <joguitos+pluck@luca0n.com>.
|
|||
|
||||
package com.luca0n.joguitos.pluck.trivia;
|
||||
|
||||
import com.luca0n.joguitos.pluck.PluckApplication;
|
||||
import com.luca0n.joguitos.pluck.R;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
public class TriviaQuery implements Serializable {
|
||||
private static final String BASE = "https://opentdb.com/api.php?";
|
||||
private static final int DEFAULT_AMOUNT = 10;
|
||||
private static final int DEFAULT_AMOUNT = 10;
|
||||
|
||||
private final int amount;
|
||||
private final TriviaCategory category;
|
||||
private final TriviaDifficulty difficulty;
|
||||
private final TriviaType type;
|
||||
private final int amount;
|
||||
private final TriviaCategory category;
|
||||
private final TriviaDifficulty difficulty;
|
||||
private final TriviaType type;
|
||||
|
||||
private TriviaQuery(Builder builder) {
|
||||
this.amount = builder.amount;
|
||||
this.category = builder.category;
|
||||
this.difficulty = builder.difficulty;
|
||||
this.type = builder.type;
|
||||
}
|
||||
private TriviaQuery(Builder builder) {
|
||||
this.amount = builder.amount;
|
||||
this.category = builder.category;
|
||||
this.difficulty = builder.difficulty;
|
||||
this.type = builder.type;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private final int amount;
|
||||
private TriviaCategory category;
|
||||
private TriviaDifficulty difficulty;
|
||||
private TriviaType type;
|
||||
public static class Builder {
|
||||
private final int amount;
|
||||
private TriviaCategory category;
|
||||
private TriviaDifficulty difficulty;
|
||||
private TriviaType type;
|
||||
|
||||
public Builder() {
|
||||
this.amount = DEFAULT_AMOUNT;
|
||||
}
|
||||
public Builder() {
|
||||
this.amount = DEFAULT_AMOUNT;
|
||||
}
|
||||
|
||||
public Builder(int amount) {
|
||||
if (amount > 50) {
|
||||
this.amount = 50;
|
||||
} else {
|
||||
this.amount = amount;
|
||||
}
|
||||
}
|
||||
public Builder(int amount) {
|
||||
if (amount > 50) {
|
||||
this.amount = 50;
|
||||
} else {
|
||||
this.amount = amount;
|
||||
}
|
||||
}
|
||||
|
||||
public Builder category(TriviaCategory category) {
|
||||
this.category = category;
|
||||
return this;
|
||||
}
|
||||
public Builder category(TriviaCategory category) {
|
||||
this.category = category;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder difficulty(TriviaDifficulty difficulty) {
|
||||
this.difficulty = difficulty;
|
||||
return this;
|
||||
}
|
||||
public Builder difficulty(TriviaDifficulty difficulty) {
|
||||
this.difficulty = difficulty;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder type(TriviaType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
public Builder type(TriviaType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TriviaQuery build() {
|
||||
return new TriviaQuery(this);
|
||||
}
|
||||
}
|
||||
public TriviaQuery build() {
|
||||
return new TriviaQuery(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder url = new StringBuilder();
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder url = new StringBuilder();
|
||||
|
||||
url.append(BASE);
|
||||
url.append("amount=").append(this.amount);
|
||||
// Load custom server URL.
|
||||
Context c = PluckApplication.getAppContext();
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
|
||||
String key = c.getResources().getString(R.string.pref_network_server);
|
||||
// Load default server URL in case the player did not specify a custom server URL.
|
||||
String defaultServer = c.getResources().getString(R.string.pref_network_server_default);
|
||||
|
||||
if (this.category != null & this.category != TriviaCategory.ANY) {
|
||||
url.append("&category=").append(this.category.getID());
|
||||
}
|
||||
if (this.difficulty != null & this.difficulty != TriviaDifficulty.ANY) {
|
||||
url.append("&difficulty=").append(this.difficulty.getName());
|
||||
}
|
||||
if (this.type != null & this.type != TriviaType.ANY) {
|
||||
url.append("&type=").append(this.type.getName());
|
||||
}
|
||||
String base = sp.getString(key, defaultServer) + "?"; // append "?" to the end of the URL so we can specify GET parameters.
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
url.append(base);
|
||||
url.append("amount=").append(this.amount);
|
||||
|
||||
if (this.category != null & this.category != TriviaCategory.ANY) {
|
||||
url.append("&category=").append(this.category.getID());
|
||||
}
|
||||
if (this.difficulty != null & this.difficulty != TriviaDifficulty.ANY) {
|
||||
url.append("&difficulty=").append(this.difficulty.getName());
|
||||
}
|
||||
if (this.type != null & this.type != TriviaType.ANY) {
|
||||
url.append("&type=").append(this.type.getName());
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue