Added back button on game activity

This commit is contained in:
luca0N! 2021-03-03 22:18:11 -03:00
parent fb2d81550c
commit e8c2504a33
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 178 additions and 161 deletions

View File

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