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.rememberInfiniteTransition
import androidx.compose.animation.core.tween import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.offset 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 errorMessage by remember { mutableStateOf<String?>(null) }
var isProcessing by remember { mutableStateOf(false) } var isProcessing by remember { mutableStateOf(false) }
var showWaveAnimation 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) { fun onNameChange(value: String) {
model.name = value.trim() model.name = value.trim()
@@ -105,11 +113,33 @@ fun AddAgentScreen() {
else -> null else -> null
} }
} }
fun onTempDescChange(value: String) {
tempDesc = value.trim()
agnetDescError = when {
value.length > 100 -> "简介长度不能大于100"
else -> null
}
}
fun validate(): Boolean { fun validate(): Boolean {
return agnetNameError == null && agnetDescError == null 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 { BackHandler {
@@ -168,49 +198,53 @@ fun AddAgentScreen() {
} }
} }
Spacer(modifier = Modifier.height(1.dp)) Spacer(modifier = Modifier.height(1.dp))
Column(
modifier = Modifier if (!isCreatingAgent) {
.fillMaxWidth() Column(
.height(50.dp)
.padding(horizontal = 20.dp),
) {
Image(
painter = painterResource(id = R.mipmap.group_copy),
contentDescription = "",
modifier = Modifier modifier = Modifier
.size(48.dp) .fillMaxWidth()
.clip( .height(50.dp)
RoundedCornerShape(48.dp) .padding(horizontal = 20.dp),
), ) {
contentScale = ContentScale.Crop Image(
) painter = painterResource(id = R.mipmap.group_copy),
} contentDescription = "",
Spacer(modifier = Modifier.height(16.dp)) modifier = Modifier
Column( .size(48.dp)
modifier = Modifier.fillMaxWidth() .clip(
.padding(start = 20.dp) RoundedCornerShape(48.dp)
) { ),
Text( contentScale = ContentScale.Crop
text = "${AppState.profile?.nickName ?: "User"} 你好呀!今天想创造什么?",
fontSize = 16.sp,
fontWeight = FontWeight.W600
)
}
Spacer(modifier = Modifier.height(8.dp))
Column(
modifier = Modifier.fillMaxWidth()
.padding(start = 20.dp)
) {
if (!showManualCreation) {
Text(
text = "只需要一句话你的专属AI将在这里诞生。",
fontSize = 14.sp,
color = LocalAppTheme.current.text.copy(alpha = 0.6f),
) )
} }
} }
Spacer(modifier = Modifier.height(24.dp)) Spacer(modifier = Modifier.height(16.dp))
Column(
modifier = Modifier.fillMaxWidth()
.padding(start = 20.dp)
) {
Text(
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()
.padding(start = 20.dp)
) {
if (!showManualCreation) {
Text(
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) { if (!showManualCreation) {
Column( Column(
@@ -245,7 +279,6 @@ fun AddAgentScreen() {
) )
) { ) {
val focusRequester = remember { FocusRequester() } val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
Box( Box(
modifier = Modifier modifier = Modifier
@@ -258,36 +291,68 @@ fun AddAgentScreen() {
} }
) )
FormTextInput2( TextField(
value = model.desc, value = tempDesc,
hint = "一个会写诗的AI一个懂你笑点的AI...", onValueChange = { value -> onTempDescChange(value) },
background = appColors.inputBackground2,
focusRequester = focusRequester,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(95.dp) .height(95.dp)
) { value -> .focusRequester(focusRequester),
onDescChange(value) 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( Row(
modifier = Modifier modifier = Modifier
.align(Alignment.BottomEnd) .align(Alignment.BottomEnd)
.padding(end = 12.dp, bottom = 12.dp) .padding(end = 12.dp, bottom = 12.dp)
.noRippleClickable { .noRippleClickable {
if (!isProcessing && model.desc.isNotEmpty()) { // 只有在有内容且未处理中且未创建中时才可点击
if (tempDesc.isNotEmpty() && !isProcessing && !isCreatingAgent) {
isProcessing = true isProcessing = true
showWaveAnimation = true // 显示构思动画
keyboardController?.hide()
model.viewModelScope.launch { model.viewModelScope.launch {
try { try {
//AI美化功能待实现 val optimizedText = optimizeTextWithAI(tempDesc)
if (optimizedText != null) {
onTempDescChange(optimizedText)
}
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} finally { } finally {
isProcessing = false isProcessing = false
showWaveAnimation = false // 隐藏构思动画
} }
} }
} }
}, }
.then(
if (tempDesc.isEmpty() || isProcessing || isCreatingAgent) {
Modifier.alpha(0.5f)
} else {
Modifier
}
),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
Icon( Icon(
@@ -298,7 +363,7 @@ fun AddAgentScreen() {
) )
Spacer(modifier = Modifier.width(5.dp)) Spacer(modifier = Modifier.width(5.dp))
Text( Text(
text = "AI美化", text = stringResource(R.string.agent_text_beautify),
color = Color(0xFF6246FF), color = Color(0xFF6246FF),
fontSize = 14.sp fontSize = 14.sp
) )
@@ -306,7 +371,8 @@ fun AddAgentScreen() {
} }
} }
Spacer(modifier = Modifier.height(24.dp)) Spacer(modifier = Modifier.height(24.dp))
if (showWaveAnimation) { if ((isCreatingAgent && showWaveAnimation) || (isProcessing && showWaveAnimation)) {
// 显示构思动画
Row( Row(
modifier = Modifier modifier = Modifier
.align(Alignment.Start) .align(Alignment.Start)
@@ -385,12 +451,83 @@ fun AddAgentScreen() {
Spacer(modifier = Modifier.width(8.dp)) Spacer(modifier = Modifier.width(8.dp))
Text( Text(
text = "正在为你构思", text = stringResource(R.string.ideaing),
color = Color.Black.copy(alpha = 0.6f), color = Color.Black.copy(alpha = 0.6f),
fontSize = 14.sp 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( Box(
modifier = Modifier modifier = Modifier
.align(Alignment.Start) .align(Alignment.Start)
@@ -423,7 +560,7 @@ fun AddAgentScreen() {
) )
Spacer(modifier = Modifier.width(8.dp)) Spacer(modifier = Modifier.width(8.dp))
Text( Text(
text = "手动创造Ai", text = stringResource(R.string.create_agent_hand),
color = Color.Black, color = Color.Black,
fontWeight = FontWeight.W600, fontWeight = FontWeight.W600,
fontSize = 14.sp fontSize = 14.sp
@@ -437,7 +574,6 @@ fun AddAgentScreen() {
.padding(horizontal = 16.dp) .padding(horizontal = 16.dp)
.align(Alignment.Start) .align(Alignment.Start)
) { ) {
// 添加新的一句话创造AI按钮
Box( Box(
modifier = Modifier modifier = Modifier
.align(Alignment.Start) .align(Alignment.Start)
@@ -482,7 +618,7 @@ fun AddAgentScreen() {
) )
Spacer(modifier = Modifier.width(8.dp)) Spacer(modifier = Modifier.width(8.dp))
Text( Text(
text = "一句话创造AI", text = stringResource(R.string.create_agent_auto),
color = Color.Black, color = Color.Black,
fontWeight = FontWeight.W600, fontWeight = FontWeight.W600,
fontSize = 14.sp fontSize = 14.sp
@@ -491,41 +627,41 @@ fun AddAgentScreen() {
} }
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
Text( Text(
text = "头像", text = stringResource(R.string.avatar),
fontSize = 12.sp, fontSize = 12.sp,
fontWeight = FontWeight.W600 fontWeight = FontWeight.W600
) )
Spacer(modifier = Modifier.height(4.dp)) Spacer(modifier = Modifier.height(4.dp))
Box( Box(
modifier = Modifier modifier = Modifier
.size(72.dp) .size(72.dp)
.clip(CircleShape) .clip(CircleShape)
.background( .background(
brush = Brush.linearGradient( brush = Brush.linearGradient(
colors = listOf( colors = listOf(
Color(0x777c45ed), Color(0x777c45ed),
Color(0x777c68ef), Color(0x777c68ef),
Color(0x557bd8f8) 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),
) )
.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)) Spacer(modifier = Modifier.height(18.dp))
// 原版两个输入框 // 原版两个输入框
Column( Column(
@@ -540,7 +676,7 @@ fun AddAgentScreen() {
Spacer(modifier = Modifier.height(4.dp)) Spacer(modifier = Modifier.height(4.dp))
FormTextInput( FormTextInput(
value = model.name, value = model.name,
hint = "给它取个名字,让它成为独一无二的你", hint = stringResource(R.string.agent_name_hint_1),
background = appColors.inputBackground2, background = appColors.inputBackground2,
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) { value -> ) { value ->
@@ -594,44 +730,67 @@ fun AddAgentScreen() {
), ),
color = Color.White, color = Color.White,
backgroundColor = Color.Transparent, 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, isLoading = model.isUpdating,
enabled = !model.isUpdating && validate() enabled = !model.isUpdating && validate()
) { ) {
// 验证输入 // 如果处于构思完成状态且已显示手动创建表单,则执行创建操作
val validationError = model.validate() if (isCreatingAgent && showManualCreationForm) {
if (validationError != null) { // 验证输入
// 显示验证错误 val validationError = model.validate()
errorMessage = validationError if (validationError != null) {
model.viewModelScope.launch { // 显示验证错误
kotlinx.coroutines.delay(3000) errorMessage = validationError
errorMessage = null model.viewModelScope.launch {
} kotlinx.coroutines.delay(3000)
return@ActionButton errorMessage = null
}
// 清除之前的错误信息
errorMessage = null
// 显示波动动画
showWaveAnimation = true
// 调用创建智能体API
model.viewModelScope.launch {
try {
val result = model.createAgent(context)
if (result != null) {
// 创建成功,清空数据并关闭页面
model.clearData()
navController.popBackStack()
} }
} catch (e: Exception) { return@ActionButton
// 隐藏波动动画 }
showWaveAnimation = false
// 显示错误信息 // 清除之前的错误信息
errorMessage = "创建智能体失败: ${e.message}" errorMessage = null
e.printStackTrace()
// 调用创建智能体API
model.viewModelScope.launch {
try {
val result = model.createAgent(context)
if (result != null) {
// 创建成功,清空数据并关闭页面
model.clearData()
navController.popBackStack()
}
} catch (e: Exception) {
// 显示错误信息
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? { fun validate(): String? {
return when { return when {
// name.isEmpty() -> "智能体名称不能为空" name.isEmpty() -> "智能体名称不能为空"
// name.length < 2 -> "智能体名称长度不能少于2个字符" name.length < 2 -> "智能体名称长度不能少于2个字符"
// name.length > 20 -> "智能体名称长度不能超过20个字符" name.length > 20 -> "智能体名称长度不能超过20个字符"
desc.isEmpty() -> "智能体描述不能为空" desc.isEmpty() -> "智能体描述不能为空"
desc.length > 100 -> "智能体描述长度不能超过100个字符" desc.length > 100 -> "智能体描述长度不能超过100个字符"
else -> null else -> null

View File

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

View File

@@ -6,4 +6,15 @@
<string name="create_group_chat_option">グループチャット</string> <string name="create_group_chat_option">グループチャット</string>
<string name="create_moment">モーメント</string> <string name="create_moment">モーメント</string>
<string name="create_close">閉じる</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> </resources>

View File

@@ -199,4 +199,14 @@
<string name="create_moment">动态</string> <string name="create_moment">动态</string>
<string name="create_close">关闭</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> </resources>

View File

@@ -195,4 +195,14 @@
<string name="create_moment">Moment</string> <string name="create_moment">Moment</string>
<string name="create_close">Close</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> </resources>