Advertisement
pabloducato

QuizOnlineActivityFault

Jul 6th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.27 KB | None | 0 0
  1. package com.example.quiz.AccountActivity;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.res.ColorStateList;
  7. import android.graphics.Color;
  8. import android.graphics.drawable.ColorDrawable;
  9. import android.net.ConnectivityManager;
  10. import android.net.NetworkInfo;
  11. import android.os.Bundle;
  12. import android.os.CountDownTimer;
  13. import android.os.Handler;
  14. import android.support.annotation.NonNull;
  15. import android.support.annotation.Nullable;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.view.MotionEvent;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import com.example.quiz.AccountActivity.Model.QuestionFirebase;
  24. import com.example.quiz.R;
  25. import com.google.firebase.database.DataSnapshot;
  26. import com.google.firebase.database.DatabaseError;
  27. import com.google.firebase.database.DatabaseReference;
  28. import com.google.firebase.database.FirebaseDatabase;
  29. import com.google.firebase.database.ValueEventListener;
  30.  
  31. import java.util.Locale;
  32.  
  33. public class QuizOnlineActivity extends AppCompatActivity {
  34.  
  35.     Button beback,b1,b2,b3,b4,accept,reset;
  36.     TextView question, timer, maintitle;
  37.  
  38.     private CountDownTimer countDownTimer;
  39.     private long timeLeftInMillis;
  40.     private static final long COUNTDOWN_IN_MILLIS = 61000;
  41.     private ColorStateList textColorDefaultCd;
  42.  
  43.  
  44.     int total = 1;
  45.     int questiontag = 0;
  46.     int correct = 0;
  47.     int wrong = 0;
  48.     //int accept_er = 0;
  49.  
  50.  
  51.     DatabaseReference databaseReference;
  52.  
  53.     @Override
  54.     protected void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.activity_quiz_online);
  57.  
  58.         timer = findViewById(R.id.timer);
  59.  
  60.         textColorDefaultCd = timer.getTextColors();
  61.  
  62.         b1 = findViewById(R.id.button1);
  63.         b2 = findViewById(R.id.button2);
  64.         b3 = findViewById(R.id.button3);
  65.         b4 = findViewById(R.id.button4);
  66.  
  67.         question = findViewById(R.id.question);
  68.  
  69.         maintitle = findViewById(R.id.maintitle);
  70.  
  71.         accept = findViewById(R.id.accept);
  72.  
  73.         reset = findViewById(R.id.reset);
  74.  
  75.         beback = findViewById(R.id.beback);
  76.         beback.setOnClickListener(new View.OnClickListener() {
  77.             @Override
  78.             public void onClick(View v) {
  79.                 beback.setBackgroundColor(getResources().getColor(R.color.mclaren));
  80.                 Handler handler = new Handler();
  81.                 handler.postDelayed(new Runnable() {
  82.                     @Override
  83.                     public void run() {
  84.                         beback.setBackgroundColor(Color.WHITE);
  85.                         Intent intent = new Intent(QuizOnlineActivity.this, ProfileActivity.class);
  86.                         startActivity(intent);
  87.                     }
  88.                 },50);
  89.  
  90.             }
  91.         });
  92.  
  93.         /*accept.setOnTouchListener(new View.OnTouchListener() {
  94.             @Override
  95.             public boolean onTouch(View v, MotionEvent event) {
  96.                 return false;
  97.             }
  98.         });*/
  99.  
  100.         accept.setOnClickListener(new View.OnClickListener() {
  101.             @Override
  102.             public void onClick(View v) {
  103.                 accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  104.                 maintitle.setVisibility(v.GONE);
  105.                 accept.setText("Potwierdź");
  106.                 beback.setText("Przerwij");
  107.                 b1.setVisibility(v.VISIBLE);
  108.                 b2.setVisibility(v.VISIBLE);
  109.                 b3.setVisibility(v.VISIBLE);
  110.                 b4.setVisibility(v.VISIBLE);
  111.                 question.setVisibility(v.VISIBLE);
  112.                 timer.setVisibility(v.VISIBLE);
  113.                 reset.setVisibility(v.VISIBLE);
  114.                 Handler handler = new Handler();
  115.                 handler.postDelayed(new Runnable() {
  116.                     @Override
  117.                     public void run() {
  118.                         accept.setBackgroundColor(Color.WHITE);
  119.                         updateQuestions();
  120.                     }
  121.                 },0);
  122.             }
  123.         });
  124.  
  125.     }
  126.  
  127.  
  128.  
  129.     private boolean isNetworkAvailable() {
  130.         ConnectivityManager connectivityManager
  131.                 = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  132.         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  133.         return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  134.     }
  135.  
  136.     private void startCountDown() {
  137.         countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
  138.             @Override
  139.             public void onTick(long millisUntilFinished) {
  140.                 timeLeftInMillis = millisUntilFinished;
  141.                 updateCountDownText();
  142.             }
  143.  
  144.             @Override
  145.             public void onFinish() {
  146.                 countDownTimer.cancel();
  147.                 //timeLeftInMillis = 60000; BRON BOZE!!!
  148.                 updateCountDownText();
  149.                 b1.setBackgroundColor(Color.WHITE);
  150.                 b2.setBackgroundColor(Color.WHITE);
  151.                 b3.setBackgroundColor(Color.WHITE);
  152.                 b4.setBackgroundColor(Color.WHITE);
  153.                 updateQuestions();
  154.             }
  155.         }.start();
  156.     }
  157.  
  158.     private void updateCountDownText() {
  159.         int minutes = (int) (timeLeftInMillis / 1000) / 60; //minutes
  160.         int seconds = (int) (timeLeftInMillis / 1000) % 60;
  161.         String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
  162.         timer.setText(timeFormatted);
  163.         if (timeLeftInMillis < 65000 && timeLeftInMillis >= 30000) {
  164.             timer.setTextColor(getResources().getColor(R.color.greengrenade));
  165.         } else if (timeLeftInMillis <= 30000 && timeLeftInMillis >= 15000) {
  166.             timer.setTextColor(getResources().getColor(R.color.timer));
  167.         } else if (timeLeftInMillis <= 15000 && timeLeftInMillis > 0) {
  168.             timer.setTextColor(getResources().getColor(R.color.ferrari)); //RED KOLOR CZERWONY
  169.         } else if (timeLeftInMillis == 0) {
  170.             updateQuestions();
  171.         } else {
  172.             timer.setTextColor(textColorDefaultCd);
  173.         }
  174.     }
  175.  
  176.     private void updateQuestions()
  177.     {
  178.         timeLeftInMillis = COUNTDOWN_IN_MILLIS;
  179.         if(isNetworkAvailable()==false)
  180.         {
  181.             Toast.makeText(QuizOnlineActivity.this, "Brak połączenia z internetem", Toast.LENGTH_SHORT).show();
  182.  
  183.         }
  184.         else if(isNetworkAvailable()==true && total==31)
  185.         {
  186.             countDownTimer.cancel();
  187.             Intent i = new Intent(getApplicationContext(), ResultActivity.class);
  188.             i.putExtra("Łącznie pytań:", String.valueOf(questiontag));
  189.             i.putExtra("Poprawne odpowiedzi:", String.valueOf(correct));
  190.             i.putExtra("Złe odpowiedzi:", String.valueOf(wrong));
  191.             startActivity(i);
  192.         }
  193.         else
  194.         {
  195.             if(total<=30)
  196.             {
  197.  
  198.  
  199.                 databaseReference = FirebaseDatabase.getInstance().getReference().child("questions").child(String.valueOf(total));
  200.                 databaseReference.addValueEventListener(new ValueEventListener() {
  201.                     @Override
  202.                     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  203.                         final QuestionFirebase questionFirebase = dataSnapshot.getValue(QuestionFirebase.class);
  204.  
  205.                         question.setText(questionFirebase.getQuestion());
  206.                         b1.setText(questionFirebase.getOption1());
  207.                         b2.setText(questionFirebase.getOption2());
  208.                         b3.setText(questionFirebase.getOption3());
  209.                         b4.setText(questionFirebase.getOption4());
  210.  
  211.                         //timeLeftInMillis = 60000;
  212.                         //updateCountDownText();
  213.                         startCountDown();
  214.  
  215.                         //final boolean[] tmp1 = new boolean[1];
  216.                         //final boolean[] tmp2 = new boolean[1];
  217.                         //final boolean[] tmp3 = new boolean[1];
  218.                         //final boolean[] tmp4 = new boolean[1];
  219.  
  220.                         //tmp1[0]=true;
  221.                         //tmp2[0]=true;
  222.                         //tmp3[0]=true;
  223.                         //tmp4[0]=true;
  224.  
  225.                         //final String[] s1 = {String.valueOf(tmp1)};
  226.                         //final String[] s2 = {String.valueOf(tmp2)};
  227.                         //final String[] s3 = {String.valueOf(tmp4)};
  228.                         //final String[] s4 = {String.valueOf(tmp4)};
  229.  
  230.                         final boolean[] varControlOne = {true};
  231.                         final boolean[] varControlTwo = {true};
  232.                         final boolean[] varControlThree = {true};
  233.                         final boolean[] varControlFour = {true};
  234.  
  235.  
  236.                         if (b1.getText().toString().equals(questionFirebase.getAnswer1())) {
  237.                             varControlOne[0] = false;
  238.                         }
  239.                         else if (b2.getText().toString().equals(questionFirebase.getAnswer2())) {
  240.                             varControlTwo[0] = false;
  241.                         }
  242.                         else if (b3.getText().toString().equals(questionFirebase.getAnswer3())) {
  243.                             varControlThree[0] = false;
  244.                         }
  245.                         else if (b4.getText().toString().equals(questionFirebase.getAnswer4())) {
  246.                             varControlFour[0] = false;
  247.                         }
  248.  
  249.  
  250.                         b1.setOnClickListener(new View.OnClickListener() {
  251.                             @Override
  252.                             public void onClick(View view) {
  253.                                 b1.setBackgroundColor(getResources().getColor(R.color.mclaren));
  254.                                 if (b1.getText().toString().equals(questionFirebase.getAnswer1())) {
  255.                                     varControlOne[0] = true;
  256.                                 }
  257.                             }
  258.                         });
  259.  
  260.                         b2.setOnClickListener(new View.OnClickListener() {
  261.                             @Override
  262.                             public void onClick(View v) {
  263.                                 b2.setBackgroundColor(getResources().getColor(R.color.mclaren));
  264.                                 if (b2.getText().toString().equals(questionFirebase.getAnswer2())) {
  265.                                     varControlTwo[0] = true;
  266.                                 }
  267.  
  268.                             }
  269.                         });
  270.  
  271.                         b3.setOnClickListener(new View.OnClickListener() {
  272.                             @Override
  273.                             public void onClick(View v) {
  274.                                 b3.setBackgroundColor(getResources().getColor(R.color.mclaren));
  275.                                 if (b3.getText().toString().equals(questionFirebase.getAnswer3())) {
  276.                                     varControlThree[0] = true;
  277.                                 }
  278.                             }
  279.                         });
  280.  
  281.                         b4.setOnClickListener(new View.OnClickListener() {
  282.                             @Override
  283.                             public void onClick(View v) {
  284.                                 b4.setBackgroundColor(getResources().getColor(R.color.mclaren));
  285.                                 if (b4.getText().toString().equals(questionFirebase.getAnswer4())) {
  286.                                     varControlFour[0] = true;
  287.                                 }
  288.                             }
  289.                         });
  290.  
  291.                         reset.setOnClickListener(new View.OnClickListener() {
  292.                             @Override
  293.                             public void onClick(View v) {
  294.                                 reset.setBackgroundColor(getResources().getColor(R.color.mclaren));
  295.                                 b1.setBackgroundColor(Color.WHITE);
  296.                                 b2.setBackgroundColor(Color.WHITE);
  297.                                 b3.setBackgroundColor(Color.WHITE);
  298.                                 b4.setBackgroundColor(Color.WHITE);
  299.                                 varControlOne[0] = true;
  300.                                 varControlTwo[0] = true;
  301.                                 varControlThree[0] = true;
  302.                                 varControlFour[0] = true;
  303.                                 if (b1.getText().toString().equals(questionFirebase.getAnswer1())) {
  304.                                     varControlOne[0] = false;
  305.                                 }
  306.                                 else if (b2.getText().toString().equals(questionFirebase.getAnswer2())) {
  307.                                     varControlTwo[0] = false;
  308.                                 }
  309.                                 else if (b3.getText().toString().equals(questionFirebase.getAnswer3())) {
  310.                                     varControlThree[0] = false;
  311.                                 }
  312.                                 else if (b4.getText().toString().equals(questionFirebase.getAnswer4())) {
  313.                                     varControlFour[0] = false;
  314.                                 }
  315.                                 Handler handler = new Handler();
  316.                                 handler.postDelayed(new Runnable() {
  317.                                     @Override
  318.                                     public void run() {
  319.                                         reset.setBackgroundColor(Color.WHITE);
  320.                                     }
  321.                                 }, 50);
  322.                             }
  323.                         });
  324.  
  325.  
  326.                         accept.setOnClickListener(new View.OnClickListener() {
  327.                             @Override
  328.                             public void onClick(View v) {
  329.                                 countDownTimer.cancel();
  330.                                 //timeLeftInMillis = 60000;
  331.                                 //updateCountDownText();
  332.                                 //startCountDown();  BRON BOZE
  333.                                 accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  334.                                 //startCountDown();
  335.                                 //updateCountDownText();
  336.                                 if (varControlOne[0] == true && varControlTwo[0] == true && varControlThree[0] == true && varControlFour[0] == true)
  337.                                 {
  338.                                     correct++;
  339.                                     questiontag++;
  340.                                     Handler handler = new Handler();
  341.                                     handler.postDelayed(new Runnable() {
  342.                                         @Override
  343.                                         public void run() {
  344.                                             //countDownTimer.cancel();
  345.                                             accept.setBackgroundColor(Color.WHITE);
  346.                                             b1.setBackgroundColor(Color.WHITE);
  347.                                             b2.setBackgroundColor(Color.WHITE);
  348.                                             b3.setBackgroundColor(Color.WHITE);
  349.                                             b4.setBackgroundColor(Color.WHITE);
  350.                                             updateQuestions();
  351.                                         }
  352.                                     }, 0);
  353.                                 }
  354.                                 else if(varControlOne[0] == false || varControlTwo[0] == false || varControlThree[0] == false || varControlFour[0] == false)
  355.                                     {
  356.                                     accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  357.                                     wrong++;
  358.                                     questiontag++;
  359.                                     Handler handler = new Handler();
  360.                                     handler.postDelayed(new Runnable() {
  361.                                         @Override
  362.                                         public void run() {
  363.                                             accept.setBackgroundColor(Color.WHITE);
  364.                                             b1.setBackgroundColor(Color.WHITE);
  365.                                             b2.setBackgroundColor(Color.WHITE);
  366.                                             b3.setBackgroundColor(Color.WHITE);
  367.                                             b4.setBackgroundColor(Color.WHITE);
  368.                                             updateQuestions();
  369.                                         }
  370.                                     }, 0);
  371.                                 }
  372.                                 else
  373.                                 {
  374.                                     accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  375.                                     wrong++;
  376.                                     questiontag++;
  377.                                     Handler handler = new Handler();
  378.                                     handler.postDelayed(new Runnable() {
  379.                                         @Override
  380.                                         public void run() {
  381.                                             accept.setBackgroundColor(Color.WHITE);
  382.                                             b1.setBackgroundColor(Color.WHITE);
  383.                                             b2.setBackgroundColor(Color.WHITE);
  384.                                             b3.setBackgroundColor(Color.WHITE);
  385.                                             b4.setBackgroundColor(Color.WHITE);
  386.                                             updateQuestions();
  387.                                         }
  388.                                     }, 0);
  389.                                 }
  390.                             }
  391.                         });
  392.                     }
  393.  
  394.                     @Override
  395.                     public void onCancelled(@NonNull DatabaseError databaseError) {
  396.  
  397.                     }
  398.                 });
  399.             }
  400.         }
  401.         total++;
  402.     }
  403.  
  404.  
  405.     @Override
  406.     protected void onDestroy() {
  407.         super.onDestroy();
  408.         if (countDownTimer != null) {
  409.             countDownTimer.cancel();
  410.         }
  411.     }
  412.  
  413.  
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement