我的智能体

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 = "",
modifier = Modifier
.size(40.dp)
.clip(RoundedCornerShape(40.dp))
,
.clip(RoundedCornerShape(40.dp)),
contentScale = ContentScale.Crop
)
Column(
@@ -80,12 +79,9 @@ fun AgentCard(
textAlign = TextAlign.Start,
text = agentEntity.title,
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(
modifier = Modifier
@@ -103,6 +99,7 @@ fun AgentCard(
//MomentPostLocation(momentEntity.location)
}
}
}
}
}
}

View File

@@ -36,6 +36,7 @@ import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
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 kotlinx.coroutines.launch
@@ -226,7 +227,7 @@ fun Agent() {
) {
when (it) {
0 -> {
MineAgent()
}
1 -> {

View File

@@ -13,6 +13,8 @@ import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
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.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
@@ -37,9 +39,7 @@ fun MineAgent() {
var agentList = model.agentList
val scope = rememberCoroutineScope()
val state = rememberPullRefreshState(model.refreshing, onRefresh = {
model.refreshPager(
pullRefresh = true
)
model.refreshPager(pullRefresh = true)
})
val listState = rememberLazyListState()
@@ -53,19 +53,20 @@ fun MineAgent() {
// load more if scrolled to bottom
LaunchedEffect(reachedBottom) {
if (reachedBottom) {
if (reachedBottom && !model.isLoading && model.hasNext) {
model.loadMore()
}
}
LaunchedEffect(Unit) {
model.refreshPager()
}
Column(
modifier = Modifier
.fillMaxSize()
) {
if(agentList.size == 0) {
if(agentList.isEmpty() && !model.isLoading) {
Box(
modifier = Modifier
.fillMaxSize(),
@@ -81,22 +82,22 @@ fun MineAgent() {
modifier = Modifier.size(140.dp)
)
Spacer(modifier = Modifier.size(32.dp))
androidx.compose.material.Text(
text = "You haven't followed anyone yet",
Text(
text = "您还没有创建任何智能体",
color = AppColors.text,
fontSize = 16.sp,
fontWeight = FontWeight.W600
)
Spacer(modifier = Modifier.size(16.dp))
androidx.compose.material.Text(
text = "Click start your social journey.",
Text(
text = "点击开始创建您的第一个智能体",
color = AppColors.text,
fontSize = 16.sp,
fontWeight = FontWeight.W400
)
}
}
}else{
} else {
Box(Modifier.pullRefresh(state)) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
@@ -106,14 +107,43 @@ fun MineAgent() {
agentList.size,
key = { idx -> idx }
) { idx ->
val agentItem = agentList[idx] ?: return@items
AgentCard(agentEntity = agentItem,
)
val agentItem = agentList[idx]
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))
}
}
// 错误信息显示
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.mutableStateOf
import androidx.compose.runtime.setValue
import com.aiosman.ravenow.entity.AgentLoaderExtraArgs
import com.aiosman.ravenow.entity.MomentLoaderExtraArgs
import com.aiosman.ravenow.ui.index.tabs.ai.BaseAgentModel
import com.aiosman.ravenow.ui.index.tabs.moment.BaseMomentModel
import org.greenrobot.eventbus.EventBus
object MineAgentViewModel : BaseAgentModel() {
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aiosman.ravenow.data.api.ApiClient
import com.aiosman.ravenow.data.ServiceException
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)
private val pageSize = 20
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
}
}
}
}