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.
357 lines
11 KiB
357 lines
11 KiB
package com.gyf.csams.main.model
|
|
|
|
import android.app.Activity
|
|
import android.content.Intent
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
import androidx.lifecycle.ViewModel
|
|
import androidx.lifecycle.viewModelScope
|
|
import com.google.gson.reflect.TypeToken
|
|
import com.gyf.csams.*
|
|
import com.gyf.csams.account.ui.AccountActivity
|
|
import com.gyf.csams.uikit.AbstractComment
|
|
import com.gyf.csams.uikit.OnlyToken
|
|
import com.gyf.csams.util.AppDatabase
|
|
import com.gyf.csams.util.SimpleCallback
|
|
import com.gyf.csams.util.Token
|
|
import com.gyf.csams.util.TokenManager
|
|
import com.gyf.lib.ScrollList
|
|
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.ApiResponse
|
|
import com.gyf.lib.util.HttpClient
|
|
import com.gyf.lib.util.randomChinese
|
|
import com.gyf.lib.util.randomNum
|
|
import com.orhanobut.logger.Logger
|
|
import kotlinx.coroutines.Job
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.launch
|
|
|
|
data class LeaveMessageFormatVo(val message: String, val studentId: String)
|
|
|
|
/**
|
|
* 跑马灯
|
|
*
|
|
*/
|
|
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()
|
|
_marqueeTexts.value?.apply {
|
|
val c = mutableListOf<LeaveMessageFormatVo>()
|
|
c.add(
|
|
LeaveMessageFormatVo(
|
|
message = "${TokenManager.token?.name}说:${newContent.formValue.value}",
|
|
studentId = "${TokenManager.token?.studentId}"
|
|
)
|
|
)
|
|
c.addAll(this)
|
|
_marqueeTexts.postValue(c)
|
|
}
|
|
} 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()
|
|
)
|
|
}
|
|
}
|
|
|
|
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 MainViewModel : AbstractComment() {
|
|
|
|
|
|
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 = {
|
|
callback("留言发送${if (it.body == true) "成功" else "失败"}")
|
|
}, onFail = {
|
|
callback("留言发送失败")
|
|
}, type = object : TypeToken<ApiResponse<Boolean>>() {}.type
|
|
),
|
|
jsonParam = LeaveMessageVo(
|
|
message = newContent.formValue.value ?: "",
|
|
token = it
|
|
)
|
|
)
|
|
}
|
|
} else {
|
|
callback(UNKNOW_ERROR)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 社团列表
|
|
*
|
|
*/
|
|
class ListViewModel : ScrollList<AssociationListVo>() {
|
|
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()
|
|
|
|
data class UserLogoutVo(val studentId: 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)
|
|
)
|
|
}
|
|
|
|
}
|
|
|
|
fun logout(context: Activity, callback: (message: String) -> Unit) {
|
|
val studentId = TokenManager.token?.studentId
|
|
if (studentId != null) {
|
|
Logger.i("帐号$studentId 将要退出登录")
|
|
viewModelScope.launch {
|
|
HttpClient.post(Api.buildUrl(AccountApi.Logout),
|
|
SimpleCallback<Boolean>(action = "登出", onSuccess = { it ->
|
|
it.body?.let {
|
|
if (it) {
|
|
viewModelScope.launch {
|
|
val db = AppDatabase.getInstance(context = context)
|
|
db?.tokenDao()?.deleteAll()
|
|
Logger.i("退出登陆成功")
|
|
context.finish()
|
|
context.startActivity(
|
|
Intent(
|
|
context,
|
|
AccountActivity::class.java
|
|
)
|
|
)
|
|
|
|
}
|
|
TokenManager.token = null
|
|
}
|
|
}
|
|
callback(it.message)
|
|
}, onFail = { callback("退出登陆失败") },
|
|
type = object : TypeToken<ApiResponse<Boolean>>() {}.type
|
|
),
|
|
jsonParam = UserLogoutVo(studentId = studentId)
|
|
)
|
|
}
|
|
} else {
|
|
callback("操作异常,请联系管理员")
|
|
}
|
|
}
|
|
}
|
|
|