refactor: 优化回忆录功能

- 重构MyMemoirScreen我的回忆录页面
- 优化MyMemoirViewModel回忆录ViewModel
This commit is contained in:
iammm0
2026-01-26 11:54:14 +08:00
parent a18bb83c5e
commit 712ff915d8
2 changed files with 16 additions and 241 deletions

View File

@@ -28,14 +28,15 @@ import androidx.compose.material3.pulltorefresh.PullToRefreshBox
import com.huaga.life_echo.data.database.Chapter import com.huaga.life_echo.data.database.Chapter
import com.huaga.life_echo.network.models.ChapterContentDto import com.huaga.life_echo.network.models.ChapterContentDto
import com.huaga.life_echo.network.models.ChapterDto import com.huaga.life_echo.network.models.ChapterDto
import com.huaga.life_echo.ui.components.memoir.* import com.huaga.life_echo.ui.components.memoir.BookInfoCard
import com.huaga.life_echo.ui.components.memoir.ChapterCard
import com.huaga.life_echo.ui.components.memoir.ChapterReadingView
import com.huaga.life_echo.ui.components.memoir.FullTextReadingView
import com.huaga.life_echo.ui.components.common.EmptyStateView import com.huaga.life_echo.ui.components.common.EmptyStateView
import com.huaga.life_echo.ui.theme.LightPurple import com.huaga.life_echo.ui.theme.LightPurple
import com.huaga.life_echo.ui.viewmodel.MyMemoirViewModel import com.huaga.life_echo.ui.viewmodel.MyMemoirViewModel
import com.huaga.life_echo.ui.viewmodel.ViewModelFactory import com.huaga.life_echo.ui.viewmodel.ViewModelFactory
import com.huaga.life_echo.ui.viewmodel.ConversationListViewModel
import com.huaga.life_echo.ui.icons.AppIcons import com.huaga.life_echo.ui.icons.AppIcons
import com.huaga.life_echo.network.models.ConversationListItemDto
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
@@ -44,9 +45,6 @@ fun MyMemoirScreen(
navController: androidx.navigation.NavHostController? = null, navController: androidx.navigation.NavHostController? = null,
viewModel: MyMemoirViewModel = viewModel( viewModel: MyMemoirViewModel = viewModel(
factory = ViewModelFactory(LocalContext.current) factory = ViewModelFactory(LocalContext.current)
),
conversationListViewModel: ConversationListViewModel = viewModel(
factory = ViewModelFactory(LocalContext.current)
) )
) { ) {
val chapters by viewModel.chapters.collectAsState(initial = emptyList()) val chapters by viewModel.chapters.collectAsState(initial = emptyList())
@@ -55,14 +53,7 @@ fun MyMemoirScreen(
val bookInfo by viewModel.bookInfo.collectAsState() val bookInfo by viewModel.bookInfo.collectAsState()
val showFullTextReading by viewModel.showFullTextReading.collectAsState() val showFullTextReading by viewModel.showFullTextReading.collectAsState()
// 整理对话相关状态 // 注意:整理功能已自动化,不再需要手动触发
var showOrganizeDialog by remember { mutableStateOf(false) }
var showOrganizeSuccessDialog by remember { mutableStateOf(false) }
val conversations by conversationListViewModel.conversations.collectAsState(initial = emptyList())
val conversationsLoading by conversationListViewModel.isLoading.collectAsState()
val isOrganizing by viewModel.isOrganizing.collectAsState()
val organizingProgress by viewModel.organizingProgress.collectAsState()
val organizingStatus by viewModel.organizingStatus.collectAsState()
// 下拉刷新状态 // 下拉刷新状态
var isRefreshing by remember { mutableStateOf(false) } var isRefreshing by remember { mutableStateOf(false) }
@@ -80,31 +71,11 @@ fun MyMemoirScreen(
} }
} }
// 加载对话列表转换为ConversationListItemDto格式
val conversationDtos = remember(conversations) {
conversations.map { conv ->
ConversationListItemDto(
id = conv.id,
title = conv.title ?: "回忆录助手",
avatarUrl = conv.avatarUrl,
latestMessagePreview = conv.latestMessagePreview,
latestMessageTime = conv.latestMessageTime ?: conv.startedAt,
unreadCount = 0,
isDefaultAssistant = conv.title == null
)
}
}
// 加载对话列表 // 加载书籍信息和章节列表
LaunchedEffect(showOrganizeDialog) {
if (showOrganizeDialog) {
conversationListViewModel.refreshConversations()
}
}
// 加载书籍信息
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
viewModel.loadBookInfo() viewModel.loadBookInfo()
viewModel.refreshChapters()
} }
// 转换Chapter为ChapterDto用于显示 // 转换Chapter为ChapterDto用于显示
@@ -209,92 +180,17 @@ fun MyMemoirScreen(
// 操作按钮区域 // 操作按钮区域
item { item {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 16.dp)
) {
// 整理进度条(如果正在整理)
if (isOrganizing) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp),
shape = RoundedCornerShape(12.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Text(
text = organizingStatus.ifEmpty { "正在整理对话..." },
fontSize = 14.sp,
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier.padding(bottom = 8.dp)
)
LinearProgressIndicator(
progress = organizingProgress,
modifier = Modifier.fillMaxWidth(),
color = LightPurple,
trackColor = MaterialTheme.colorScheme.surfaceVariant
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = "${(organizingProgress * 100).toInt()}%",
fontSize = 12.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.align(Alignment.End)
)
}
}
}
// 操作按钮
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(horizontal = 16.dp), .padding(horizontal = 16.dp, vertical = 16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp) horizontalArrangement = Arrangement.spacedBy(12.dp)
) { ) {
// 整理对话按钮(改进样式)
Button(
onClick = { showOrganizeDialog = true },
modifier = Modifier.weight(1f),
enabled = !isOrganizing,
colors = ButtonDefaults.buttonColors(
containerColor = LightPurple
),
shape = RoundedCornerShape(12.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Icon(
imageVector = AppIcons.Edit,
contentDescription = null,
tint = Color.White,
modifier = Modifier.size(20.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
"整理对话",
color = Color.White,
fontSize = 14.sp
)
}
}
// 「阅读全文」按钮 - 只在有章节时显示 // 「阅读全文」按钮 - 只在有章节时显示
if (chapterDtos.isNotEmpty()) { if (chapterDtos.isNotEmpty()) {
Button( Button(
onClick = { viewModel.toggleFullTextReading() }, onClick = { viewModel.toggleFullTextReading() },
modifier = Modifier.weight(1f), modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
containerColor = LightPurple containerColor = LightPurple
), ),
@@ -321,7 +217,6 @@ fun MyMemoirScreen(
} }
} }
} }
}
// 如果没有章节,显示空状态 // 如果没有章节,显示空状态
if (chapterDtos.isEmpty()) { if (chapterDtos.isEmpty()) {
@@ -396,49 +291,5 @@ fun MyMemoirScreen(
} }
} }
} }
// 整理对话对话框
if (showOrganizeDialog) {
OrganizeConversationDialog(
conversations = conversationDtos,
isLoading = conversationsLoading,
onDismiss = { showOrganizeDialog = false },
onSelectConversation = { conversationId ->
viewModel.organizeConversation(
conversationId = conversationId,
onSuccess = {
showOrganizeSuccessDialog = true
},
onError = { errorMsg ->
// 错误处理可以在这里添加
}
)
}
)
}
// 整理成功对话框
if (showOrganizeSuccessDialog) {
AlertDialog(
onDismissRequest = { showOrganizeSuccessDialog = false },
title = { Text("整理成功") },
text = {
Text(
text = "对话内容正在整理中,请稍后刷新查看章节。",
modifier = Modifier.wrapContentHeight()
)
},
confirmButton = {
TextButton(
onClick = {
showOrganizeSuccessDialog = false
viewModel.refreshChapters()
}
) {
Text("确定", color = LightPurple)
}
}
)
}
} }
} }

View File

@@ -29,11 +29,6 @@ class MyMemoirViewModel(
val memoirState = MutableStateFlow<MemoirStateDto?>(null) val memoirState = MutableStateFlow<MemoirStateDto?>(null)
val tasksStatus = MutableStateFlow<TasksStatusDto?>(null) val tasksStatus = MutableStateFlow<TasksStatusDto?>(null)
// 整理对话进度
val isOrganizing = MutableStateFlow(false)
val organizingProgress = MutableStateFlow(0f) // 0.0 - 1.0
val organizingStatus = MutableStateFlow("") // 状态文本
fun selectChapter(chapter: Chapter) { fun selectChapter(chapter: Chapter) {
selectedChapter.value = chapter selectedChapter.value = chapter
} }
@@ -172,76 +167,5 @@ class MyMemoirViewModel(
fun toggleFullTextReading() { fun toggleFullTextReading() {
showFullTextReading.value = !showFullTextReading.value showFullTextReading.value = !showFullTextReading.value
} }
/**
* 整理对话内容成章节
*/
fun organizeConversation(conversationId: String, onSuccess: () -> Unit, onError: (String) -> Unit) {
viewModelScope.launch {
isOrganizing.value = true
organizingProgress.value = 0f
organizingStatus.value = "正在提交整理任务..."
error.value = null
try {
val result = apiService.organizeConversation(conversationId)
result.fold(
onSuccess = {
organizingProgress.value = 0.3f
organizingStatus.value = "任务已提交,正在处理..."
// 模拟进度更新实际应该通过任务状态API获取
var progress = 0.3f
val progressSteps = listOf(0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f)
val statusSteps = listOf(
"正在分析对话内容...",
"正在分类整理...",
"正在生成章节...",
"正在优化内容...",
"即将完成..."
)
progressSteps.forEachIndexed { index, step ->
delay(1000)
progress = step
organizingProgress.value = progress
if (index < statusSteps.size) {
organizingStatus.value = statusSteps[index]
}
}
// 等待处理完成
delay(2000)
organizingProgress.value = 1.0f
organizingStatus.value = "整理完成!"
delay(500)
isOrganizing.value = false
organizingProgress.value = 0f
organizingStatus.value = ""
// 刷新章节列表
refreshChapters()
onSuccess()
},
onFailure = { exception ->
isOrganizing.value = false
organizingProgress.value = 0f
organizingStatus.value = ""
val errorMsg = "整理失败: ${exception.message}"
error.value = errorMsg
onError(errorMsg)
}
)
} catch (e: Exception) {
isOrganizing.value = false
organizingProgress.value = 0f
organizingStatus.value = ""
val errorMsg = "整理失败: ${e.message}"
error.value = errorMsg
onError(errorMsg)
}
}
}
} }