修复添加Agent时取消选择头像后数据被清空的问题
- 引入`isSelectingAvatar`状态来标记是否正在选择头像。 - 当用户从图片裁剪页面返回或取消选择时,不再清空`AddAgentViewModel`中的数据。 - 仅当用户从`AddAgent`页面返回(并且不是在选择头像的过程中)时,才清空已填写的数据。 - 调整了`AddAgent`页面的返回逻辑,以确保在选择头像过程中返回时数据得以保留。
This commit is contained in:
@@ -25,6 +25,7 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
@@ -44,6 +45,14 @@ import com.aiosman.ravenow.ui.account.AccountEditViewModel
|
||||
import com.aiosman.ravenow.ui.comment.NoticeScreenHeader
|
||||
import com.aiosman.ravenow.ui.comment.ScreenHeader
|
||||
import com.aiosman.ravenow.ui.comment.ScreenHeader2
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.ui.graphics.ColorFilter
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import com.aiosman.ravenow.ui.composables.ActionButton
|
||||
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
|
||||
import com.aiosman.ravenow.ui.composables.StatusBarSpacer
|
||||
@@ -89,11 +98,18 @@ fun AddAgentScreen() {
|
||||
}
|
||||
|
||||
|
||||
// 页面退出时清空数据
|
||||
DisposableEffect(Unit) {
|
||||
onDispose {
|
||||
// 处理系统返回键
|
||||
BackHandler {
|
||||
// 如果不是在选择头像过程中,则清空数据
|
||||
if (!model.isSelectingAvatar) {
|
||||
model.clearData()
|
||||
}
|
||||
navController.popBackStack()
|
||||
}
|
||||
|
||||
// 页面进入时重置头像选择状态
|
||||
LaunchedEffect(Unit) {
|
||||
model.isSelectingAvatar = false
|
||||
}
|
||||
|
||||
Column(
|
||||
@@ -107,18 +123,46 @@ fun AddAgentScreen() {
|
||||
modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)
|
||||
.background(color = appColors.decentBackground)
|
||||
) {
|
||||
ScreenHeader2 (
|
||||
title = stringResource(R.string.agent_add),
|
||||
moreIcon = false
|
||||
// 自定义header,控制返回按钮行为
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(id = R.drawable.rider_pro_back_icon),
|
||||
contentDescription = "返回",
|
||||
modifier = Modifier.size(24.dp).clickable(
|
||||
indication = null,
|
||||
interactionSource = remember { MutableInteractionSource() }
|
||||
) {
|
||||
// 与BackHandler保持一致的逻辑
|
||||
if (!model.isSelectingAvatar) {
|
||||
model.clearData()
|
||||
}
|
||||
navController.navigateUp()
|
||||
},
|
||||
colorFilter = ColorFilter.tint(appColors.text)
|
||||
)
|
||||
Spacer(modifier = Modifier.size(12.dp))
|
||||
Text(
|
||||
stringResource(R.string.agent_add),
|
||||
fontWeight = FontWeight.W600,
|
||||
modifier = Modifier.weight(1f),
|
||||
textAlign = TextAlign.Center,
|
||||
fontSize = 17.sp,
|
||||
color = appColors.text
|
||||
)
|
||||
Spacer(modifier = Modifier.size(12.dp))
|
||||
Icon(
|
||||
modifier = Modifier
|
||||
.size(24.dp)
|
||||
|
||||
.noRippleClickable {
|
||||
// 提交创建智能体的逻辑可以在这里实现
|
||||
},
|
||||
imageVector = Icons.Default.Check,
|
||||
contentDescription = "Add")
|
||||
contentDescription = "Add",
|
||||
tint = appColors.text
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.height(44.dp))
|
||||
@@ -146,6 +190,8 @@ fun AddAgentScreen() {
|
||||
.background(appColors.main)
|
||||
.align(Alignment.BottomEnd)
|
||||
.noRippleClickable {
|
||||
// 设置正在选择头像的标志
|
||||
model.isSelectingAvatar = true
|
||||
navController.navigate(NavigationRoute.AgentImageCrop.route)
|
||||
},
|
||||
contentAlignment = Alignment.Center
|
||||
|
||||
@@ -23,7 +23,7 @@ object AddAgentViewModel : ViewModel() {
|
||||
var desc by mutableStateOf("")
|
||||
var croppedBitmap by mutableStateOf<Bitmap?>(null)
|
||||
var isUpdating by mutableStateOf(false)
|
||||
var isFromAddAgent by mutableStateOf(false)
|
||||
var isSelectingAvatar by mutableStateOf(false) // 标记是否正在选择头像
|
||||
|
||||
suspend fun updateAgentAvatar(context: Context) {
|
||||
croppedBitmap?.let {
|
||||
@@ -83,6 +83,6 @@ object AddAgentViewModel : ViewModel() {
|
||||
desc = ""
|
||||
croppedBitmap = null
|
||||
isUpdating = false
|
||||
isFromAddAgent = false
|
||||
isSelectingAvatar = false
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,8 @@ fun AgentImageCropScreen() {
|
||||
}
|
||||
}
|
||||
if (uri == null) {
|
||||
// 用户取消选择图片,重置标志
|
||||
AddAgentViewModel.isSelectingAvatar = false
|
||||
navController.popBackStack()
|
||||
}
|
||||
}
|
||||
@@ -119,6 +121,8 @@ fun AgentImageCropScreen() {
|
||||
painter = painterResource(R.drawable.rider_pro_back_icon),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.clickable {
|
||||
// 用户取消头像选择,重置标志
|
||||
AddAgentViewModel.isSelectingAvatar = false
|
||||
navController.popBackStack()
|
||||
},
|
||||
colorFilter = ColorFilter.tint(Color.White)
|
||||
@@ -134,6 +138,8 @@ fun AgentImageCropScreen() {
|
||||
if (croppedBitmap != null) {
|
||||
// 如果已经有裁剪结果,直接返回
|
||||
AddAgentViewModel.croppedBitmap = croppedBitmap
|
||||
// 重置头像选择标志
|
||||
AddAgentViewModel.isSelectingAvatar = false
|
||||
AddAgentViewModel.viewModelScope.launch {
|
||||
AddAgentViewModel.updateAgentAvatar(context)
|
||||
navController.popBackStack()
|
||||
|
||||
Reference in New Issue
Block a user