package com.gyf.csams.association.model import android.app.Application 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.R import com.gyf.csams.module.* import com.gyf.csams.uikit.AssociationMenu import com.gyf.csams.uikit.TopMenuInterface import com.gyf.lib.model.ScrollViewModel import com.gyf.lib.uikit.StringForm import com.gyf.lib.util.* import kotlinx.coroutines.launch class AssociationViewModel : ViewModel(), TopMenuInterface { override val _currentMenu: MutableLiveData = MutableLiveData() override val currentMenu: LiveData = _currentMenu /** * 下拉菜单状态 */ private val _expanded = MutableLiveData(false) val expanded: LiveData = _expanded private val _associationVo = MutableLiveData() val associationVo: LiveData = _associationVo private val _applyAssociationResultVo = MutableLiveData() val applyAssociationResultVo: LiveData = _applyAssociationResultVo private val _dropMenuMessage = MutableLiveData() val dropMenuMessage: LiveData = _dropMenuMessage fun update(message: String? = null) { _dropMenuMessage.postValue(message) } fun update(applyAssociationResultVo: ApplyAssociationResultVo? = null) { _applyAssociationResultVo.postValue(applyAssociationResultVo) } fun load(id: Int) { viewModelScope.launch { HttpClient.post( Api.buildUrl(AssociationApi.Load), HttpCallback(action = "获取社团信息", onSuccess = { it.body?.let { _associationVo.postValue(it) } }, typeToken = object : TypeToken>() {}.type), jsonParam = ShowAssociationVo(id = id, token = TokenManager.getToken()) ) } } /** * 切换下拉菜单状态 * */ fun switchType() { _expanded.value?.let { _expanded.value = !it } } fun close() { _expanded.value = false } /** * 申请入团 * */ fun applyAssociation(associationId: Int, callback: (result: ApplyAssociationResultVo) -> Unit) { viewModelScope.launch { HttpClient.post( url = Api.buildUrl(AssociationApi.CheckApply), callback = HttpCallback( action = "申请入团", onSuccess = { it.body?.let { callback(it) } }, typeToken = object : TypeToken>() {}.type ), jsonParam = SearchExamVo(associationId = associationId, token = TokenManager.getToken()) ) } } } /** * 社团成员 * */ class MemberViewModel(application: Application) : ScrollViewModel(application) { val name = StringForm(formDesc = "姓名关键字", application.resources.getInteger(R.integer.name_length)) override val initSize: Int = 10 /** * 加载社团成员 * */ fun load(id: Int) { viewModelScope.launch { HttpClient.post( Api.buildUrl(AssociationApi.LoadMember), HttpCallback>( action = "加载社团成员", onSuccess = { it.body?.let { _data.postValue(it) } }, typeToken = object : TypeToken>>() {}.type ), jsonParam = QueryAssociationMembers( id = id, name = name.formValue.value, token = TokenManager.getToken() ) ) } } } class OngoingActViewModel : ViewModel() { private val _act = MutableLiveData() val act: LiveData = _act } /** * 历史活动 * */ class HistoryActViewModel(application: Application) : ScrollViewModel(application) { fun load(associationId: Int) { viewModelScope.launch { HttpClient.post( url = Api.buildUrl(ActivityApi.Load), callback = HttpCallback>( action = "查看社团活动", onSuccess = { _data.postValue(it.body) }, typeToken = object : TypeToken>>() {}.type ), jsonParam = SearchActivityVo( associationId = associationId, token = TokenManager.getToken() ) ) } } }