You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.6 KiB

package com.gyf.csams.association.model
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.gyf.csams.uikit.ScrollList
import com.gyf.csams.uikit.StringForm
import kotlin.random.Random
/**
* 题型
*
*/
enum class ExamType(val type:String){
//选择题
cq("选择题"),
//开放题
oq("开放题")
}
sealed class Exam{
abstract val examType:ExamType
abstract val question:StringForm
}
/**
* 开放题
*
* @property examType 题型描述
* @property question 问题
*/
data class OpenQuestionsVo(
override val examType: ExamType = ExamType.oq, override val question: StringForm
) : Exam()
/**
* 选择题
*
* @property examType 题型描述
* @property answers 答案
* @property rightAnswer 正确答案
* @property question 问题
*/
data class ChoiceQuestionVo(override val examType: ExamType = ExamType.cq,
val answers:List<StringForm>,
val rightAnswer:Int,
override val question: StringForm
):Exam()
/**
* 题库状态管理
*
*/
/**
* 问题长度
*/
const val QUESTION_TEXT_LENGTH=30
/**
* 选择题选项数
*/
const val ANSWER_SIZE=4
/**
* 答案长度
*
*/
const val ANSWER_TEXT_LENGTH=15
class ExamViewModel:ScrollList<Exam>() {
val questionIsNull: String = "问题不能为空"
val deleteLeastOne: String="至少保留一道题目"
val updateExam="更新题库"
val back="返回"
val menuName="入团题库"
val deleteTip="确定删除此题目?"
val addTip="确定添加此题目?"
val actionLabel="确定"
override val initSize = 10
private val _newExam:MutableLiveData<Exam> = MutableLiveData(createExam(ExamType.cq))
val newExam:LiveData<Exam> = _newExam
init {
load()
}
/**
* 切换题型
*
*/
fun switchType(exam: Exam){
if(exam is ChoiceQuestionVo) _newExam.value=createExam(ExamType.oq) else _newExam.value=createExam(ExamType.cq)
}
/**
* 创建题目
*
* @param type
* @return
*/
private fun createExam(type: ExamType): Exam {
val question=StringForm(formDesc = "问题",textLength = QUESTION_TEXT_LENGTH)
return when(type){
ExamType.cq-> ChoiceQuestionVo(
answers = ('A'..'D').map { StringForm(formDesc = "选项",textLength = ANSWER_TEXT_LENGTH,value = "选项$it") },
rightAnswer = 0,
question = question
)
ExamType.oq-> OpenQuestionsVo(question = question)
}
}
/**
* 更新题目
*
*/
fun update(oldExam: Exam,newExam:Exam){
if(oldExam==_newExam.value){
_newExam.value=newExam
}else{
_data.value?.apply {
this[indexOf(oldExam)]=newExam
val list = mutableListOf<Exam>()
list.addAll(this)
_data.value?.clear()
_data.value=list
}
}
}
/**
* TODO 更新题库
*
* @param callback
*/
fun updateExam(callback: (message: String) -> Unit){
callback("功能尚未实现,敬请期待")
}
/**
* 加载题目
*
*/
override fun load() {
_data.value?.apply {
repeat(initSize) {
if (Random.nextBoolean()){
add(OpenQuestionsVo(question = StringForm(formDesc = "问题",textLength = QUESTION_TEXT_LENGTH,value = "这是一道开放题:$size")))
} else{
add(
ChoiceQuestionVo(
question = StringForm(formDesc = "问题",textLength = QUESTION_TEXT_LENGTH,value = "这是一道选择题:$size"),
answers = ('A'..'D').map { StringForm(formDesc = "选项",textLength = ANSWER_TEXT_LENGTH,value = "选项$it") },
rightAnswer = Random.nextInt(ANSWER_SIZE)
)
)
}
}
}
}
/**
*TODO 加载更多题目
*
* @param callback
*/
override fun loadMore(callback: (message: String) -> Unit) {
// _data.value?.apply {
// val list= mutableListOf<Exam>()
// list.addAll(this)
// list.apply {
// repeat(10) {
// if (Random.nextBoolean()) add(OpenQuestionsVo(question = "这是一道开放题:$size")) else add(
// ChoiceQuestionVo(
// question = "这是一道选择题:$size,请从选项中选出正确答案",
// answers = listOf("选项A", "选项B", "选项C", "选项D"),
// rightAnswer = 3
// )
// )
// }
// }
// _data.postValue(list)
// callback("成功加载更多题目")
// }
// callback("功能尚未实现,敬请期待")
}
fun addQuestion() {
_data.value?.apply {
_newExam.value?.let{
val list= mutableListOf<Exam>()
list.addAll(this)
list.add(it)
_data.postValue(list)
}
_newExam.value=createExam(ExamType.cq)
}
}
/**
* TODO 删除题目
*
*/
fun deleteQuestion(exam: Exam) {
_data.value?.apply {
val list = mutableListOf<Exam>()
remove(exam)
list.addAll(this)
_data.postValue(list)
}
}
}