|
| 1 | +package fr.free.nrw.commons.quiz; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.DialogInterface; |
| 5 | +import android.content.Intent; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.graphics.drawable.VectorDrawableCompat; |
| 8 | +import android.support.v7.app.AlertDialog; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.support.v7.widget.Toolbar; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.ProgressBar; |
| 13 | +import android.widget.RadioButton; |
| 14 | +import android.widget.TextView; |
| 15 | + |
| 16 | +import com.facebook.drawee.backends.pipeline.Fresco; |
| 17 | +import com.facebook.drawee.drawable.ProgressBarDrawable; |
| 18 | +import com.facebook.drawee.generic.GenericDraweeHierarchy; |
| 19 | +import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder; |
| 20 | +import com.facebook.drawee.view.SimpleDraweeView; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | + |
| 24 | +import butterknife.BindView; |
| 25 | +import butterknife.ButterKnife; |
| 26 | +import butterknife.OnClick; |
| 27 | +import fr.free.nrw.commons.R; |
| 28 | +import fr.free.nrw.commons.contributions.ContributionsActivity; |
| 29 | + |
| 30 | +public class QuizActivity extends AppCompatActivity { |
| 31 | + |
| 32 | + @BindView(R.id.question_image) SimpleDraweeView imageView; |
| 33 | + @BindView(R.id.question_text) TextView questionText; |
| 34 | + @BindView(R.id.question_title) TextView questionTitle; |
| 35 | + @BindView(R.id.quiz_positive_answer) RadioButton positiveAnswer; |
| 36 | + @BindView(R.id.quiz_negative_answer) RadioButton negativeAnswer; |
| 37 | + @BindView(R.id.toolbar) Toolbar toolbar; |
| 38 | + |
| 39 | + private QuizController quizController = new QuizController(); |
| 40 | + private ArrayList<QuizQuestion> quiz = new ArrayList<QuizQuestion>(); |
| 41 | + private int questionIndex = 0; |
| 42 | + private int score; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void onCreate(Bundle savedInstanceState) { |
| 46 | + super.onCreate(savedInstanceState); |
| 47 | + setContentView(R.layout.activity_quiz); |
| 48 | + Fresco.initialize(this); |
| 49 | + |
| 50 | + quizController.initialize(this); |
| 51 | + ButterKnife.bind(this); |
| 52 | + setSupportActionBar(toolbar); |
| 53 | + displayQuestion(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * to move to next question and check whether answer is selected or not |
| 58 | + */ |
| 59 | + @OnClick(R.id.next_button) |
| 60 | + public void setNextQuestion(){ |
| 61 | + if( questionIndex <= quiz.size() && (positiveAnswer.isChecked() || negativeAnswer.isChecked())) { |
| 62 | + evaluateScore(); |
| 63 | + } else if ( !positiveAnswer.isChecked() && !negativeAnswer.isChecked()){ |
| 64 | + AlertDialog.Builder alert = new AlertDialog.Builder(this); |
| 65 | + alert.setTitle(getResources().getString(R.string.warning)); |
| 66 | + alert.setMessage(getResources().getString(R.string.warning_for_no_answer)); |
| 67 | + alert.setPositiveButton(R.string.continue_message, new DialogInterface.OnClickListener() { |
| 68 | + @Override |
| 69 | + public void onClick(DialogInterface dialog, int which) { |
| 70 | + dialog.dismiss(); |
| 71 | + } |
| 72 | + }); |
| 73 | + AlertDialog dialog = alert.create(); |
| 74 | + dialog.show(); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * to give warning before ending quiz |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public void onBackPressed() { |
| 84 | + AlertDialog.Builder alert = new AlertDialog.Builder(this); |
| 85 | + alert.setTitle(getResources().getString(R.string.warning)); |
| 86 | + alert.setMessage(getResources().getString(R.string.quiz_back_button)); |
| 87 | + alert.setPositiveButton(R.string.continue_message, new DialogInterface.OnClickListener() { |
| 88 | + @Override |
| 89 | + public void onClick(DialogInterface dialog, int which) { |
| 90 | + Intent i = new Intent(QuizActivity.this, QuizResultActivity.class); |
| 91 | + dialog.dismiss(); |
| 92 | + i.putExtra("QuizResult",score); |
| 93 | + startActivity(i); |
| 94 | + } |
| 95 | + }); |
| 96 | + alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 97 | + @Override |
| 98 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 99 | + dialogInterface.dismiss(); |
| 100 | + } |
| 101 | + }); |
| 102 | + AlertDialog dialog = alert.create(); |
| 103 | + dialog.show(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * to display the question |
| 108 | + */ |
| 109 | + public void displayQuestion() { |
| 110 | + quiz = quizController.getQuiz(); |
| 111 | + questionText.setText(quiz.get(questionIndex).getQuestion()); |
| 112 | + questionTitle.setText(getResources().getString(R.string.question)+quiz.get(questionIndex).getQuestionNumber()); |
| 113 | + imageView.setHierarchy(GenericDraweeHierarchyBuilder |
| 114 | + .newInstance(getResources()) |
| 115 | + .setFailureImage(VectorDrawableCompat.create(getResources(), |
| 116 | + R.drawable.ic_error_outline_black_24dp, getTheme())) |
| 117 | + .setProgressBarImage(new ProgressBarDrawable()) |
| 118 | + .build()); |
| 119 | + |
| 120 | + imageView.setImageURI(quiz.get(questionIndex).getUrl()); |
| 121 | + new RadioGroupHelper(this, R.id.quiz_positive_answer, R.id.quiz_negative_answer); |
| 122 | + positiveAnswer.setChecked(false); |
| 123 | + negativeAnswer.setChecked(false); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * to evaluate score and check whether answer is correct or wrong |
| 128 | + */ |
| 129 | + public void evaluateScore() { |
| 130 | + if((quiz.get(questionIndex).isAnswer() && positiveAnswer.isChecked()) || |
| 131 | + (!quiz.get(questionIndex).isAnswer() && negativeAnswer.isChecked()) ){ |
| 132 | + customAlert(getResources().getString(R.string.correct),quiz.get(questionIndex).getAnswerMessage() ); |
| 133 | + score++; |
| 134 | + } else{ |
| 135 | + customAlert(getResources().getString(R.string.wrong), quiz.get(questionIndex).getAnswerMessage()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * to display explanation after each answer, update questionIndex and move to next question |
| 141 | + * @param title |
| 142 | + * @param Message |
| 143 | + */ |
| 144 | + public void customAlert(String title, String Message) { |
| 145 | + AlertDialog.Builder alert = new AlertDialog.Builder(this); |
| 146 | + alert.setTitle(title); |
| 147 | + alert.setMessage(Message); |
| 148 | + alert.setPositiveButton(R.string.continue_message, new DialogInterface.OnClickListener() { |
| 149 | + @Override |
| 150 | + public void onClick(DialogInterface dialog, int which) { |
| 151 | + questionIndex++; |
| 152 | + if(questionIndex == quiz.size()){ |
| 153 | + Intent i = new Intent(QuizActivity.this, QuizResultActivity.class); |
| 154 | + dialog.dismiss(); |
| 155 | + i.putExtra("QuizResult",score); |
| 156 | + startActivity(i); |
| 157 | + }else { |
| 158 | + displayQuestion(); |
| 159 | + } |
| 160 | + } |
| 161 | + }); |
| 162 | + AlertDialog dialog = alert.create(); |
| 163 | + dialog.show(); |
| 164 | + } |
| 165 | +} |
0 commit comments