create agent
This commit is contained in:
@@ -15,7 +15,9 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -31,6 +33,7 @@ import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.aiosman.ravenow.LocalAppTheme
|
||||
import com.aiosman.ravenow.LocalNavController
|
||||
@@ -58,12 +61,11 @@ fun AddAgentScreen() {
|
||||
val context = LocalContext.current
|
||||
var agnetNameError by remember { mutableStateOf<String?>(null) }
|
||||
var agnetDescError by remember { mutableStateOf<String?>(null) }
|
||||
fun onNicknameChange(value: String) {
|
||||
var errorMessage by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
fun onNameChange(value: String) {
|
||||
model.name = value
|
||||
agnetNameError = when {
|
||||
/*value.isEmpty() -> "昵称不能为空"
|
||||
value.length < 3 -> "昵称长度不能小于3"
|
||||
value.length > 20 -> "昵称长度不能大于20"*/
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -73,7 +75,7 @@ fun AddAgentScreen() {
|
||||
fun onDescChange(value: String) {
|
||||
model.desc = value
|
||||
agnetDescError = when {
|
||||
value.length > 100 -> "个人简介长度不能大于24"
|
||||
value.length > 100 -> "简介长度不能大于100"
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -115,7 +117,7 @@ fun AddAgentScreen() {
|
||||
) {
|
||||
CustomAsyncImage(
|
||||
context,
|
||||
"",
|
||||
model.croppedBitmap,
|
||||
modifier = Modifier
|
||||
.size(88.dp)
|
||||
.clip(
|
||||
@@ -125,6 +127,10 @@ fun AddAgentScreen() {
|
||||
contentScale = ContentScale.Crop,
|
||||
placeholderRes = R.mipmap.rider_pro_agent_avatar
|
||||
)
|
||||
// 调试信息
|
||||
LaunchedEffect(model.croppedBitmap) {
|
||||
println("AddAgent: croppedBitmap changed: ${model.croppedBitmap != null}")
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(32.dp)
|
||||
@@ -132,6 +138,7 @@ fun AddAgentScreen() {
|
||||
.background(appColors.main)
|
||||
.align(Alignment.BottomEnd)
|
||||
.noRippleClickable {
|
||||
model.isFromAddAgent = true
|
||||
navController.navigate(NavigationRoute.ImageCrop.route)
|
||||
},
|
||||
contentAlignment = Alignment.Center
|
||||
@@ -156,7 +163,7 @@ fun AddAgentScreen() {
|
||||
background = appColors.inputBackground2,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) { value ->
|
||||
onNicknameChange(value)
|
||||
onNameChange(value)
|
||||
}
|
||||
// Spacer(modifier = Modifier.height(16.dp))
|
||||
FormTextInput2(
|
||||
@@ -170,6 +177,18 @@ fun AddAgentScreen() {
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.height(58.dp))
|
||||
|
||||
// 错误信息显示
|
||||
errorMessage?.let { error ->
|
||||
Text(
|
||||
text = error,
|
||||
color = Color.Red,
|
||||
modifier = Modifier.padding(horizontal = 16.dp),
|
||||
fontSize = 14.sp
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
}
|
||||
|
||||
ActionButton(
|
||||
modifier = Modifier
|
||||
.width(345.dp)
|
||||
@@ -187,10 +206,40 @@ fun AddAgentScreen() {
|
||||
color = Color.White,
|
||||
backgroundColor = Color.Transparent,
|
||||
text = stringResource(R.string.agent_create),
|
||||
isLoading = model.isUpdating,
|
||||
loadingText = "创建中...",
|
||||
enabled = !model.isUpdating && model.validate() == null
|
||||
) {
|
||||
|
||||
|
||||
}
|
||||
// 验证输入
|
||||
val validationError = model.validate()
|
||||
if (validationError != null) {
|
||||
// 显示验证错误
|
||||
errorMessage = validationError
|
||||
println("AddAgent: Validation error: $validationError")
|
||||
return@ActionButton
|
||||
}
|
||||
|
||||
// 清除之前的错误信息
|
||||
errorMessage = null
|
||||
|
||||
// 调用创建智能体API
|
||||
model.viewModelScope.launch {
|
||||
try {
|
||||
println("AddAgent: Starting to create agent...")
|
||||
val result = model.createAgent(context)
|
||||
if (result != null) {
|
||||
println("AddAgent: Agent created successfully, closing page")
|
||||
// 创建成功,关闭页面
|
||||
navController.popBackStack()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("AddAgent: Error creating agent: ${e.message}")
|
||||
// 显示错误信息
|
||||
errorMessage = "创建智能体失败: ${e.message}"
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,10 @@ import androidx.lifecycle.ViewModel
|
||||
import com.aiosman.ravenow.data.AccountService
|
||||
import com.aiosman.ravenow.data.AccountServiceImpl
|
||||
import com.aiosman.ravenow.data.UploadImage
|
||||
import com.aiosman.ravenow.data.ServiceException
|
||||
import com.aiosman.ravenow.entity.AccountProfileEntity
|
||||
import com.aiosman.ravenow.entity.AgentEntity
|
||||
import com.aiosman.ravenow.entity.createAgent
|
||||
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
|
||||
import com.aiosman.ravenow.utils.TrtcHelper
|
||||
import java.io.File
|
||||
@@ -18,8 +21,60 @@ import java.io.File
|
||||
object AddAgentViewModel : ViewModel() {
|
||||
var name by mutableStateOf("")
|
||||
var desc by mutableStateOf("")
|
||||
var imageUrl by mutableStateOf<Uri?>(null)
|
||||
var croppedBitmap by mutableStateOf<Bitmap?>(null)
|
||||
var isUpdating by mutableStateOf(false)
|
||||
var isFromAddAgent by mutableStateOf(false)
|
||||
|
||||
suspend fun updateAgentAvatar(context: Context) {
|
||||
println("AddAgentViewModel: updateAgentAvatar called, croppedBitmap: ${croppedBitmap != null}")
|
||||
croppedBitmap?.let {
|
||||
val file = File(context.cacheDir, "agent_avatar.jpg")
|
||||
it.compress(Bitmap.CompressFormat.JPEG, 100, file.outputStream())
|
||||
println("AddAgentViewModel: Avatar saved to ${file.absolutePath}")
|
||||
// 这里可以上传图片到服务器,暂时先保存到本地
|
||||
// UploadImage(file, "agent_avatar.jpg", "", "jpg")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun createAgent(context: Context): AgentEntity? {
|
||||
try {
|
||||
isUpdating = true
|
||||
println("AddAgentViewModel: Creating agent with name: $name, desc: $desc")
|
||||
|
||||
// 准备头像文件
|
||||
val avatarFile = if (croppedBitmap != null) {
|
||||
val file = File(context.cacheDir, "agent_avatar.jpg")
|
||||
croppedBitmap!!.compress(Bitmap.CompressFormat.JPEG, 100, file.outputStream())
|
||||
UploadImage(file, "agent_avatar.jpg", "", "jpg")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
// 调用API创建智能体
|
||||
val result = createAgent(
|
||||
title = name,
|
||||
desc = desc,
|
||||
avatar = avatarFile
|
||||
)
|
||||
|
||||
println("AddAgentViewModel: Agent created successfully with ID: ${result.id}")
|
||||
return result
|
||||
} catch (e: Exception) {
|
||||
println("AddAgentViewModel: Error creating agent: ${e.message}")
|
||||
throw e
|
||||
} finally {
|
||||
isUpdating = false
|
||||
}
|
||||
}
|
||||
|
||||
fun validate(): String? {
|
||||
return when {
|
||||
name.isEmpty() -> "智能体名称不能为空"
|
||||
name.length < 2 -> "智能体名称长度不能少于2个字符"
|
||||
name.length > 20 -> "智能体名称长度不能超过20个字符"
|
||||
desc.isEmpty() -> "智能体描述不能为空"
|
||||
desc.length > 100 -> "智能体描述长度不能超过100个字符"
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user