package me.bello.brandking;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import me.bello.brandking.data.QuestionData;
import me.bello.brandking.utils.IntentTools;
import me.bello.brandking.utils.PreferenceTools;
import me.bello.brandking.view.MyWebView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
private Context mContext;
private RelativeLayout progressBar;
private TextView _typeText;
private TextView _numText;
private TextView _baiduText, _rightText, _wrongText, _preText, _nextText;
private TextView _questionText;
private RadioGroup _optionRadioGroup;
private RadioButton _option1Text;
private RadioButton _option2Text;
private RadioButton _option3Text;
private RadioButton _option4Text;
public static ArrayList<QuestionData> questionListData = new ArrayList<>();
private int key;
private int finish = 0;
private int rightNum = 0;
private int wrongNum = 0;
private String rightIDs = "", wrongIDs = "";
private MyHandler mHandler = new MyHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = MainActivity.this;
setContentView(R.layout.activity_main);
initUI();
new Thread(new Runnable() {
@Override
public void run() {
getDatas();
}
}).start();
}
@Override
protected void onResume() {
super.onResume();
finish = PreferenceTools.getInt(mContext, PreferenceTools.FINISH, 0);
rightNum = PreferenceTools.getInt(mContext, PreferenceTools.RIGHT_NUM, 0);
wrongNum = PreferenceTools.getInt(mContext, PreferenceTools.WRONG_NUM, 0);
rightIDs = PreferenceTools.getString(mContext, PreferenceTools.RIGHT_ID, "");
wrongIDs = PreferenceTools.getString(mContext, PreferenceTools.WRONG_ID, "");
}
private void initUI() {
progressBar = findViewById(R.id.progress_bar);
_typeText = (TextView) findViewById(R.id.type_text);
_typeText.setOnClickListener(this);
_numText = (TextView) findViewById(R.id.num_text);
_baiduText = findViewById(R.id.baidu_text);
_baiduText.setOnClickListener(this);
_rightText = findViewById(R.id.right_text);
_wrongText = findViewById(R.id.wrong_text);
_wrongText.setOnClickListener(this);
_preText = findViewById(R.id.pre_text);
_preText.setOnClickListener(this);
_nextText = findViewById(R.id.next_text);
_nextText.setOnClickListener(this);
_questionText = (TextView) findViewById(R.id.question_text);
_optionRadioGroup = (RadioGroup) findViewById(R.id.option_radio_group);
_optionRadioGroup.setOnCheckedChangeListener(this);
_option1Text = (RadioButton) findViewById(R.id.option_1_btn);
_option2Text = (RadioButton) findViewById(R.id.option_2_btn);
_option3Text = (RadioButton) findViewById(R.id.option_3_btn);
_option4Text = (RadioButton) findViewById(R.id.option_4_btn);
}
private void getDatas() {
try {
InputStream raw = getResources().openRawResource(R.raw.quizzes);
String qStr = inputStream2String(raw);
JsonArray jsonArray = new JsonParser().parse(qStr).getAsJsonArray();
for (JsonElement obj : jsonArray) {
QuestionData cse = new Gson().fromJson(obj, QuestionData.class);
questionListData.add(cse);
}
mHandler.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 重新加载新题
*/
private void resetUI() {
setRadioGroupStatus(_optionRadioGroup, true);
_option1Text.setBackgroundResource(R.drawable.press_option_bg);
_option2Text.setBackgroundResource(R.drawable.press_option_bg);
_option3Text.setBackgroundResource(R.drawable.press_option_bg);
_option4Text.setBackgroundResource(R.drawable.press_option_bg);
QuestionData data = questionListData.get(finish);
_typeText.setText(data.getSchool() + " / " + data.getType());
_numText.setText((finish + 1) + " / " + questionListData.size());
_rightText.setText(rightNum + "");
_wrongText.setText(wrongNum + "");
_questionText.setText(data.getQuiz());
_option1Text.setText(data.getOptions()[0]);
_option2Text.setText(data.getOptions()[1]);
_option3Text.setText(data.getOptions()[2]);
_option4Text.setText(data.getOptions()[3]);
key = data.getAnswer();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
//科目筛选
case R.id.type_text:
break;
//百度搜索
case R.id.baidu_text:
Bundle bundle = new Bundle();
bundle.putString("search", "https://www.baidu.com/s?wd=" + questionListData.get(finish).getQuiz());
IntentTools.setIntent(mContext, MyWebView.class, bundle, false);
break;
//错题集
case R.id.wrong_text:
IntentTools.setIntent(mContext, ErrorActivity.class, null, false);
break;
//上一题
case R.id.pre_text:
if (finish == 0)
return;
finish--;
PreferenceTools.setInt(mContext, PreferenceTools.FINISH, finish);
resetUI();
break;
//下一题
case R.id.next_text:
if (finish == questionListData.size() - 1)
return;
finish++;
PreferenceTools.setInt(mContext, PreferenceTools.FINISH, finish);
resetUI();
break;
}
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.option_1_btn:
sendAnswer(1);
break;
case R.id.option_2_btn:
sendAnswer(2);
break;
case R.id.option_3_btn:
sendAnswer(3);
break;
case R.id.option_4_btn:
sendAnswer(4);
break;
}
}
/**
* 答题并保存记录
*
* @param i
*/
private void sendAnswer(int i) {
String questionId = questionListData.get(finish).get_id();
setRadioGroupStatus(_optionRadioGroup, false);
if (i == key) {
showOptionBtnSelect(i, true);
//如果不存在就开始保存
if (!rightIDs.contains(questionId)) {
rightIDs += questionId + ",";
PreferenceTools.setString(mContext, PreferenceTools.RIGHT_ID, rightIDs);
rightNum++;
PreferenceTools.setInt(mContext, PreferenceTools.RIGHT_NUM, rightNum);
//保存后删除错误里的数据
if (wrongIDs.contains(questionId)) {
wrongIDs = wrongIDs.replace(questionId + ",", "");
PreferenceTools.setString(mContext, PreferenceTools.WRONG_ID, wrongIDs);
wrongNum--;
PreferenceTools.setInt(mContext, PreferenceTools.WRONG_NUM, wrongNum);
}
}
} else {
showOptionBtnSelect(i, false);
showOptionBtnSelect(key, true);
//如果不存在就开始保存
if (!wrongIDs.contains(questionId)) {
wrongIDs += questionId + ",";
PreferenceTools.setString(mContext, PreferenceTools.WRONG_ID, wrongIDs);
wrongNum++;
PreferenceTools.setInt(mContext, PreferenceTools.WRONG_NUM, wrongNum);
//保存后删除正确里的数据
if (rightIDs.contains(questionId)) {
rightIDs = rightIDs.replace(questionId + ",", "");
PreferenceTools.setString(mContext, PreferenceTools.RIGHT_ID, rightIDs);
rightNum--;
PreferenceTools.setInt(mContext, PreferenceTools.RIGHT_NUM, rightNum);
}
}
}
if (finish != questionListData.size() - 1) {
finish++;
PreferenceTools.setInt(mContext, PreferenceTools.FINISH, finish);
mHandler.sendEmptyMessageDelayed(0, 1000);
} else {
Toast.makeText(mContext, "最后一题", Toast.LENGTH_SHORT).show();
}
}
private void showOptionBtnSelect(int i, boolean isRight) {
switch (i) {
case 1:
if (isRight) {
_option1Text.setBackgroundResource(R.drawable.shape_option_green);
} else {
_option1Text.setBackgroundResource(R.drawable.shape_option_red);
}
break;
case 2:
if (isRight) {
_option2Text.setBackgroundResource(R.drawable.shape_option_green);
} else {
_option2Text.setBackgroundResource(R.drawable.shape_option_red);
}
break;
case 3:
if (isRight) {
_option3Text.setBackgroundResource(R.drawable.shape_option_green);
} else {
_option3Text.setBackgroundResource(R.drawable.shape_option_red);
}
break;
case 4:
if (isRight) {
_option4Text.setBackgroundResource(R.drawable.shape_option_green);
} else {
_option4Text.setBackgroundResource(R.drawable.shape_option_red);
}
break;
}
}
public void setRadioGroupStatus(RadioGroup testRadioGroup, boolean isChecked) {
for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
testRadioGroup.getChildAt(i).setEnabled(isChecked);
testRadioGroup.getChildAt(i).setClickable(isChecked);
}
testRadioGroup.clearCheck();
}
public static String inputStream2String(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressBar.setVisibility(View.GONE);
resetUI();
}
}
}