Added back button on game activity
This commit is contained in:
parent
fb2d81550c
commit
e8c2504a33
|
@ -33,6 +33,7 @@ import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.MenuItem;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
|
@ -40,6 +41,7 @@ import android.widget.TextView;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
|
@ -56,198 +58,213 @@ import com.luca0n.joguitos.pluck.util.ApiUtil;
|
||||||
import com.luca0n.joguitos.pluck.util.SoundUtil;
|
import com.luca0n.joguitos.pluck.util.SoundUtil;
|
||||||
|
|
||||||
public class TriviaGameActivity extends BaseActivity
|
public class TriviaGameActivity extends BaseActivity
|
||||||
implements IDownloadTriviaQuestionReceiver {
|
implements IDownloadTriviaQuestionReceiver {
|
||||||
static final String EXTRA_TRIVIA_QUERY = "extra_trivia_query";
|
static final String EXTRA_TRIVIA_QUERY = "extra_trivia_query";
|
||||||
private final String STATE_TRIVIA_GAME = "state_trivia_game";
|
private final String STATE_TRIVIA_GAME = "state_trivia_game";
|
||||||
|
|
||||||
private TriviaGame game;
|
private TriviaGame game;
|
||||||
|
|
||||||
@BindView(R.id.progress_bar)
|
@BindView(R.id.progress_bar)
|
||||||
ProgressBar progressBar;
|
ProgressBar progressBar;
|
||||||
@BindView(R.id.trivia_status_bar)
|
@BindView(R.id.trivia_status_bar)
|
||||||
LinearLayout triviaStatusBar;
|
LinearLayout triviaStatusBar;
|
||||||
@BindView(R.id.text_question_category)
|
@BindView(R.id.text_question_category)
|
||||||
TextView textViewQuestionCategory;
|
TextView textViewQuestionCategory;
|
||||||
@BindView(R.id.text_question_difficulty)
|
@BindView(R.id.text_question_difficulty)
|
||||||
TextView textViewQuestionDifficulty;
|
TextView textViewQuestionDifficulty;
|
||||||
@BindView(R.id.text_question_progress)
|
@BindView(R.id.text_question_progress)
|
||||||
TextView textViewQuestionProgress;
|
TextView textViewQuestionProgress;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_trivia_game);
|
setContentView(R.layout.activity_trivia_game);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
if (savedInstanceState != null) {
|
ActionBar actionBar = getSupportActionBar();
|
||||||
this.game = (TriviaGame) savedInstanceState.getSerializable(STATE_TRIVIA_GAME);
|
if (actionBar != null)
|
||||||
} else {
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||||
Bundle bundle = getIntent().getExtras();
|
|
||||||
assert bundle != null;
|
|
||||||
TriviaQuery query = (TriviaQuery) bundle.get(EXTRA_TRIVIA_QUERY);
|
|
||||||
|
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
if (savedInstanceState != null) {
|
||||||
|
this.game = (TriviaGame) savedInstanceState.getSerializable(STATE_TRIVIA_GAME);
|
||||||
|
} else {
|
||||||
|
Bundle bundle = getIntent().getExtras();
|
||||||
|
assert bundle != null;
|
||||||
|
TriviaQuery query = (TriviaQuery) bundle.get(EXTRA_TRIVIA_QUERY);
|
||||||
|
|
||||||
DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask();
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
task.setReceiver(this);
|
|
||||||
task.execute(query);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask();
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
task.setReceiver(this);
|
||||||
super.onSaveInstanceState(outState);
|
task.execute(query);
|
||||||
outState.putSerializable(STATE_TRIVIA_GAME, this.game);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_trivia_game);
|
super.onSaveInstanceState(outState);
|
||||||
|
outState.putSerializable(STATE_TRIVIA_GAME, this.game);
|
||||||
|
}
|
||||||
|
|
||||||
if (fragment instanceof TriviaGameErrorFragment) {
|
@Override
|
||||||
super.onBackPressed();
|
public void onBackPressed() {
|
||||||
} else {
|
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_trivia_game);
|
||||||
new AlertDialog.Builder(this)
|
|
||||||
.setTitle(R.string.ui_quit_game)
|
|
||||||
.setMessage(R.string.ui_quit_game_msg)
|
|
||||||
.setPositiveButton(android.R.string.yes, (dialog, which) ->
|
|
||||||
TriviaGameActivity.super.onBackPressed())
|
|
||||||
.setNegativeButton(android.R.string.no, (dialog, which) -> {
|
|
||||||
})
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onTriviaQuestionsDownloaded(String json) {
|
if (fragment instanceof TriviaGameErrorFragment) {
|
||||||
if (json == null) {
|
super.onBackPressed();
|
||||||
onNetworkError();
|
} else {
|
||||||
return;
|
new AlertDialog.Builder(this)
|
||||||
} else {
|
.setTitle(R.string.ui_quit_game)
|
||||||
try {
|
.setMessage(R.string.ui_quit_game_msg)
|
||||||
this.game = new TriviaGame(ApiUtil.jsonToQuestionArray(json));
|
.setPositiveButton(android.R.string.yes, (dialog, which) ->
|
||||||
} catch (NoTriviaResultsException e) {
|
TriviaGameActivity.super.onBackPressed())
|
||||||
onNoTriviaResults();
|
.setNegativeButton(android.R.string.no, (dialog, which) -> {
|
||||||
return;
|
})
|
||||||
}
|
.show();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Setup game layout
|
@Override
|
||||||
progressBar.setVisibility(View.GONE);
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
triviaStatusBar.setVisibility(View.VISIBLE);
|
switch (item.getItemId()) {
|
||||||
updateStatusBar();
|
case android.R.id.home:
|
||||||
updateTriviaQuestion();
|
finish();
|
||||||
}
|
return true;
|
||||||
|
default:
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void updateStatusBar() {
|
public void onTriviaQuestionsDownloaded(String json) {
|
||||||
String progress = getResources().getString(R.string.ui_question_progress,
|
if (json == null) {
|
||||||
game.getQuestionProgress(), game.getQuestionsCount());
|
onNetworkError();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
this.game = new TriviaGame(ApiUtil.jsonToQuestionArray(json));
|
||||||
|
} catch (NoTriviaResultsException e) {
|
||||||
|
onNoTriviaResults();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String category = (game.getCurrentQuestion().getCategory() != null)
|
// Setup game layout
|
||||||
? game.getCurrentQuestion().getCategory().toString() : "";
|
progressBar.setVisibility(View.GONE);
|
||||||
|
triviaStatusBar.setVisibility(View.VISIBLE);
|
||||||
|
updateStatusBar();
|
||||||
|
updateTriviaQuestion();
|
||||||
|
}
|
||||||
|
|
||||||
String difficulty = game.getCurrentQuestion().getDifficulty().toString();
|
private void updateStatusBar() {
|
||||||
|
String progress = getResources().getString(R.string.ui_question_progress,
|
||||||
|
game.getQuestionProgress(), game.getQuestionsCount());
|
||||||
|
|
||||||
textViewQuestionProgress.setText(progress);
|
String category = (game.getCurrentQuestion().getCategory() != null)
|
||||||
textViewQuestionCategory.setText(category);
|
? game.getCurrentQuestion().getCategory().toString() : "";
|
||||||
textViewQuestionDifficulty.setText(difficulty);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateTriviaQuestion() {
|
String difficulty = game.getCurrentQuestion().getDifficulty().toString();
|
||||||
Fragment fragment = TriviaQuestionFragment.newInstance();
|
|
||||||
getSupportFragmentManager().beginTransaction()
|
|
||||||
.replace(R.id.frame_trivia_game, fragment)
|
|
||||||
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
|
||||||
.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onNetworkError() {
|
textViewQuestionProgress.setText(progress);
|
||||||
String msg = getResources().getString(R.string.error_network);
|
textViewQuestionCategory.setText(category);
|
||||||
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
|
textViewQuestionDifficulty.setText(difficulty);
|
||||||
|
}
|
||||||
|
|
||||||
// Hide the progress bar.
|
private void updateTriviaQuestion() {
|
||||||
progressBar.setVisibility(View.GONE);
|
Fragment fragment = TriviaQuestionFragment.newInstance();
|
||||||
|
getSupportFragmentManager().beginTransaction()
|
||||||
|
.replace(R.id.frame_trivia_game, fragment)
|
||||||
|
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
||||||
|
.commit();
|
||||||
|
}
|
||||||
|
|
||||||
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
private void onNetworkError() {
|
||||||
ft.replace(R.id.frame_trivia_game, errorFragment);
|
String msg = getResources().getString(R.string.error_network);
|
||||||
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
|
||||||
ft.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onNoTriviaResults() {
|
// Hide the progress bar.
|
||||||
String msg = getResources().getString(R.string.error_no_trivia_results);
|
progressBar.setVisibility(View.GONE);
|
||||||
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
|
|
||||||
|
|
||||||
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
||||||
ft.replace(R.id.frame_trivia_game, errorFragment);
|
ft.replace(R.id.frame_trivia_game, errorFragment);
|
||||||
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
||||||
ft.commit();
|
ft.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TriviaQuestion getCurrentQuestion() {
|
private void onNoTriviaResults() {
|
||||||
return this.game.getCurrentQuestion();
|
String msg = getResources().getString(R.string.error_no_trivia_results);
|
||||||
}
|
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
|
||||||
|
|
||||||
public void onAnswerClick(Button answer, Button correctAnswer) {
|
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
|
||||||
boolean guess = game.nextQuestion(answer.getText().toString());
|
ft.replace(R.id.frame_trivia_game, errorFragment);
|
||||||
|
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
||||||
|
ft.commit();
|
||||||
|
}
|
||||||
|
|
||||||
final int green = getResources().getColor(R.color.colorAccentGreen);
|
public TriviaQuestion getCurrentQuestion() {
|
||||||
int color = guess ? green
|
return this.game.getCurrentQuestion();
|
||||||
: getResources().getColor(R.color.colorAccentRed);
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
public void onAnswerClick(Button answer, Button correctAnswer) {
|
||||||
ColorStateList stateList = ColorStateList.valueOf(color);
|
boolean guess = game.nextQuestion(answer.getText().toString());
|
||||||
answer.setBackgroundTintList(stateList);
|
|
||||||
|
|
||||||
if (!guess) {
|
final int green = getResources().getColor(R.color.colorAccentGreen);
|
||||||
final ColorStateList greenStateList = ColorStateList.valueOf(green);
|
int color = guess ? green
|
||||||
correctAnswer.setBackgroundTintList(greenStateList);
|
: getResources().getColor(R.color.colorAccentRed);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
answer.getBackground().getCurrent().setColorFilter(
|
|
||||||
new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
|
|
||||||
|
|
||||||
if (!guess)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
correctAnswer.getBackground().getCurrent().setColorFilter(
|
ColorStateList stateList = ColorStateList.valueOf(color);
|
||||||
new PorterDuffColorFilter(green, PorterDuff.Mode.MULTIPLY));
|
answer.setBackgroundTintList(stateList);
|
||||||
}
|
|
||||||
|
|
||||||
SoundUtil.playSound(this, guess ?
|
if (!guess) {
|
||||||
SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG);
|
final ColorStateList greenStateList = ColorStateList.valueOf(green);
|
||||||
|
correctAnswer.setBackgroundTintList(greenStateList);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
answer.getBackground().getCurrent().setColorFilter(
|
||||||
|
new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
|
||||||
|
|
||||||
new Handler().postDelayed(() -> {
|
if (!guess)
|
||||||
if (game.isDone()) {
|
correctAnswer.getBackground().getCurrent().setColorFilter(
|
||||||
Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class);
|
new PorterDuffColorFilter(green, PorterDuff.Mode.MULTIPLY));
|
||||||
intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game);
|
}
|
||||||
startActivity(intent);
|
|
||||||
finish();
|
|
||||||
} else {
|
|
||||||
updateStatusBar();
|
|
||||||
updateTriviaQuestion();
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class DownloadTriviaQuestionsTask extends AsyncTask<TriviaQuery, Integer, String> {
|
SoundUtil.playSound(this, guess ?
|
||||||
private IDownloadTriviaQuestionReceiver receiver;
|
SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG);
|
||||||
|
|
||||||
@Override
|
new Handler().postDelayed(() -> {
|
||||||
protected String doInBackground(TriviaQuery... query) {
|
if (game.isDone()) {
|
||||||
String json;
|
Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class);
|
||||||
try {
|
intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game);
|
||||||
json = ApiUtil.GET(query[0]);
|
startActivity(intent);
|
||||||
} catch (IOException e) {
|
finish();
|
||||||
return null;
|
} else {
|
||||||
}
|
updateStatusBar();
|
||||||
return json;
|
updateTriviaQuestion();
|
||||||
}
|
}
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
private static class DownloadTriviaQuestionsTask extends AsyncTask<TriviaQuery, Integer, String> {
|
||||||
protected void onPostExecute(String json) {
|
private IDownloadTriviaQuestionReceiver receiver;
|
||||||
receiver.onTriviaQuestionsDownloaded(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setReceiver(IDownloadTriviaQuestionReceiver receiver) {
|
@Override
|
||||||
this.receiver = receiver;
|
protected String doInBackground(TriviaQuery... query) {
|
||||||
}
|
String json;
|
||||||
}
|
try {
|
||||||
|
json = ApiUtil.GET(query[0]);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String json) {
|
||||||
|
receiver.onTriviaQuestionsDownloaded(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setReceiver(IDownloadTriviaQuestionReceiver receiver) {
|
||||||
|
this.receiver = receiver;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue