| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package com.example.programingquiz;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import com.google.android.material.snackbar.Snackbar;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.Collections;
- public class BeginnerTaskActivity extends AppCompatActivity {
- TextView questionTV, contentTV;
- Button aBtn, bBtn;
- CategoryActivity.DifficultyType difficultyType;
- String login, taskCategory;
- LoginActivity.LoginType loginType;
- long taskID;
- @Override
- public void onBackPressed() {}
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_beginner_task);
- questionTV = findViewById(R.id.questionTV);
- contentTV = findViewById(R.id.contentTV);
- aBtn = findViewById(R.id.aBtn);
- bBtn = findViewById(R.id.bBtn);
- aBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- openReportActivity(aBtn.getText().toString());
- }
- });
- bBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- openReportActivity(bBtn.getText().toString());
- }
- });
- getIntentExtras();
- drawTask();
- }
- private void getIntentExtras(){
- Intent intent = getIntent();
- login = intent.getStringExtra("login");
- loginType = (LoginActivity.LoginType) intent.getSerializableExtra("type");
- taskCategory = intent.getStringExtra("taskCategory").toLowerCase();
- difficultyType = (CategoryActivity.DifficultyType) intent.getSerializableExtra("difficultyType");
- }
- private void drawTask(){
- String url = StartActivity.URL + "api/drawTask/" + taskCategory + "/" + difficultyType.toString();
- RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
- JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- questionTV.setText(response.getString("question"));
- taskID = response.getInt("id");
- contentTV.setText(response.getString("content").replace("\\n", "\n").replace("\\t", "\t"));
- JSONArray array = response.getJSONArray("answers");
- ArrayList <String> answers = shuffleAnswers(array);
- aBtn.setText(answers.get(0));
- bBtn.setText(answers.get(1));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new com.android.volley.Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Snackbar.make(findViewById(android.R.id.content), "some error occurred, please try again later", Snackbar.LENGTH_LONG).show();
- String errorMsg = error.toString();
- System.out.println(errorMsg);
- }
- });
- queue.add(request);
- }
- private ArrayList<String> shuffleAnswers(JSONArray answers){
- ArrayList <String> result = new ArrayList<>();
- for(int i = 0; i < answers.length(); i++) {
- try {
- result.add(answers.getString(i));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- Collections.shuffle(result);
- return result;
- }
- private void openReportActivity(String userAnswer){
- Intent intent = new Intent(this, ReportActivity.class);
- intent.putExtra("login", login);
- intent.putExtra("type", loginType);
- intent.putExtra("taskID", taskID);
- intent.putExtra("difficultyType", difficultyType);
- intent.putExtra("taskCategory", taskCategory);
- intent.putExtra("userAnswer", userAnswer);
- intent.putExtra("aAnswer", aBtn.getText().toString());
- intent.putExtra("bAnswer", bBtn.getText().toString());
- startActivity(intent);
- }
- }
|