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.

204 lines
4.9 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.csams.NOT_IMPL_TIP
import com.gyf.csams.uikit.ScrollList
import com.gyf.csams.uikit.SendInterface
import com.gyf.csams.uikit.StringForm
import com.gyf.csams.util.randomChinese
import com.gyf.csams.util.randomNum
import com.orhanobut.logger.Logger
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
* 跑马灯
*
*/
class MarqueeViewModel : ViewModel() {
val marqueeTexts = listOf("床前明月光", "疑是地上霜", "举头望明月", "低头思故乡")
private val _marqueeIndex = MutableLiveData(0)
var marqueeIndex: LiveData<Int> = _marqueeIndex
private var marqueeJob: Job? = null
fun addAsync(delayMillis: Int) {
if (marqueeJob == null || marqueeJob?.isCompleted == true) {
marqueeJob = viewModelScope.launch {
_marqueeIndex.postValue(
if (_marqueeIndex.value == marqueeTexts.size - 1) 0 else _marqueeIndex.value?.plus(
1
)
)
delay(timeMillis = delayMillis.toLong())
}
}
}
}
/**
* 社团
*
* @property name 社团名称
*/
data class AssociationDto(val name: String)
/**
* 主页
*
*/
class MainViewModel : ViewModel(), SendInterface {
override val _openDialog: MutableLiveData<Boolean> = MutableLiveData()
override val openDialog: LiveData<Boolean> = _openDialog
override val newContent: StringForm = StringForm(formDesc = "留言", textLength = 30)
override fun openDialog() {
_openDialog.value = true
}
override fun closeDialog() {
_openDialog.value = false
}
/**
* TODO 发送留言
*
* @param callback
*/
override fun send(callback: (message: String) -> Unit) {
callback(NOT_IMPL_TIP)
}
}
/**
* 社团列表
*
*/
class ListViewModel : ScrollList<AssociationDto>() {
val name = StringForm(formDesc = "社团名称", textLength = 5)
val desc = StringForm(formDesc = "社团简介", textLength = 10)
//社团列表加载数量
val associationListSize = 10
//社团列表
private val _associationList = MutableLiveData<MutableList<AssociationDto>>(mutableListOf())
val associationDto: LiveData<MutableList<AssociationDto>> = _associationList
val searchDesc = "搜索"
/**
* TODO 社团检索
*
* @param callback
*/
fun search(callback: (value: String) -> Unit) {
Logger.i("搜索条件[社团名称:${name.formValue.value},社团简介:${desc.formValue.value}]")
callback(NOT_IMPL_TIP)
}
override val initSize: Int = 10
init {
load()
}
/**
* 加载社团列表
*
*/
override fun load() {
viewModelScope.launch {
_associationList.value?.apply {
repeat(
10
) {
add(AssociationDto(name = "社团${_associationList.value?.size}"))
}
}
Logger.i("初始化社团列表size=${_associationList.value?.size}")
}
}
/**
* 加载更多社团列表
*
*/
override fun loadMore(callback: (message: String) -> Unit) {
viewModelScope.launch {
_associationList.value?.apply {
val list = mutableListOf<AssociationDto>()
list.addAll(this)
list.apply {
repeat(10) {
add(AssociationDto(name = "社团${size}"))
}
}
Logger.i("t.size=${size}")
_associationList.postValue(list)
Logger.i("加载更多社团size=${_associationList.value?.size}")
callback("成功加载更多社团")
}
}
}
}
/**
* 我的信息
*
* @property studentId 学号
* @property name 姓名
* @property duty 职务
* @property headImg 头像
* @property desc 个人简介
*/
data class InfoVo(
val studentId: String,
val name: String,
val duty: String,
val headImg: String,
val desc: String
)
/**
* 个人中心
*
*/
class CenterViewModel : ViewModel() {
val myAssociationDesc = "我的社团"
val myJoinActivity = "我参加的社团活动"
val myLikeActivity = "我点赞的社团活动"
val myCollectActivity = "我收藏的社团活动"
private val _info = MutableLiveData<InfoVo>()
val info: LiveData<InfoVo> = _info
init {
load()
}
private fun load() {
viewModelScope.launch {
_info.value = InfoVo(
studentId = randomNum(),
name = randomChinese(3),
duty = randomChinese(3),
headImg = "",
desc = randomChinese(10)
)
}
}
}