顶部 推荐 agent 列表实现

This commit is contained in:
2025-10-17 17:21:52 +08:00
parent 4bdbbb0231
commit 28fb94a824
3 changed files with 145 additions and 16 deletions

View File

@@ -16,9 +16,17 @@ import com.aiosman.ravenow.ui.index.tabs.ai.tabs.mine.MineAgentViewModel.createG
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.index.tabs.message.MessageListViewModel.userService
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.expolre.AgentItem
import com.aiosman.ravenow.utils.Utils
import kotlinx.coroutines.launch
/**
* 缓存数据结构用于存储每个分类的Agent列表
*/
data class AgentCacheData(
val items: List<AgentItem>,
val currentPage: Int,
val hasMoreData: Boolean
)
object AgentViewModel: ViewModel() {
private val apiClient: RaveNowAPI = ApiClient.api
@@ -48,17 +56,34 @@ object AgentViewModel: ViewModel() {
var hasMoreData by mutableStateOf(true)
private set
private val pageSize = 20
private var currentCategoryId: Int? = null
// 缓存使用分类ID作为keynull表示推荐列表
private val agentCache = mutableMapOf<Int?, AgentCacheData>()
init {
loadAgentData()
loadCategories()
}
private fun loadAgentData(categoryId: Int? = null, page: Int = 1, isLoadMore: Boolean = false) {
private fun loadAgentData(categoryId: Int? = null, page: Int = 1, isLoadMore: Boolean = false, forceRefresh: Boolean = false) {
viewModelScope.launch {
// 如果不是强制刷新且不是加载更多,检查缓存
if (!forceRefresh && !isLoadMore) {
val cached = agentCache[categoryId]
if (cached != null && cached.items.isNotEmpty()) {
// 使用缓存数据
agentItems = cached.items
currentPage = cached.currentPage
hasMoreData = cached.hasMoreData
currentCategoryId = categoryId
println("使用缓存数据分类ID: $categoryId, 数据数量: ${cached.items.size}")
return@launch
}
}
if (isLoadMore) {
isLoadingMore = true
} else {
@@ -77,11 +102,18 @@ object AgentViewModel: ViewModel() {
page = page,
pageSize = pageSize,
withWorkflow = 1,
categoryIds = listOf(categoryId)
categoryIds = listOf(categoryId),
random = 1
)
} else {
// 获取所有智能体
apiClient.getAgent(page = page, pageSize = pageSize, withWorkflow = 1)
// 获取推荐智能体使用random=1
apiClient.getAgent(
page = page,
pageSize = pageSize,
withWorkflow = 1,
categoryIds = null,
random = 1
)
}
if (response.isSuccessful) {
@@ -104,6 +136,14 @@ object AgentViewModel: ViewModel() {
// 检查是否还有更多数据
hasMoreData = agents.size >= pageSize
// 更新缓存
agentCache[categoryId] = AgentCacheData(
items = agentItems,
currentPage = currentPage,
hasMoreData = hasMoreData
)
println("更新缓存分类ID: $categoryId, 数据数量: ${agentItems.size}")
} else {
errorMessage = "获取Agent数据失败: ${response.code()}"
}
@@ -121,7 +161,15 @@ object AgentViewModel: ViewModel() {
private fun loadCategories() {
viewModelScope.launch {
// 如果分类已经加载,不重复请求
if (categories.isNotEmpty()) {
println("使用已缓存的分类数据,数量: ${categories.size}")
return@launch
}
try {
// 获取完整的语言标记(如 "zh-CN"
val sysLang = com.aiosman.ravenow.utils.Utils.getPreferredLanguageTag()
val response = apiClient.getCategories(
page = 1,
pageSize = 100,
@@ -130,14 +178,15 @@ object AgentViewModel: ViewModel() {
withParent = false,
withCount = true,
hideEmpty = true,
lang = Utils.getCurrentLanguage()
lang = sysLang
)
println("分类数据请求完成,响应成功: ${response.isSuccessful}")
println("分类数据请求完成,响应成功: ${response.isSuccessful}, 语言标记: $sysLang")
if (response.isSuccessful) {
val categoryList = response.body()?.list ?: emptyList()
println("获取到 ${categoryList.size} 个分类")
// 使用当前语言获取翻译后的分类名称
categories = categoryList.map { category ->
CategoryItem.fromCategoryTemplate(category)
CategoryItem.fromCategoryTemplate(category, sysLang)
}
println("成功处理并映射了 ${categories.size} 个分类")
} else {
@@ -214,10 +263,12 @@ object AgentViewModel: ViewModel() {
}
/**
* 刷新推荐Agent数据
* 刷新当前分类的Agent数据(强制刷新,清除缓存)
*/
fun refreshAgentData() {
loadAgentData()
// 清除当前分类的缓存
agentCache.remove(currentCategoryId)
loadAgentData(categoryId = currentCategoryId, forceRefresh = true)
}
/**
@@ -234,6 +285,7 @@ object AgentViewModel: ViewModel() {
*/
fun ResetModel() {
agentItems = emptyList()
categories = emptyList()
errorMessage = null
isRefreshing = false
isLoading = false
@@ -241,6 +293,8 @@ object AgentViewModel: ViewModel() {
currentPage = 1
hasMoreData = true
currentCategoryId = null
// 清空缓存
agentCache.clear()
}
}
@@ -252,11 +306,11 @@ data class CategoryItem(
val promptCount: Int?
) {
companion object {
fun fromCategoryTemplate(template: CategoryTemplate): CategoryItem {
fun fromCategoryTemplate(template: CategoryTemplate, lang: String): CategoryItem {
return CategoryItem(
id = template.id,
name = template.name,
description = template.description,
name = template.getLocalizedName(lang),
description = template.getLocalizedDescription(lang),
avatar = "${ApiClient.BASE_API_URL}${template.avatar}",
promptCount = template.promptCount
)