|
|
package com.br_technology.securitytrain_master.ui.view.home.activity.course
|
|
|
|
|
|
import android.app.Activity
|
|
|
import android.content.Intent
|
|
|
import android.view.View
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
import androidx.core.content.ContextCompat
|
|
|
import com.br_technology.securitytrain_master.R
|
|
|
import com.br_technology.securitytrain_master.base.common.ConstantParamKey
|
|
|
import com.br_technology.securitytrain_master.databinding.ActivityPracticeBinding
|
|
|
import com.br_technology.securitytrain_master.ui.bean.CourseParam
|
|
|
import com.br_technology.securitytrain_master.ui.view.bank.activity.AnswerSheetActivity
|
|
|
import com.br_technology.securitytrain_master.ui.view.bank.adapter.CourseTestPaperAdapter
|
|
|
import com.br_technology.securitytrain_master.ui.view.home.viewmodel.CoursePracticeViewModel
|
|
|
import com.br_technology.securitytrain_master.view.DialogTitle
|
|
|
import com.br_technology.securitytrain_master.view.listener.DialogListener
|
|
|
import com.br_technology.securitytrain_master.view.listener.ToolBarClickListener
|
|
|
import com.luck.picture.lib.tools.ToastUtils
|
|
|
import com.wjx.android.wanandroidmvvm.base.view.BaseLifeCycleActivity
|
|
|
|
|
|
/**
|
|
|
* Author by YSir
|
|
|
* Date on 2022/1/23.
|
|
|
* description
|
|
|
* PS: Not easy to write code, please indicate.
|
|
|
*/
|
|
|
/**
|
|
|
* 课程练习详情
|
|
|
*/
|
|
|
class CoursePractiseActivity :
|
|
|
BaseLifeCycleActivity<CoursePracticeViewModel, ActivityPracticeBinding>(ActivityPracticeBinding::inflate) {
|
|
|
|
|
|
private var courseBean: CourseParam? = null
|
|
|
|
|
|
private val mPagerAdapter = CourseTestPaperAdapter()
|
|
|
private var userItemId = 0
|
|
|
private var isFinish = false
|
|
|
private val titleDialog by lazy {
|
|
|
DialogTitle(this)
|
|
|
}
|
|
|
|
|
|
private val startForResult =
|
|
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
|
|
if (result.resultCode == Activity.RESULT_OK) {
|
|
|
//页面返回值
|
|
|
val data = result.data?.getIntExtra("position", 0)
|
|
|
if (data != null) {
|
|
|
binding.previous.visibility = View.VISIBLE
|
|
|
binding.finishJob.visibility = View.GONE
|
|
|
binding.next.visibility = View.VISIBLE
|
|
|
if (data == 0) {
|
|
|
binding.previous.visibility = View.GONE
|
|
|
binding.finishJob.visibility = View.GONE
|
|
|
binding.next.visibility = View.VISIBLE
|
|
|
}
|
|
|
if (data == mPagerAdapter.itemCount - 1) {
|
|
|
binding.previous.visibility = View.VISIBLE
|
|
|
binding.finishJob.visibility = View.VISIBLE
|
|
|
binding.next.visibility = View.GONE
|
|
|
}
|
|
|
if (data in 0 until mPagerAdapter.itemCount) {
|
|
|
binding.practicePager.currentItem = data
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun initData() {
|
|
|
super.initData()
|
|
|
courseBean = intent.getParcelableExtra(ConstantParamKey.COURSE_BEAN)
|
|
|
val isExam = intent.getBooleanExtra("isExam", false)
|
|
|
courseBean?.let {
|
|
|
if (isExam) {
|
|
|
mViewModel.startExam(it.mTrainClassId.toInt())
|
|
|
} else {
|
|
|
mViewModel.startPractise(it.mTrainClassId.toInt())
|
|
|
}
|
|
|
binding.toolBar.setTitle(
|
|
|
if (it.isTrainClass) {
|
|
|
"班级练习"
|
|
|
} else {
|
|
|
"班级考试"
|
|
|
}
|
|
|
)
|
|
|
}
|
|
|
binding.apply {
|
|
|
// 禁止滑动
|
|
|
practicePager.isUserInputEnabled = false
|
|
|
|
|
|
toolBar.setRightText("答题卡")
|
|
|
toolBar.setRightTextDrawable(
|
|
|
ContextCompat.getDrawable(
|
|
|
baseContext,
|
|
|
R.mipmap.answer_sheet
|
|
|
)
|
|
|
)
|
|
|
// 答题卡点击事件
|
|
|
toolBar.addRightListener(object : ToolBarClickListener {
|
|
|
override fun onClick(view: View) {
|
|
|
val intent =
|
|
|
Intent(this@CoursePractiseActivity, AnswerSheetActivity::class.java)
|
|
|
.putStringArrayListExtra("resultData", mPagerAdapter.getArrayList())
|
|
|
.putExtra("showCorrect", mPagerAdapter.showCorrect)
|
|
|
.putExtra("title", binding.toolBar.getTitle())
|
|
|
startForResult.launch(intent)
|
|
|
}
|
|
|
})
|
|
|
|
|
|
|
|
|
// 上一题
|
|
|
previous.setOnClickListener {
|
|
|
val index = practicePager.currentItem
|
|
|
if (index >= 1) {
|
|
|
practicePager.currentItem = index - 1
|
|
|
}
|
|
|
next.visibility = View.VISIBLE
|
|
|
finishJob.visibility = View.GONE
|
|
|
if (index == 1) {
|
|
|
previous.visibility = View.GONE
|
|
|
}
|
|
|
|
|
|
}
|
|
|
// 下一题
|
|
|
next.setOnClickListener {
|
|
|
if (mPagerAdapter.showCorrect) {
|
|
|
modifyStatus()
|
|
|
} else {
|
|
|
next.isClickable = false
|
|
|
val answer = mPagerAdapter.getAnswer()[practicePager.currentItem]
|
|
|
if (answer.answer.isEmpty()) {
|
|
|
next.isClickable = true
|
|
|
ToastUtils.s(baseContext, "请选择题目选项")
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
mViewModel.subSingle(userItemId, answer.id, answer.answer)
|
|
|
}
|
|
|
}
|
|
|
// 完成
|
|
|
finishJob.setOnClickListener {
|
|
|
// 完成
|
|
|
finishJob.isClickable = false
|
|
|
val answer = mPagerAdapter.getAnswer()[practicePager.currentItem]
|
|
|
if (answer.answer.isEmpty()) {
|
|
|
finishJob.isClickable = true
|
|
|
ToastUtils.s(baseContext, "请选择题目选项")
|
|
|
return@setOnClickListener
|
|
|
}
|
|
|
isFinish = true
|
|
|
mViewModel.subSingle(userItemId, answer.id, answer.answer)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
// 标题弹窗
|
|
|
titleDialog.setListener(object : DialogListener {
|
|
|
override fun determine() {
|
|
|
finish()
|
|
|
}
|
|
|
})
|
|
|
// 查看答题卡
|
|
|
titleDialog.viewAnswerSheetListener {
|
|
|
binding.finishJob.visibility = View.INVISIBLE
|
|
|
mPagerAdapter.showCorrect = true
|
|
|
mPagerAdapter.notifyDataSetChanged()
|
|
|
binding.practicePager.currentItem = 0
|
|
|
binding.previous.visibility = View.GONE
|
|
|
binding.next.visibility = View.VISIBLE
|
|
|
isFinish = false
|
|
|
titleDialog.dismiss()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
override fun initDataObserver() {
|
|
|
mViewModel.exam.observe(this) {
|
|
|
if (it.code == 1) {
|
|
|
userItemId = it.data.user_item_id
|
|
|
mPagerAdapter.addList(it.data.list)
|
|
|
binding.practicePager.adapter = mPagerAdapter
|
|
|
} else {
|
|
|
ToastUtils.s(baseContext, it.msg)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
mViewModel.subSingleData.observe(this) {
|
|
|
binding.next.isClickable = true
|
|
|
modifyStatus()
|
|
|
// 胶卷时调用此接口
|
|
|
if (isFinish) {
|
|
|
mPagerAdapter.notifyDataSetChanged()
|
|
|
mViewModel.subFinish(userItemId)
|
|
|
}
|
|
|
}
|
|
|
mViewModel.subFinish.observe(this) {
|
|
|
if (!titleDialog.isShowing) {
|
|
|
// 点击外部不取消
|
|
|
titleDialog.setInfo("共${mPagerAdapter.itemCount}道题")
|
|
|
titleDialog.setTitleSpan("答对${it.data.correct_count}道题").show()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private fun modifyStatus() {
|
|
|
val index = binding.practicePager.currentItem
|
|
|
if (index < mPagerAdapter.itemCount - 1) {
|
|
|
binding.practicePager.currentItem = index + 1
|
|
|
}
|
|
|
binding.previous.visibility = View.VISIBLE
|
|
|
if (index == mPagerAdapter.itemCount - 2) {
|
|
|
binding.next.visibility = View.GONE
|
|
|
if (!mPagerAdapter.showCorrect) {
|
|
|
binding.finishJob.visibility = View.VISIBLE
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
if (titleDialog.isShowing) {
|
|
|
titleDialog.dismiss()
|
|
|
}
|
|
|
super.onDestroy()
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|