parent
303f98ff04
commit
94b057d1cf
@ -1,42 +1,135 @@ |
||||
package com.gyf.csams.association.model |
||||
|
||||
import android.app.Application |
||||
import android.net.Uri |
||||
import androidx.lifecycle.AndroidViewModel |
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.MutableLiveData |
||||
import androidx.lifecycle.ViewModel |
||||
import com.gyf.csams.NOT_IMPL_TIP |
||||
import com.gyf.lib.uikit.StringForm |
||||
import androidx.lifecycle.viewModelScope |
||||
import com.google.gson.reflect.TypeToken |
||||
import com.gyf.csams.Api |
||||
import com.gyf.csams.AssociationApi |
||||
import com.gyf.csams.MainApplication |
||||
import com.gyf.csams.UNKNOW_ERROR |
||||
import com.gyf.csams.util.SimpleCallback |
||||
import com.gyf.csams.util.Token |
||||
import com.gyf.csams.util.TokenManager |
||||
import com.gyf.lib.uikit.FormStatus |
||||
import com.gyf.lib.uikit.ValidStringForm |
||||
import com.gyf.lib.util.ApiResponse |
||||
import com.gyf.lib.util.HttpClient |
||||
import com.orhanobut.logger.Logger |
||||
import kotlinx.coroutines.launch |
||||
import java.io.File |
||||
import java.io.InputStream |
||||
|
||||
data class RegAssociationVo(val name: String, val desc: String, val fileId: Int, val token: Token) |
||||
|
||||
data class Image(val uri: Uri, val createTime: Long, val size: Long) |
||||
|
||||
class RegAssociationViewModel : ViewModel() { |
||||
class RegAssociationViewModel(application: Application) : AndroidViewModel(application) { |
||||
|
||||
val frameDesc = "社团注册资料" |
||||
|
||||
val name = StringForm(formDesc = "社团名称", textLength = 5) |
||||
val desc = StringForm(formDesc = "社团简介", textLength = 30) |
||||
val name = ValidStringForm(formDesc = "社团名称", textLength = 5) |
||||
val desc = ValidStringForm(formDesc = "社团简介", textLength = 30) |
||||
|
||||
|
||||
val _picture = MutableLiveData<Uri>() |
||||
val picture: LiveData<Uri> = _picture |
||||
private val _picture = MutableLiveData<Uri?>() |
||||
val picture: LiveData<Uri?> = _picture |
||||
|
||||
val piciurePlaceHolder = "请上传图片" |
||||
private val _fileId = MutableLiveData<Int>() |
||||
val fileId: LiveData<Int> = _fileId |
||||
|
||||
val errorPicture = "图片加载失败,请联系管理员" |
||||
val picturePlaceHolder = "请上传图片" |
||||
|
||||
val errorPicture = "图片加载失败,请联系管理员" |
||||
|
||||
fun setPicture(uri: Uri) { |
||||
_picture.value = uri |
||||
} |
||||
|
||||
fun getInputSteam(): InputStream? { |
||||
_picture.value?.let { |
||||
val resolver = getApplication<MainApplication>().contentResolver |
||||
return resolver.openInputStream(it) |
||||
} |
||||
throw IllegalArgumentException(UNKNOW_ERROR) |
||||
} |
||||
|
||||
|
||||
fun uploadPhoto(callback: (value: String) -> Unit) { |
||||
getInputSteam()?.readBytes()?.apply { |
||||
val token = TokenManager.token |
||||
if (token != null) { |
||||
|
||||
viewModelScope.launch { |
||||
val context = getApplication<MainApplication>() |
||||
|
||||
runCatching { |
||||
val cacheFile = File(context.cacheDir, "${System.currentTimeMillis()}") |
||||
cacheFile.writeBytes(this@apply) |
||||
|
||||
HttpClient.uploadFile( |
||||
Api.buildUrl(AssociationApi.Logo), |
||||
SimpleCallback<List<Int>>("上传图片", onSuccess = { |
||||
Logger.i(it.message) |
||||
callback(it.message) |
||||
it.body?.let { |
||||
_fileId.postValue(it.first()) |
||||
} |
||||
|
||||
}, onFail = { |
||||
Logger.e(it) |
||||
callback("图片上传失败") |
||||
}, type = object : TypeToken<ApiResponse<List<Int>>>() {}.type), |
||||
id = token.userId, |
||||
token = token.token, |
||||
fileList = arrayOf(cacheFile) |
||||
) |
||||
} |
||||
} |
||||
} else { |
||||
callback(UNKNOW_ERROR) |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* TODO 注册社团 |
||||
* |
||||
* |
||||
* @param callback |
||||
*/ |
||||
fun register(callback: (value: String) -> Unit) { |
||||
callback(NOT_IMPL_TIP) |
||||
val nameValue = name.formValue.value |
||||
val descValue = desc.formValue.value |
||||
val token = TokenManager.token |
||||
val fileId = _fileId.value |
||||
if (token != null && nameValue != null && descValue != null && fileId != null && |
||||
name.statusForm.value == FormStatus.Valid && desc.statusForm.value == FormStatus.Valid |
||||
) { |
||||
viewModelScope.launch { |
||||
HttpClient.post( |
||||
Api.buildUrl(AssociationApi.Register), |
||||
SimpleCallback<Boolean>("注册社团", onSuccess = { |
||||
Logger.i(it.message) |
||||
callback(it.message) |
||||
name.clean() |
||||
desc.clean() |
||||
_picture.postValue(null) |
||||
}, onFail = { |
||||
Logger.e(it) |
||||
}, object : TypeToken<ApiResponse<Boolean>>() {}.type), |
||||
jsonParam = |
||||
RegAssociationVo( |
||||
name = nameValue, |
||||
desc = descValue, |
||||
token = token, |
||||
fileId = fileId |
||||
) |
||||
) |
||||
} |
||||
} else { |
||||
callback(UNKNOW_ERROR) |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue