我的智能体

This commit is contained in:
weber
2025-08-05 17:01:23 +08:00
parent dc9c013383
commit 993604bfc1
4 changed files with 131 additions and 35 deletions

View File

@@ -60,8 +60,7 @@ fun AgentCard(
contentDescription = "", contentDescription = "",
modifier = Modifier modifier = Modifier
.size(40.dp) .size(40.dp)
.clip(RoundedCornerShape(40.dp)) .clip(RoundedCornerShape(40.dp)),
,
contentScale = ContentScale.Crop contentScale = ContentScale.Crop
) )
Column( Column(
@@ -80,12 +79,9 @@ fun AgentCard(
textAlign = TextAlign.Start, textAlign = TextAlign.Start,
text = agentEntity.title, text = agentEntity.title,
color = AppColors.text, color = AppColors.text,
fontSize = 16.sp, style = TextStyle(fontWeight = FontWeight.Bold) fontSize = 16.sp,
style = TextStyle(fontWeight = FontWeight.Bold)
) )
}
Spacer(modifier = Modifier.width(16.dp))
} }
Row( Row(
modifier = Modifier modifier = Modifier
@@ -105,4 +101,5 @@ fun AgentCard(
} }
} }
} }
}
} }

View File

@@ -36,6 +36,7 @@ import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.NavigationRoute import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.index.tabs.ai.tabs.mine.MineAgent
import com.aiosman.ravenow.ui.modifiers.noRippleClickable import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -226,7 +227,7 @@ fun Agent() {
) { ) {
when (it) { when (it) {
0 -> { 0 -> {
MineAgent()
} }
1 -> { 1 -> {

View File

@@ -13,6 +13,8 @@ import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.derivedStateOf
@@ -37,9 +39,7 @@ fun MineAgent() {
var agentList = model.agentList var agentList = model.agentList
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val state = rememberPullRefreshState(model.refreshing, onRefresh = { val state = rememberPullRefreshState(model.refreshing, onRefresh = {
model.refreshPager( model.refreshPager(pullRefresh = true)
pullRefresh = true
)
}) })
val listState = rememberLazyListState() val listState = rememberLazyListState()
@@ -53,19 +53,20 @@ fun MineAgent() {
// load more if scrolled to bottom // load more if scrolled to bottom
LaunchedEffect(reachedBottom) { LaunchedEffect(reachedBottom) {
if (reachedBottom) { if (reachedBottom && !model.isLoading && model.hasNext) {
model.loadMore() model.loadMore()
} }
} }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
model.refreshPager() model.refreshPager()
} }
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
) { ) {
if(agentList.size == 0) { if(agentList.isEmpty() && !model.isLoading) {
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxSize(), .fillMaxSize(),
@@ -81,22 +82,22 @@ fun MineAgent() {
modifier = Modifier.size(140.dp) modifier = Modifier.size(140.dp)
) )
Spacer(modifier = Modifier.size(32.dp)) Spacer(modifier = Modifier.size(32.dp))
androidx.compose.material.Text( Text(
text = "You haven't followed anyone yet", text = "您还没有创建任何智能体",
color = AppColors.text, color = AppColors.text,
fontSize = 16.sp, fontSize = 16.sp,
fontWeight = FontWeight.W600 fontWeight = FontWeight.W600
) )
Spacer(modifier = Modifier.size(16.dp)) Spacer(modifier = Modifier.size(16.dp))
androidx.compose.material.Text( Text(
text = "Click start your social journey.", text = "点击开始创建您的第一个智能体",
color = AppColors.text, color = AppColors.text,
fontSize = 16.sp, fontSize = 16.sp,
fontWeight = FontWeight.W400 fontWeight = FontWeight.W400
) )
} }
} }
}else{ } else {
Box(Modifier.pullRefresh(state)) { Box(Modifier.pullRefresh(state)) {
LazyColumn( LazyColumn(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
@@ -106,14 +107,43 @@ fun MineAgent() {
agentList.size, agentList.size,
key = { idx -> idx } key = { idx -> idx }
) { idx -> ) { idx ->
val agentItem = agentList[idx] ?: return@items val agentItem = agentList[idx]
AgentCard(agentEntity = agentItem, AgentCard(agentEntity = agentItem)
}
// 加载更多指示器
if (model.isLoading && agentList.isNotEmpty()) {
item {
Box(
modifier = Modifier
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator(
modifier = Modifier.size(24.dp),
color = AppColors.main
) )
} }
} }
}
}
PullRefreshIndicator(model.refreshing, state, Modifier.align(Alignment.TopCenter)) PullRefreshIndicator(model.refreshing, state, Modifier.align(Alignment.TopCenter))
} }
} }
// 错误信息显示
model.error?.let { error ->
Box(
modifier = Modifier
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Text(
text = error,
color = AppColors.error,
fontSize = 14.sp
)
}
}
} }
} }

View File

@@ -3,20 +3,88 @@ package com.aiosman.ravenow.ui.index.tabs.ai.tabs.mine
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import com.aiosman.ravenow.entity.AgentLoaderExtraArgs import androidx.lifecycle.ViewModel
import com.aiosman.ravenow.entity.MomentLoaderExtraArgs import androidx.lifecycle.viewModelScope
import com.aiosman.ravenow.ui.index.tabs.ai.BaseAgentModel import com.aiosman.ravenow.data.api.ApiClient
import com.aiosman.ravenow.ui.index.tabs.moment.BaseMomentModel import com.aiosman.ravenow.data.ServiceException
import org.greenrobot.eventbus.EventBus import com.aiosman.ravenow.entity.AgentEntity
import kotlinx.coroutines.launch
object MineAgentViewModel : ViewModel() {
var agentList by mutableStateOf<List<AgentEntity>>(emptyList())
var refreshing by mutableStateOf(false)
var isLoading by mutableStateOf(false)
var hasNext by mutableStateOf(true)
var currentPage by mutableStateOf(1)
var error by mutableStateOf<String?>(null)
object MineAgentViewModel : BaseAgentModel() { private val pageSize = 20
init { init {
//EventBus.getDefault().register(this) refreshPager()
}
override fun extraArgs(): AgentLoaderExtraArgs {
return AgentLoaderExtraArgs()
} }
fun refreshPager(pullRefresh: Boolean = false) {
if (isLoading && !pullRefresh) return
viewModelScope.launch {
try {
isLoading = true
refreshing = pullRefresh
error = null
val response = ApiClient.api.getMyAgent(
page = 1,
pageSize = pageSize
)
val body = response.body()
if (body != null) {
val newAgents = body.list.map { it.toAgentEntity() }
agentList = newAgents
currentPage = 1
hasNext = newAgents.size == pageSize
} else {
throw ServiceException("Failed to load agents")
}
} catch (e: Exception) {
error = e.message ?: "加载失败"
e.printStackTrace()
} finally {
isLoading = false
refreshing = false
}
}
}
fun loadMore() {
if (isLoading || !hasNext) return
viewModelScope.launch {
try {
isLoading = true
error = null
val response = ApiClient.api.getMyAgent(
page = currentPage + 1,
pageSize = pageSize
)
val body = response.body()
if (body != null) {
val newAgents = body.list.map { it.toAgentEntity() }
agentList = agentList + newAgents
currentPage += 1
hasNext = newAgents.size == pageSize
} else {
throw ServiceException("Failed to load more agents")
}
} catch (e: Exception) {
error = e.message ?: "加载更多失败"
e.printStackTrace()
} finally {
isLoading = false
}
}
}
} }