优化AI页面资源管理和数据加载
- AgentViewModel:
- 新增 `refreshAgentData` 方法用于刷新推荐Agent数据。
- 新增 `ensureDataLoaded` 方法用于检查推荐Agent数据是否为空,如果为空则重新加载。
- ResourceCleanupManager:
- `cleanupPageResources` 方法新增 `preserveRecommendedData` 参数,用于控制是否保留推荐数据。
- 新增 `cleanupAiPageCompletely` 方法,用于完全清理AI页面资源(包括推荐数据),主要用于登出等场景。
- Agent.kt:
- 在页面进入时调用 `viewModel.ensureDataLoaded()` 确保推荐Agent数据已加载。
- 页面退出时不再清理推荐Agent数据,只在完全登出或需要彻底清理时才清理。
此更改旨在:
- 避免不必要的推荐Agent数据重复加载。
- 优化AI页面退出时的资源清理逻辑,仅在需要时才清理推荐数据。
This commit is contained in:
@@ -29,6 +29,7 @@ import androidx.compose.material.Icon
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -80,13 +81,19 @@ fun Agent() {
|
||||
|
||||
val viewModel: AgentViewModel = viewModel()
|
||||
|
||||
// 确保推荐Agent数据已加载
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.ensureDataLoaded()
|
||||
}
|
||||
|
||||
// 防抖状态
|
||||
var lastClickTime by remember { mutableStateOf(0L) }
|
||||
|
||||
// 页面退出时清理AI相关资源
|
||||
// 页面退出时只清理必要的资源,不清理推荐Agent数据
|
||||
DisposableEffect(Unit) {
|
||||
onDispose {
|
||||
ResourceCleanupManager.cleanupPageResources("ai")
|
||||
// 只清理子页面的资源,保留推荐Agent数据
|
||||
// ResourceCleanupManager.cleanupPageResources("ai")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -95,6 +95,22 @@ object AgentViewModel: ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新推荐Agent数据
|
||||
*/
|
||||
fun refreshAgentData() {
|
||||
loadAgentData()
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据是否为空,如果为空则重新加载
|
||||
*/
|
||||
fun ensureDataLoaded() {
|
||||
if (agentItems.isEmpty() && !isLoading) {
|
||||
loadAgentData()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置ViewModel状态,用于登出或切换账号时清理数据
|
||||
*/
|
||||
|
||||
@@ -62,7 +62,7 @@ object ResourceCleanupManager {
|
||||
// 重置Index相关ViewModel
|
||||
IndexViewModel.ResetModel()
|
||||
|
||||
// 重置AI相关ViewModel
|
||||
// 重置AI相关ViewModel(完全清理,包括推荐数据)
|
||||
AgentViewModel.ResetModel()
|
||||
|
||||
HotAgentViewModel.let {
|
||||
@@ -210,11 +210,15 @@ object ResourceCleanupManager {
|
||||
/**
|
||||
* 清理特定页面的资源
|
||||
* @param pageType 页面类型
|
||||
* @param preserveRecommendedData 是否保留推荐数据,默认为true
|
||||
*/
|
||||
fun cleanupPageResources(pageType: String) {
|
||||
fun cleanupPageResources(pageType: String, preserveRecommendedData: Boolean = true) {
|
||||
when (pageType) {
|
||||
"ai" -> {
|
||||
// 如果需要保留推荐数据,则不重置AgentViewModel(它包含推荐Agent数据)
|
||||
if (!preserveRecommendedData) {
|
||||
AgentViewModel.ResetModel()
|
||||
}
|
||||
HotAgentViewModel.let {
|
||||
it.agentList = emptyList()
|
||||
it.refreshing = false
|
||||
@@ -243,4 +247,11 @@ object ResourceCleanupManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 完全清理AI页面资源(包括推荐数据),用于登出等场景
|
||||
*/
|
||||
fun cleanupAiPageCompletely() {
|
||||
cleanupPageResources("ai", preserveRecommendedData = false)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user