AI美化功能

This commit is contained in:
2025-09-23 18:32:01 +08:00
parent 88f379fe5b
commit ea911f113b
6 changed files with 319 additions and 129 deletions

View File

@@ -74,6 +74,11 @@ import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.offset
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.text.TextStyle
/**
* 添加智能体界面
*/
@@ -87,7 +92,10 @@ fun AddAgentScreen() {
var errorMessage by remember { mutableStateOf<String?>(null) }
var isProcessing by remember { mutableStateOf(false) }
var showWaveAnimation by remember { mutableStateOf(false) }
var isCreatingAgent by remember { mutableStateOf(false) } // 控制是否处于创建状态
var showManualCreationForm by remember { mutableStateOf(false) } // 控制是否显示手动创建表单
var tempDesc by remember { mutableStateOf("") } // 独立的临时描述变量
val keyboardController = LocalSoftwareKeyboardController.current
fun onNameChange(value: String) {
model.name = value.trim()
@@ -105,11 +113,33 @@ fun AddAgentScreen() {
else -> null
}
}
fun onTempDescChange(value: String) {
tempDesc = value.trim()
agnetDescError = when {
value.length > 100 -> "简介长度不能大于100"
else -> null
}
}
fun validate(): Boolean {
return agnetNameError == null && agnetDescError == null
}
// AI文案优化
suspend fun optimizeTextWithAI(content: String): String? {
return try {
val sessionId = ""
val response = com.aiosman.ravenow.data.api.ApiClient.api.agentMoment(
com.aiosman.ravenow.data.api.AgentMomentRequestBody(
generateText = content,
sessionId = sessionId
)
)
response.body()?.data
} catch (e: Exception) {
e.printStackTrace()
null
}
}
// 处理系统返回键
BackHandler {
@@ -168,6 +198,8 @@ fun AddAgentScreen() {
}
}
Spacer(modifier = Modifier.height(1.dp))
if (!isCreatingAgent) {
Column(
modifier = Modifier
.fillMaxWidth()
@@ -185,17 +217,19 @@ fun AddAgentScreen() {
contentScale = ContentScale.Crop
)
}
}
Spacer(modifier = Modifier.height(16.dp))
Column(
modifier = Modifier.fillMaxWidth()
.padding(start = 20.dp)
) {
Text(
text = "${AppState.profile?.nickName ?: "User"} 你好呀!今天想创造什么?",
text = "${AppState.profile?.nickName ?: "User"} ${stringResource(R.string.welcome_1)}",
fontSize = 16.sp,
fontWeight = FontWeight.W600
)
}
if (!isCreatingAgent) {
Spacer(modifier = Modifier.height(8.dp))
Column(
modifier = Modifier.fillMaxWidth()
@@ -203,15 +237,15 @@ fun AddAgentScreen() {
) {
if (!showManualCreation) {
Text(
text = "只需要一句话你的专属AI将在这里诞生。",
text = stringResource(R.string.welcome_2),
fontSize = 14.sp,
color = LocalAppTheme.current.text.copy(alpha = 0.6f),
)
}
}
}
Spacer(modifier = Modifier.height(24.dp))
if (!showManualCreation) {
Column(
modifier = Modifier
@@ -245,7 +279,6 @@ fun AddAgentScreen() {
)
) {
val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
Box(
modifier = Modifier
@@ -258,36 +291,68 @@ fun AddAgentScreen() {
}
)
FormTextInput2(
value = model.desc,
hint = "一个会写诗的AI一个懂你笑点的AI...",
background = appColors.inputBackground2,
focusRequester = focusRequester,
TextField(
value = tempDesc,
onValueChange = { value -> onTempDescChange(value) },
modifier = Modifier
.fillMaxWidth()
.height(95.dp)
) { value ->
onDescChange(value)
}
.focusRequester(focusRequester),
placeholder = {
Text(
text = stringResource(R.string.agent_desc_hint_auto),
color = Color.Gray
)
},
textStyle = TextStyle(
color = LocalAppTheme.current.text,
fontSize = 16.sp
),
colors = TextFieldDefaults.colors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
focusedContainerColor = appColors.inputBackground2,
unfocusedContainerColor = appColors.inputBackground2,
),
shape = RoundedCornerShape(10.dp),
supportingText = null,
trailingIcon = null,
leadingIcon = null
)
Row(
modifier = Modifier
.align(Alignment.BottomEnd)
.padding(end = 12.dp, bottom = 12.dp)
.noRippleClickable {
if (!isProcessing && model.desc.isNotEmpty()) {
// 只有在有内容且未处理中且未创建中时才可点击
if (tempDesc.isNotEmpty() && !isProcessing && !isCreatingAgent) {
isProcessing = true
showWaveAnimation = true // 显示构思动画
keyboardController?.hide()
model.viewModelScope.launch {
try {
//AI美化功能待实现
val optimizedText = optimizeTextWithAI(tempDesc)
if (optimizedText != null) {
onTempDescChange(optimizedText)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
isProcessing = false
showWaveAnimation = false // 隐藏构思动画
}
}
}
},
}
.then(
if (tempDesc.isEmpty() || isProcessing || isCreatingAgent) {
Modifier.alpha(0.5f)
} else {
Modifier
}
),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
@@ -298,7 +363,7 @@ fun AddAgentScreen() {
)
Spacer(modifier = Modifier.width(5.dp))
Text(
text = "AI美化",
text = stringResource(R.string.agent_text_beautify),
color = Color(0xFF6246FF),
fontSize = 14.sp
)
@@ -306,7 +371,8 @@ fun AddAgentScreen() {
}
}
Spacer(modifier = Modifier.height(24.dp))
if (showWaveAnimation) {
if ((isCreatingAgent && showWaveAnimation) || (isProcessing && showWaveAnimation)) {
// 显示构思动画
Row(
modifier = Modifier
.align(Alignment.Start)
@@ -385,12 +451,83 @@ fun AddAgentScreen() {
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "正在为你构思",
text = stringResource(R.string.ideaing),
color = Color.Black.copy(alpha = 0.6f),
fontSize = 14.sp
)
}
} else {
} else if (isCreatingAgent && !showWaveAnimation && showManualCreationForm) {
Column(
modifier = Modifier
.padding(horizontal = 16.dp)
.align(Alignment.Start)
) {
Text(
text = stringResource(R.string.avatar),
fontSize = 12.sp,
fontWeight = FontWeight.W600
)
Spacer(modifier = Modifier.height(4.dp))
Box(
modifier = Modifier
.size(72.dp)
.clip(CircleShape)
.background(
brush = Brush.linearGradient(
colors = listOf(
Color(0x777c45ed),
Color(0x777c68ef),
Color(0x557bd8f8)
)
)
)
.align(Alignment.Start)
.noRippleClickable {
// 设置正在选择头像的标志
model.isSelectingAvatar = true
navController.navigate(NavigationRoute.AgentImageCrop.route)
},
contentAlignment = Alignment.Center
) {
Icon(
painter = painterResource(id = R.mipmap.icons_infor_edit),
contentDescription = "Edit",
tint = Color.White,
modifier = Modifier.size(20.dp),
)
}
Spacer(modifier = Modifier.height(18.dp))
// 原版两个输入框
Text(
text = stringResource(R.string.agent_name),
fontSize = 12.sp,
fontWeight = FontWeight.W600
)
Spacer(modifier = Modifier.height(4.dp))
FormTextInput(
value = model.name,
hint = stringResource(R.string.agent_name_hint_1),
background = appColors.inputBackground2,
modifier = Modifier.fillMaxWidth(),
) { value ->
onNameChange(value)
}
Text(
text = stringResource(R.string.agent_desc),
fontSize = 12.sp,
fontWeight = FontWeight.W600
)
Spacer(modifier = Modifier.height(4.dp))
FormTextInput2(
value = model.desc,
hint = stringResource(R.string.agent_desc_hint),
background = appColors.inputBackground2,
modifier = Modifier.fillMaxWidth(),
) { value ->
onDescChange(value)
}
}
} else if (!isCreatingAgent && !showWaveAnimation) {
Box(
modifier = Modifier
.align(Alignment.Start)
@@ -423,7 +560,7 @@ fun AddAgentScreen() {
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "手动创造Ai",
text = stringResource(R.string.create_agent_hand),
color = Color.Black,
fontWeight = FontWeight.W600,
fontSize = 14.sp
@@ -437,7 +574,6 @@ fun AddAgentScreen() {
.padding(horizontal = 16.dp)
.align(Alignment.Start)
) {
// 添加新的一句话创造AI按钮
Box(
modifier = Modifier
.align(Alignment.Start)
@@ -482,7 +618,7 @@ fun AddAgentScreen() {
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "一句话创造AI",
text = stringResource(R.string.create_agent_auto),
color = Color.Black,
fontWeight = FontWeight.W600,
fontSize = 14.sp
@@ -492,7 +628,7 @@ fun AddAgentScreen() {
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "头像",
text = stringResource(R.string.avatar),
fontSize = 12.sp,
fontWeight = FontWeight.W600
)
@@ -540,7 +676,7 @@ fun AddAgentScreen() {
Spacer(modifier = Modifier.height(4.dp))
FormTextInput(
value = model.name,
hint = "给它取个名字,让它成为独一无二的你",
hint = stringResource(R.string.agent_name_hint_1),
background = appColors.inputBackground2,
modifier = Modifier.fillMaxWidth(),
) { value ->
@@ -594,10 +730,12 @@ fun AddAgentScreen() {
),
color = Color.White,
backgroundColor = Color.Transparent,
text = stringResource(R.string.create_confirm),
text = if (isCreatingAgent && showManualCreationForm) stringResource(R.string.create_confirm) else stringResource(R.string.create_confirm),
isLoading = model.isUpdating,
enabled = !model.isUpdating && validate()
) {
// 如果处于构思完成状态且已显示手动创建表单,则执行创建操作
if (isCreatingAgent && showManualCreationForm) {
// 验证输入
val validationError = model.validate()
if (validationError != null) {
@@ -613,9 +751,6 @@ fun AddAgentScreen() {
// 清除之前的错误信息
errorMessage = null
// 显示波动动画
showWaveAnimation = true
// 调用创建智能体API
model.viewModelScope.launch {
try {
@@ -626,14 +761,38 @@ fun AddAgentScreen() {
navController.popBackStack()
}
} catch (e: Exception) {
// 隐藏波动动画
showWaveAnimation = false
// 显示错误信息
errorMessage = "创建智能体失败: ${e.message}"
e.printStackTrace()
}
}
} else if (isCreatingAgent && showWaveAnimation) {
return@ActionButton
} else {
if (tempDesc.isEmpty()) {
errorMessage = "智能体描述不能为空"
model.viewModelScope.launch {
kotlinx.coroutines.delay(3000)
errorMessage = null
}
return@ActionButton
}
// 清除之前的错误信息
errorMessage = null
// 开始构思过程
isCreatingAgent = true
showWaveAnimation = true
// 添加延时
model.viewModelScope.launch {
kotlinx.coroutines.delay(2000)
showWaveAnimation = false
showManualCreationForm = true
onDescChange(tempDesc)
}
}
}
}

View File

@@ -66,9 +66,9 @@ object AddAgentViewModel : ViewModel() {
fun validate(): String? {
return when {
// name.isEmpty() -> "智能体名称不能为空"
// name.length < 2 -> "智能体名称长度不能少于2个字符"
// name.length > 20 -> "智能体名称长度不能超过20个字符"
name.isEmpty() -> "智能体名称不能为空"
name.length < 2 -> "智能体名称长度不能少于2个字符"
name.length > 20 -> "智能体名称长度不能超过20个字符"
desc.isEmpty() -> "智能体描述不能为空"
desc.length > 100 -> "智能体描述长度不能超过100个字符"
else -> null

View File

@@ -112,7 +112,7 @@ fun FormTextInput2(
}
BasicTextField(
maxLines = 5,
maxLines = 6,
value = value,
onValueChange = {
onValueChange(it)

View File

@@ -6,4 +6,15 @@
<string name="create_group_chat_option">グループチャット</string>
<string name="create_moment">モーメント</string>
<string name="create_close">閉じる</string>
<!-- Create Agent Page -->
<string name="welcome_1">こんにちは!今日は何を作りたいですか?</string>
<string name="welcome_2">たった一言で、あなた専用のAIがここで生まれます。</string>
<string name="agent_desc_hint_auto">詩を書くAI、あなたの笑いを理解するAI...</string>
<string name="agent_text_beautify">AI美化</string>
<string name="ideaing">アイデアを考え中</string>
<string name="avatar">アバター</string>
<string name="create_agent_hand">手動でAIを作成</string>
<string name="create_agent_auto">一言でAIを作成</string>
<string name="agent_name_hint_1">名前を付けて、あなただけの特別な存在にしましょう</string>
</resources>

View File

@@ -199,4 +199,14 @@
<string name="create_moment">动态</string>
<string name="create_close">关闭</string>
<!-- Create Agent Page -->
<string name="welcome_1">你好呀!今天想创造什么?</string>
<string name="welcome_2">只需要一句话你的专属AI将在这里诞生。</string>
<string name="agent_desc_hint_auto">一个会写诗的AI一个懂你笑点的AI...</string>
<string name="agent_text_beautify">AI美化</string>
<string name="ideaing">正在为你构思</string>
<string name="avatar">头像</string>
<string name="create_agent_hand">手动创造Ai</string>
<string name="create_agent_auto">一句话创造Ai</string>
<string name="agent_name_hint_1">给它取个名字,让它成为独一无二的你</string>
</resources>

View File

@@ -195,4 +195,14 @@
<string name="create_moment">Moment</string>
<string name="create_close">Close</string>
<!-- Create Agent Page -->
<string name="welcome_1">Hello ! What would you like to create today?</string>
<string name="welcome_2">Just one sentence, and your exclusive AI will be born here.</string>
<string name="agent_desc_hint_auto">An AI that writes poetry, an AI that understands your sense of humor...</string>
<string name="agent_text_beautify">AI Beautify</string>
<string name="ideaing">Brainstorming for you</string>
<string name="avatar">Avatar</string>
<string name="create_agent_hand">Create AI Manually</string>
<string name="create_agent_auto">Create AI with One Sentence</string>
<string name="agent_name_hint_1">Give it a name to make it uniquely yours</string>
</resources>