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.
csamsclient/background/src/main/java/com/gyf/csams/main/model/ManagementOfficerModel.kt

123 lines
3.2 KiB

package com.gyf.csams.main.model
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gyf.lib.util.randomChinese
import com.gyf.lib.util.randomNum
import com.orhanobut.logger.Logger
import kotlinx.coroutines.launch
enum class ColumnType {
Text,
DropMenu
}
/**
* 职务
*
* @property desc
*/
enum class Duty(val desc: String) {
Minister("部长"),
Manager("干事")
}
/**
* 人员信息
*
* @property name 名字
* @property studentId 学号
* @property mobile 手机号
* @property duty 职务
* @property counselor 导员
*/
data class OfficerVo(
val name: String,
val studentId: String,
val mobile: String,
val duty: Duty,
val counselor: String
)
data class AllOfficerVo(
val secretariat: MutableList<OfficerVo>,
val propaganda: MutableList<OfficerVo>,
val publicRelationsDepartment: MutableList<OfficerVo>
)
/**
* 部门干事数据状态管理
*
*/
class ManagementOfficerModel : ViewModel() {
private val _data = MutableLiveData<AllOfficerVo>()
val data: LiveData<AllOfficerVo> = _data
init {
load()
}
private fun replace(
list: MutableList<OfficerVo>,
index: Int,
callback: (s: MutableList<OfficerVo>) -> Unit
) {
val s = mutableListOf<OfficerVo>()
list[index] = list[index].copy(duty = Duty.Minister)
s.add(list[index])
s.addAll(list.filter { officerVo -> officerVo != list[index] }
.map { officerVo -> officerVo.copy(duty = Duty.Manager) })
callback(s)
}
fun updateDuty(list: MutableList<OfficerVo>, index: Int) {
_data.value?.apply {
Logger.i("$secretariat")
when (list) {
secretariat -> replace(list = list, index = index) {
_data.postValue(copy(secretariat = it))
}
propaganda -> replace(list = list, index = index) {
_data.postValue(copy(propaganda = it))
}
publicRelationsDepartment -> replace(list = list, index = index) {
_data.postValue(copy(publicRelationsDepartment = it))
}
}
}
}
/**
* TODO 加载部门成员
*
*/
private fun load() {
viewModelScope.launch {
val officerVoList = mutableListOf<OfficerVo>()
val baseSize = 3
val peopleSize = 5
repeat(peopleSize * baseSize) {
officerVoList.add(
OfficerVo(
name = randomChinese(3), studentId = randomNum(8), mobile = randomNum(11),
if (it % peopleSize == 0) Duty.Minister else Duty.Manager, counselor = ""
)
)
}
val all = officerVoList.chunked(peopleSize)
_data.postValue(
AllOfficerVo(
secretariat = all[0].toMutableList(),
propaganda = all[1].toMutableList(),
publicRelationsDepartment = all[2].toMutableList()
)
)
}
}
}