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.

290 lines
8.3 KiB

package com.gyf.csams.main.model
import android.app.Application
import androidx.lifecycle.*
import com.google.gson.reflect.TypeToken
import com.gyf.csams.account.model.UserVo
import com.gyf.csams.uikit.AbstractComment
import com.gyf.csams.util.SimpleCallback
import com.gyf.lib.model.ScrollViewModel
import com.gyf.lib.uikit.FormStatus
import com.gyf.lib.uikit.PersonInfoVo
import com.gyf.lib.uikit.StringForm
import com.gyf.lib.uikit.ValidStringForm
import com.gyf.lib.util.*
import com.orhanobut.logger.Logger
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
data class LeaveMessageFormatVo(val message: String, val user: UserVo)
class NotificationViewModel(application: Application) : AndroidViewModel(application) {
private val _count = MutableLiveData<Long>()
val count: LiveData<Long> = _count
init {
count()
}
/**
* 通知计数
*
*/
private fun count() {
viewModelScope.launch {
TokenManager.token?.let {
HttpClient.post(
url = Api.buildUrl(NotificationApi.Count),
SimpleCallback<Long>(action = "未读通知计数", onSuccess = {
it.body?.let {
_count.postValue(it)
}
Logger.i(it.message)
}, onFail = {
Logger.e(it)
}, type = object : TypeToken<ApiResponse<Long>>() {}.type),
jsonParam = NotificationDto(
receiverId = it.id,
receiverClient = ClientType.Foreground,
token = it
)
)
}
}
}
}
/**
* 跑马灯
*
*/
class MarqueeViewModel : AbstractComment() {
private val _marqueeTexts = MutableLiveData<List<LeaveMessageFormatVo>>()
val marqueeTexts: LiveData<List<LeaveMessageFormatVo>> = _marqueeTexts
private val _marqueeIndex = MutableLiveData(0)
var marqueeIndex: LiveData<Int> = _marqueeIndex
private var marqueeJob: Job? = null
override val newContent: ValidStringForm =
object : ValidStringForm(formDesc = "留言", textLength = 20) {
override fun check() {
_formValue.value?.let {
if (it.contains("\n")) _statusForm.value =
FormStatus.FormatError else _statusForm.value = FormStatus.Valid
}
}
}
/**
*
* @param callback
*/
override fun send(callback: (message: String) -> Unit) {
TokenManager.token.let {
if (it != null) {
viewModelScope.launch {
HttpClient.post(url = Api.buildUrl(MainApi.LeaveMessage),
SimpleCallback<Boolean>(
action = "发送留言", onSuccess = {
if (it.body == true) {
callback("留言发送成功")
newContent.clean()
loadMessage(callback)
} else {
callback("留言发送失败")
}
}, onFail = {
callback("留言发送失败")
}, type = object : TypeToken<ApiResponse<Boolean>>() {}.type
),
jsonParam = LeaveMessageVo(
message = newContent.formValue.value ?: "",
token = it
)
)
}
} else {
callback(UNKNOW_ERROR)
}
}
}
fun loadMessage(callback: (message: String) -> Unit) {
viewModelScope.launch {
HttpClient.post(
Api.buildUrl(MainApi.GetMessage),
SimpleCallback<List<LeaveMessageFormatVo>>(action = "获取留言", onSuccess = {
// callback(it.message)
Logger.i(it.message)
it.body?.let {
_marqueeTexts.postValue(it)
}
}, onFail = {
Logger.e(it)
callback("留言获取失败")
}, type = object : TypeToken<ApiResponse<List<LeaveMessageFormatVo>>>() {}.type),
jsonParam = OnlyToken(clientType = ClientType.Foreground)
)
}
}
fun addAsync(delayMillis: Int) {
if (marqueeJob == null || marqueeJob?.isCompleted == true) {
marqueeJob = viewModelScope.launch {
_marqueeTexts.value?.apply {
_marqueeIndex.postValue(
if (_marqueeIndex.value == size - 1) 0 else _marqueeIndex.value?.plus(
1
)
)
delay(timeMillis = delayMillis.toLong())
}
}
}
}
}
/**
* 社团
*
* @property name 社团名称
*/
data class AssociationListVo(val name: String)
data class LeaveMessageVo(val message: String, val token: Token)
/**
* 社团列表
*
*/
class ListViewModel(application: Application) : ScrollViewModel<AssociationListVo>(application) {
val name = StringForm(formDesc = "社团名称", textLength = 5)
val desc = StringForm(formDesc = "社团简介", textLength = 10)
//社团列表加载数量
val associationListSize = 10
//社团列表
private val _associationList = MutableLiveData<MutableList<AssociationListVo>>(mutableListOf())
val associationListVo: LiveData<MutableList<AssociationListVo>> = _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(AssociationListVo(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<AssociationListVo>()
list.addAll(this)
list.apply {
repeat(10) {
add(AssociationListVo(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,
override val name: String,
override val duty: String,
override val headImg: String,
override val desc: String
) : PersonInfoVo()
/**
* 个人中心
*
*/
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)
)
}
}
}