Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kudisprivate.student.activity.mapel.latihan;
- import android.annotation.SuppressLint;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.os.Build;
- import android.os.CountDownTimer;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import androidx.recyclerview.widget.GridLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import com.bumptech.glide.Glide;
- import com.bumptech.glide.request.RequestOptions;
- import com.github.chrisbanes.photoview.PhotoView;
- import com.google.android.material.dialog.MaterialAlertDialogBuilder;
- import com.kudisprivate.student.R;
- import com.kudisprivate.student.activity.mapel.model.DetailSoal;
- import com.kudisprivate.student.activity.mapel.model.ResponseSoalLatihan;
- import com.kudisprivate.student.config.manager.AlertDialogManager;
- import com.kudisprivate.student.config.manager.BackgroundManager;
- import com.kudisprivate.student.config.manager.ItemDecorationColumns;
- import com.kudisprivate.student.config.manager.SessionManager;
- import com.kudisprivate.student.config.network.CheckError;
- import com.kudisprivate.student.config.network.Connection;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Locale;
- import java.util.Objects;
- import java.util.concurrent.TimeUnit;
- import okhttp3.ResponseBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class Soal_LatihanController {
- private static final String TAG = Soal_LatihanController.class.getSimpleName();
- private Activity activity;
- private TextView tv_waktu, tv_no_urut, tv_soal, ket_tv;
- private LinearLayout constraintLayout2;
- private ImageView image_soal;
- private TextView pilihan1, pilihan2, pilihan3, pilihan4, pilihan5;
- private RadioGroup rg_pilihan;
- private RadioButton radioButton1, radioButton2, radioButton3, radioButton4, radioButton5;
- private Button button_prev, button_next, button_selesai;
- private RecyclerView recyclerView;
- private Object pil1, pil2, pil3, pil4, pil5;
- public int latihan_id;
- private CounterClass mCountDownTimer;
- private int id_jawaban_dipilih[] = null;
- private String jawaban_dipilih[] = null;
- private String waktu_pilihan[] = null;
- private int jawabanYgBenar[] = null;
- private boolean cekPertanyaan = false;
- private int urutanPertanyaan = 0;
- private List<DetailSoal> list;
- private int waktu;
- private int waktu_sisa;
- private boolean isPaused = false;
- private boolean isCanceled = false;
- public Soal_LatihanController(Activity activity) {
- this.activity = activity;
- }
- public void bind() {
- constraintLayout2 = activity.findViewById(R.id.constraintLayout2);
- tv_waktu = activity.findViewById(R.id.tv_waktu);
- tv_no_urut = activity.findViewById(R.id.tv_no_urut);
- tv_soal = activity.findViewById(R.id.tv_soal);
- image_soal = activity.findViewById(R.id.image_soal);
- ket_tv = activity.findViewById(R.id.label_image);
- pilihan1 = activity.findViewById(R.id.pilihan1);
- pilihan2 = activity.findViewById(R.id.pilihan2);
- pilihan3 = activity.findViewById(R.id.pilihan3);
- pilihan4 = activity.findViewById(R.id.pilihan4);
- pilihan5 = activity.findViewById(R.id.pilihan5);
- rg_pilihan = activity.findViewById(R.id.rg_pilihan);
- radioButton1 = activity.findViewById(R.id.radioButton1);
- radioButton2 = activity.findViewById(R.id.radioButton2);
- radioButton3 = activity.findViewById(R.id.radioButton3);
- radioButton4 = activity.findViewById(R.id.radioButton4);
- radioButton5 = activity.findViewById(R.id.radioButton5);
- button_prev = activity.findViewById(R.id.button_prev);
- button_selesai = activity.findViewById(R.id.button_selesai);
- button_next = activity.findViewById(R.id.button_next);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- constraintLayout2.setBackground(new BackgroundManager().rounded_fill("#FFFFFF", 16.0f));
- }
- image_soal.setVisibility(View.GONE);
- button_prev.setOnClickListener(this::klikSebelum);
- button_selesai.setOnClickListener(this::klikSelesai);
- button_next.setOnClickListener(this::klikBerikut);
- startLatihan(0);
- }
- public void binView(View view) {
- recyclerView = view.findViewById(R.id.recylerview);
- startLatihan(1);
- }
- public void startLatihan(int i) {
- JSONObject jsonObject = new JSONObject();
- try {
- jsonObject.put("latihan", latihan_id);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- new Connection().getLatihanEndpoint().soal_latihan(new SessionManager(activity).get_token(), jsonObject.toString())
- .enqueue(new Callback<ResponseSoalLatihan>() {
- @Override
- public void onResponse(Call<ResponseSoalLatihan> call, Response<ResponseSoalLatihan> response) {
- try {
- if (response.isSuccessful()) {
- ResponseSoalLatihan responseSoalLatihan = response.body();
- if (responseSoalLatihan.getSuccess() == true) {
- waktu = responseSoalLatihan.getDataSoal().getLatihan_detik();
- list = responseSoalLatihan.getDataSoal().getDetailSoal();
- long latihan_id = responseSoalLatihan.getDataSoal().getLatihanId();
- id_jawaban_dipilih = new int[list.size()];
- jawaban_dipilih = new String[list.size()];
- waktu_pilihan = new String[list.size()];
- Arrays.fill(id_jawaban_dipilih, -1);
- Arrays.fill(jawaban_dipilih, "");
- Arrays.fill(waktu_pilihan, "");
- jawabanYgBenar = new int[list.size()];
- Arrays.fill(jawabanYgBenar, -1);
- if (i == 0) {
- setUpSoal(list);
- } else {
- SoalGridAdapter adapter = new SoalGridAdapter(list, activity, latihan_id);
- GridLayoutManager manager = new GridLayoutManager(activity, 3,
- GridLayoutManager.VERTICAL, false);
- recyclerView.setHasFixedSize(true);
- recyclerView.setLayoutManager(manager);
- recyclerView.addItemDecoration(new ItemDecorationColumns(1, 4));
- recyclerView.setAdapter(adapter);
- }
- } else {
- new CheckError(activity).errorCode(responseSoalLatihan.getCode(), responseSoalLatihan.getMessage());
- }
- } else {
- ResponseBody responseError = response.errorBody();
- JSONObject object = new JSONObject(responseError.string());
- String message = object.getString("message");
- AlertDialogManager.showAlertDialog(activity, message)
- .setNegativeButton("Coba Lagi", (dialog, which) -> dialog.dismiss()).show();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(Call<ResponseSoalLatihan> call, Throwable t) {
- AlertDialogManager.retroOnFailure(activity, t);
- }
- });
- }
- private void setUpSoal(List<DetailSoal> list) {
- Collections.shuffle(list);
- tunjukanPertanyaan(0, cekPertanyaan);
- }
- /** Tunjukan pertanyaan dan initiallize**/
- private void tunjukanPertanyaan(int urutan_soal_soal, boolean cekPertanyaan) {
- button_selesai.setEnabled(false);
- button_selesai.setVisibility(View.GONE);
- // if(urutan_soal_soal == 0)
- // setup waktu
- // setUpWaktu();
- try {
- rg_pilihan.clearCheck();
- String no_urut = list.get(urutan_soal_soal).getId();
- String soal = list.get(urutan_soal_soal).getSoal();
- //set soal
- tv_soal.setText(soal);
- //set gambar soal
- if (!list.get(urutan_soal_soal).getGambar().equals("")) {
- RequestOptions options = new RequestOptions()
- .centerInside()
- .placeholder(R.mipmap.ic_launcher_round)
- .error(R.mipmap.ic_launcher_round);
- Glide.with(activity).load(list.get(urutan_soal_soal).getGambar()).apply(options).into(image_soal);
- image_soal.setVisibility(View.VISIBLE);
- ket_tv.setVisibility(View.VISIBLE);
- image_soal.setOnClickListener(v -> {
- View view = activity.getLayoutInflater().inflate(R.layout.dialog_custom_layout, null);
- PhotoView imageView = view.findViewById(R.id.imageView);
- RequestOptions opt = new RequestOptions()
- .placeholder(R.mipmap.ic_launcher_round)
- .error(R.mipmap.ic_launcher_round);
- Glide.with(view).load(list.get(urutan_soal_soal).getGambar()).apply(opt).into(imageView);
- new MaterialAlertDialogBuilder(activity)
- .setView(view)
- .setCancelable(true)
- .show();
- });
- } else {
- image_soal.setVisibility(View.GONE);
- ket_tv.setVisibility(View.GONE);
- }
- //set pilihan jawaban
- rg_pilihan.check(-1);
- pil1 = list.get(urutan_soal_soal).getPilihan().getA();
- pil2 = list.get(urutan_soal_soal).getPilihan().getB();
- pil3 = list.get(urutan_soal_soal).getPilihan().getC();
- pil4 = list.get(urutan_soal_soal).getPilihan().getD();
- pil5 = list.get(urutan_soal_soal).getPilihan().getE();
- if (pil1 == null && pil2 == null && pil3 == null && pil4 == null && pil5 == null) {
- pilihan1.setVisibility(View.GONE);
- pilihan2.setVisibility(View.GONE);
- pilihan3.setVisibility(View.GONE);
- pilihan4.setVisibility(View.GONE);
- pilihan5.setVisibility(View.GONE);
- } else {
- pilihan1.setText("A. " +list.get(urutan_soal_soal).getPilihan().getA());
- pilihan2.setText("B. " +list.get(urutan_soal_soal).getPilihan().getB());
- pilihan3.setText("C. " +list.get(urutan_soal_soal).getPilihan().getC());
- pilihan4.setText("D. " +list.get(urutan_soal_soal).getPilihan().getD());
- pilihan5.setText("E. " +list.get(urutan_soal_soal).getPilihan().getE());
- }
- Log.d(TAG, id_jawaban_dipilih[urutan_soal_soal] + "");
- if (id_jawaban_dipilih[urutan_soal_soal] == 1) {
- rg_pilihan.check(R.id.radioButton1);
- jawaban_dipilih[urutan_soal_soal] = list.get(urutan_soal_soal).getId();
- Log.e(TAG, "tunjukanPertanyaan: " + list.get(urutan_soal_soal).getId());
- }
- if (id_jawaban_dipilih[urutan_soal_soal] == 2) {
- rg_pilihan.check(R.id.radioButton2);
- jawaban_dipilih[urutan_soal_soal] = list.get(urutan_soal_soal).getId();
- Log.e(TAG, "tunjukanPertanyaan: " + list.get(urutan_soal_soal).getId());
- }
- if (id_jawaban_dipilih[urutan_soal_soal] == 3) {
- rg_pilihan.check(R.id.radioButton3);
- jawaban_dipilih[urutan_soal_soal] = list.get(urutan_soal_soal).getId();
- Log.e(TAG, "tunjukanPertanyaan: " + list.get(urutan_soal_soal).getId());
- }
- if (id_jawaban_dipilih[urutan_soal_soal] == 4) {
- rg_pilihan.check(R.id.radioButton4);
- jawaban_dipilih[urutan_soal_soal] = list.get(urutan_soal_soal).getId();
- Log.e(TAG, "tunjukanPertanyaan: " + list.get(urutan_soal_soal).getId());
- }
- if (id_jawaban_dipilih[urutan_soal_soal] == 5) {
- rg_pilihan.check(R.id.radioButton5);
- jawaban_dipilih[urutan_soal_soal] = list.get(urutan_soal_soal).getId();
- Log.e(TAG, "tunjukanPertanyaan: " + list.get(urutan_soal_soal).getId());
- }
- pasangLabelDanNomorUrut();
- if (urutan_soal_soal == (list.size() - 1)) {
- button_next.setEnabled(false);
- button_next.setVisibility(View.INVISIBLE);
- button_selesai.setEnabled(true);
- button_selesai.setVisibility(View.VISIBLE);
- }
- if (urutan_soal_soal == 0)
- button_prev.setEnabled(false);
- if (urutan_soal_soal > 0) {
- button_prev.setEnabled(true);
- }
- if (urutan_soal_soal < (list.size() - 1)) {
- button_next.setEnabled(true);
- button_next.setVisibility(View.VISIBLE);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
- }
- }
- public void pilihSoal(View view) {
- Log.e(TAG, "pilihSoal: " + waktu );
- }
- private void pasangLabelDanNomorUrut() {
- tv_no_urut.setText("Soal Ke " + (urutanPertanyaan + 1)+ " dari "
- + list.size() + " soal");
- }
- public void setUpWaktu() {
- // mCountDownTimer = new CounterClass(240000, 1000);
- int p2 = 600 / 60;
- // Log.e(TAG, "setUpWaktu: "+ p2 );
- Log.e(TAG, "setUpWaktu: " + waktu );
- //
- mCountDownTimer = new CounterClass(activity.getIntent().getIntExtra("waktu",0)*1000, 1000);
- mCountDownTimer.start();
- }
- public class CounterClass extends CountDownTimer {
- public CounterClass(long millisInFuture, long countDownInterval) {
- super(millisInFuture, countDownInterval);
- }
- @Override
- public void onFinish() {
- AlertDialogManager.showAlertDialog(activity, "Anda telah kehabisan waktu, soal yang belum terjawab akan tetap dismipan.")
- .setPositiveButton("oke", (dialog, which) -> {
- aturJawaban_nya();
- SimpanJawaban simpanJawaban = new SimpanJawaban(activity);
- simpanJawaban.setLatihan_id(latihan_id);
- simpanJawaban.setJawaban(Arrays.toString(jawaban_dipilih));
- simpanJawaban.simpan();
- dialog.dismiss();
- }).show();
- Log.e(TAG, "onFinish: hayoo" );
- }
- @SuppressLint("NewApi")
- @TargetApi(Build.VERSION_CODES.GINGERBREAD)
- @Override
- public void onTick(long millisUntilFinished) {
- long millis = millisUntilFinished;
- String hms = String.format(
- "%02d:%02d:%02d",
- TimeUnit.MILLISECONDS.toHours(millis),
- TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
- TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
- tv_waktu.setText(hms);
- }
- }
- private void klikSebelum(View view) {
- aturJawaban_nya();
- urutanPertanyaan--;
- if (urutanPertanyaan < 0)
- urutanPertanyaan = 0;
- tunjukanPertanyaan(urutanPertanyaan, cekPertanyaan);
- }
- private void klikSelesai(View view) {
- aturJawaban_nya();
- AlertDialogManager.showAlertDialog(activity, "Anda akan menyudahi Latihan ini.")
- .setPositiveButton("Simpan", (dialog, which) -> {
- mCountDownTimer.cancel();
- SimpanJawaban simpanJawaban = new SimpanJawaban(activity);
- simpanJawaban.setLatihan_id(latihan_id);
- simpanJawaban.setJawaban(Arrays.toString(jawaban_dipilih));
- simpanJawaban.simpan();
- })
- .setNegativeButton("Tutup", (dialog, which) -> {
- // mCountDownTimer.cancel();
- // cekPertanyaan = false;
- dialog.dismiss();
- }).show();
- }
- private void aturJawaban_nya() {
- JSONObject jsonObject = new JSONObject();
- try {
- if (radioButton1.isChecked()) {
- id_jawaban_dipilih[urutanPertanyaan] = 1;
- jsonObject.put("soal_id",list.get(urutanPertanyaan).getId());
- jsonObject.put("jawaban", "A");
- jawaban_dipilih[urutanPertanyaan] = jsonObject.toString();
- }
- if (radioButton2.isChecked()) {
- id_jawaban_dipilih[urutanPertanyaan] = 2;
- jsonObject.put("soal_id",list.get(urutanPertanyaan).getId());
- jsonObject.put("jawaban", "B");
- jawaban_dipilih[urutanPertanyaan] = jsonObject.toString();
- }
- if (radioButton3.isChecked()) {
- id_jawaban_dipilih[urutanPertanyaan] = 3;
- jsonObject.put("soal_id",list.get(urutanPertanyaan).getId());
- jsonObject.put("jawaban", "C");
- jawaban_dipilih[urutanPertanyaan] = jsonObject.toString();
- }
- if (radioButton4.isChecked()) {
- id_jawaban_dipilih[urutanPertanyaan] = 4;
- jsonObject.put("soal_id",list.get(urutanPertanyaan).getId());
- jsonObject.put("jawaban", "D");
- jawaban_dipilih[urutanPertanyaan] = jsonObject.toString();
- }
- if (radioButton5.isChecked()) {
- id_jawaban_dipilih[urutanPertanyaan] = 5;
- jsonObject.put("soal_id",list.get(urutanPertanyaan).getId());
- jsonObject.put("jawaban", "E");
- jawaban_dipilih[urutanPertanyaan] = jsonObject.toString();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Log.e(TAG, "Hasil >> "+ Arrays.toString(jawaban_dipilih));
- List<String> ls = new ArrayList<>();
- ls.add(Arrays.toString(jawaban_dipilih));
- Log.e(TAG, "aturJawaban_nya: "+ ls );
- Log.d("", Arrays.toString(jawabanYgBenar));
- }
- private void klikBerikut(View view) {
- aturJawaban_nya();
- urutanPertanyaan++;
- if (urutanPertanyaan >= list.size()) {
- urutanPertanyaan = list.size() - 1;
- }
- tunjukanPertanyaan(urutanPertanyaan, cekPertanyaan);
- Log.e(TAG, "klikBerikut waktu: " + waktu_sisa);
- }
- }
Add Comment
Please, Sign In to add comment