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.
212 lines
6.2 KiB
212 lines
6.2 KiB
package com.gyf.csams.main.model
|
|
|
|
import android.app.Application
|
|
import androidx.lifecycle.*
|
|
import com.google.gson.reflect.TypeToken
|
|
import com.gyf.csams.uikit.AbstractComment
|
|
import com.gyf.lib.model.ScrollViewModel
|
|
import com.gyf.lib.uikit.FormStatus
|
|
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
|
|
|
|
class NotificationViewModel(application: Application) : AndroidViewModel(application) {
|
|
|
|
private val _count = MutableLiveData<Long>()
|
|
val count: LiveData<Long> = _count
|
|
|
|
init {
|
|
count()
|
|
}
|
|
|
|
/**
|
|
* 通知计数
|
|
*
|
|
*/
|
|
private fun count() {
|
|
viewModelScope.launch {
|
|
HttpClient.post(
|
|
url = Api.buildUrl(NotificationApi.Count),
|
|
HttpCallback<Long>(action = "未读通知计数", onSuccess = {
|
|
it.body?.let {
|
|
_count.postValue(it)
|
|
}
|
|
Logger.i(it.message)
|
|
}, onFail = {
|
|
Logger.e(it)
|
|
}, typeToken = object : TypeToken<ApiResponse<Long>>() {}.type),
|
|
jsonParam = NotificationDto(
|
|
receiverId = TokenManager.getToken().id,
|
|
receiverClient = ClientType.Foreground,
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 跑马灯
|
|
*
|
|
*/
|
|
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) {
|
|
viewModelScope.launch {
|
|
HttpClient.post(url = Api.buildUrl(MainApi.LeaveMessage),
|
|
HttpCallback<Boolean>(
|
|
action = "发送留言", onSuccess = {
|
|
if (it.body == true) {
|
|
callback("留言发送成功")
|
|
newContent.clean()
|
|
loadMessage(callback)
|
|
} else {
|
|
callback("留言发送失败")
|
|
}
|
|
}, onFail = {
|
|
callback("留言发送失败")
|
|
}, typeToken = object : TypeToken<ApiResponse<Boolean>>() {}.type
|
|
),
|
|
jsonParam = LeaveMessageVo(
|
|
message = newContent.formValue.value ?: ""
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
fun loadMessage(callback: (message: String) -> Unit) {
|
|
viewModelScope.launch {
|
|
HttpClient.post(
|
|
Api.buildUrl(MainApi.GetMessage),
|
|
HttpCallback<List<LeaveMessageFormatVo>>(
|
|
action = "获取留言",
|
|
onSuccess = {
|
|
// callback(it.message)
|
|
Logger.i(it.message)
|
|
it.body?.let {
|
|
_marqueeTexts.postValue(it)
|
|
}
|
|
},
|
|
onFail = {
|
|
Logger.e(it)
|
|
callback("留言获取失败")
|
|
},
|
|
typeToken = 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())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 社团列表
|
|
*
|
|
*/
|
|
class AssociationListViewModel(application: Application) :
|
|
ScrollViewModel<AssociationVo>(application) {
|
|
val name = StringForm(formDesc = "社团名称", textLength = 5)
|
|
|
|
val desc = StringForm(formDesc = "社团简介", textLength = 10)
|
|
|
|
override val initSize: Int = 10
|
|
|
|
init {
|
|
load {}
|
|
}
|
|
|
|
/**
|
|
* 加载社团列表
|
|
*
|
|
*/
|
|
fun load(callback: (message: String) -> Unit) {
|
|
viewModelScope.launch {
|
|
HttpClient.post(
|
|
Api.buildUrl(AssociationApi.List),
|
|
HttpCallback<MutableList<AssociationVo>>(
|
|
action = "加载社团列表",
|
|
onSuccess = {
|
|
it.body?.let {
|
|
_data.postValue(it)
|
|
}
|
|
callback(it.message)
|
|
},
|
|
typeToken = object :
|
|
TypeToken<ApiResponse<MutableList<AssociationVo>>>() {}.type
|
|
),
|
|
jsonParam = SearchAssociationVo(
|
|
name = name.formValue.value ?: "",
|
|
desc = desc.formValue.value ?: ""
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 个人中心
|
|
*
|
|
*/
|
|
class CenterViewModel : ViewModel() {
|
|
val myAssociationDesc = "我的社团"
|
|
val myJoinActivity = "我参加的社团活动"
|
|
val myLikeActivity = "我点赞的社团活动"
|
|
val myCollectActivity = "我收藏的社团活动"
|
|
|
|
|
|
init {
|
|
load()
|
|
}
|
|
|
|
private fun load() {
|
|
viewModelScope.launch {
|
|
TODO()
|
|
}
|
|
|
|
}
|
|
}
|
|
|