资源清理管理

This commit is contained in:
weber
2025-08-27 18:32:51 +08:00
parent 2a7d310be5
commit fdf8c1fa5a
10 changed files with 466 additions and 35 deletions

View File

@@ -97,7 +97,7 @@ fun AccountEditScreen2() {
) {
//StatusBarSpacer()
Box(
modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)
modifier = Modifier.padding(horizontal = 0.dp, vertical = 16.dp)
) {
NoticeScreenHeader(
title = stringResource(R.string.edit_profile),

View File

@@ -37,6 +37,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
@@ -73,6 +74,7 @@ import com.aiosman.ravenow.ui.index.tabs.shorts.ShortVideo
import com.aiosman.ravenow.ui.index.tabs.street.StreetPage
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import com.aiosman.ravenow.ui.post.NewPostViewModel
import com.aiosman.ravenow.utils.ResourceCleanupManager
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import kotlinx.coroutines.launch
@@ -97,6 +99,13 @@ fun IndexScreen() {
val coroutineScope = rememberCoroutineScope()
val drawerState = rememberDrawerState(DrawerValue.Closed)
val context = LocalContext.current
// 页面退出时清理所有资源
DisposableEffect(Unit) {
onDispose {
ResourceCleanupManager.cleanupAllResources(context)
}
}
LaunchedEffect(Unit) {
systemUiController.setNavigationBarColor(Color.Transparent)
}
@@ -363,9 +372,19 @@ fun IndexScreen() {
@Composable
fun Home() {
val systemUiController = rememberSystemUiController()
val context = LocalContext.current
LaunchedEffect(AppState.darkMode) {
systemUiController.setStatusBarColor(Color.Transparent, darkIcons = !AppState.darkMode)
}
// 页面退出时清理动态相关资源
DisposableEffect(Unit) {
onDispose {
ResourceCleanupManager.cleanupPageResources("moment")
}
}
Column(
modifier = Modifier
.fillMaxSize(),
@@ -380,9 +399,19 @@ fun Home() {
@Composable
fun Street() {
val systemUiController = rememberSystemUiController()
val context = LocalContext.current
LaunchedEffect(AppState.darkMode) {
systemUiController.setStatusBarColor(Color.Transparent, darkIcons = !AppState.darkMode)
}
// 页面退出时清理搜索相关资源
DisposableEffect(Unit) {
onDispose {
ResourceCleanupManager.cleanupPageResources("search")
}
}
Column(
modifier = Modifier
.fillMaxSize(),
@@ -430,9 +459,19 @@ fun Video() {
@Composable
fun Profile() {
val systemUiController = rememberSystemUiController()
val context = LocalContext.current
LaunchedEffect(AppState.darkMode) {
systemUiController.setStatusBarColor(Color.Transparent, !AppState.darkMode)
}
// 页面退出时清理个人资料相关资源
DisposableEffect(Unit) {
onDispose {
ResourceCleanupManager.cleanupPageResources("profile")
}
}
Column(
modifier = Modifier
.fillMaxSize(),
@@ -446,9 +485,19 @@ fun Profile() {
@Composable
fun Notifications() {
val systemUiController = rememberSystemUiController()
val context = LocalContext.current
LaunchedEffect(AppState.darkMode) {
systemUiController.setStatusBarColor(Color.Transparent, !AppState.darkMode)
}
// 页面退出时清理消息相关资源
DisposableEffect(Unit) {
onDispose {
ResourceCleanupManager.cleanupPageResources("message")
}
}
Column(
modifier = Modifier
.fillMaxSize(),

View File

@@ -28,7 +28,12 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -53,6 +58,8 @@ import com.aiosman.ravenow.ui.composables.TabItem
import com.aiosman.ravenow.ui.composables.TabSpacer
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.expolre.AgentItem
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.expolre.ExploreViewModel
import com.aiosman.ravenow.utils.DebounceUtils
import com.aiosman.ravenow.utils.ResourceCleanupManager
import kotlinx.coroutines.launch
@OptIn( ExperimentalFoundationApi::class)
@@ -67,6 +74,16 @@ fun Agent() {
var scope = rememberCoroutineScope()
val viewModel: AgentViewModel = viewModel()
// 防抖状态
var lastClickTime by remember { mutableStateOf(0L) }
// 页面退出时清理AI相关资源
DisposableEffect(Unit) {
onDispose {
ResourceCleanupManager.cleanupPageResources("ai")
}
}
Column(
modifier = Modifier
@@ -119,11 +136,15 @@ fun Agent() {
modifier = Modifier
.size(36.dp)
.noRippleClickable {
// 设置标志,表示新增智能体后不需要刷新
com.aiosman.ravenow.ui.agent.AddAgentViewModel.isFromAddAgent = true
navController.navigate(
NavigationRoute.AddAgent.route
)
if (DebounceUtils.simpleDebounceClick(lastClickTime, 500L) {
// 设置标志,表示新增智能体后不需要刷新
com.aiosman.ravenow.ui.agent.AddAgentViewModel.isFromAddAgent = true
navController.navigate(
NavigationRoute.AddAgent.route
)
}) {
lastClickTime = System.currentTimeMillis()
}
},
painter = painterResource(id = R.drawable.rider_pro_new_post_add_pic),
contentDescription = null,
@@ -189,8 +210,12 @@ fun Agent() {
text = stringResource(R.string.agent_mine),
isSelected = pagerState.currentPage == 0,
onClick = {
scope.launch {
pagerState.animateScrollToPage(0)
if (DebounceUtils.simpleDebounceClick(lastClickTime, 300L) {
scope.launch {
pagerState.animateScrollToPage(0)
}
}) {
lastClickTime = System.currentTimeMillis()
}
}
)
@@ -199,8 +224,12 @@ fun Agent() {
text = stringResource(R.string.agent_hot),
isSelected = pagerState.currentPage == 1,
onClick = {
scope.launch {
pagerState.animateScrollToPage(1)
if (DebounceUtils.simpleDebounceClick(lastClickTime, 300L) {
scope.launch {
pagerState.animateScrollToPage(1)
}
}) {
lastClickTime = System.currentTimeMillis()
}
}
)
@@ -347,6 +376,9 @@ fun AgentPage(viewModel: AgentViewModel,agentItems: List<AgentItem>, page: Int,
@Composable
fun AgentCard2(viewModel: AgentViewModel,agentItem: AgentItem,navController: NavHostController) {
val AppColors = LocalAppTheme.current
// 防抖状态
var lastClickTime by remember { mutableStateOf(0L) }
Row(
modifier = Modifier
@@ -419,8 +451,12 @@ fun AgentCard2(viewModel: AgentViewModel,agentItem: AgentItem,navController: Nav
shape = RoundedCornerShape(8.dp)
)
.clickable {
/* viewModel.createSingleChat(agentItem.trtcId)
viewModel.goToChatAi(agentItem.trtcId, navController = navController)*/
if (DebounceUtils.simpleDebounceClick(lastClickTime, 500L) {
/* viewModel.createSingleChat(agentItem.trtcId)
viewModel.goToChatAi(agentItem.trtcId, navController = navController)*/
}) {
lastClickTime = System.currentTimeMillis()
}
},
contentAlignment = Alignment.Center
) {

View File

@@ -60,8 +60,9 @@ fun HotAgent() {
}
}
// 只在首次加载时刷新避免从AddAgent返回时重复刷新
// 只在初始化页面时刷新
LaunchedEffect(Unit) {
// 只有在列表完全为空且没有正在加载时才进行初始化刷新
if (model.agentList.isEmpty() && !model.isLoading) {
model.refreshPager()
}

View File

@@ -53,8 +53,9 @@ fun MineAgent() {
// Paging 库会自动处理加载更多,无需手动监听滚动
// 只在首次加载时刷新避免从AddAgent返回时重复刷新
// 只在初始化页面时刷新
LaunchedEffect(Unit) {
// 只有在列表完全为空且没有正在加载时才进行初始化刷新
if (agentList.itemCount == 0 && !model.isLoading) {
model.refreshPager()
}

View File

@@ -25,7 +25,6 @@ import com.aiosman.ravenow.entity.MomentPagingSource
import com.aiosman.ravenow.entity.MomentRemoteDataSource
import com.aiosman.ravenow.entity.MomentServiceImpl
import com.aiosman.ravenow.ui.index.tabs.message.MessageListViewModel.userService
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.hot.HotMomentViewModel.firstLoad
import com.aiosman.ravenow.ui.navigateToChatAi
import com.tencent.imsdk.v2.V2TIMConversationOperationResult
import com.tencent.imsdk.v2.V2TIMManager
@@ -47,6 +46,7 @@ object MineAgentViewModel : ViewModel() {
var hasNext by mutableStateOf(true)
var currentPage by mutableStateOf(1)
var error by mutableStateOf<String?>(null)
var isFirstLoad by mutableStateOf(true)
// 记录已预加载的图片ID避免重复加载
private val preloadedImageIds = mutableSetOf<Int>()
@@ -55,10 +55,10 @@ object MineAgentViewModel : ViewModel() {
fun refreshPager() {
if (!firstLoad) {
if (!isFirstLoad) {
return
}
firstLoad = false
isFirstLoad = false
viewModelScope.launch {
Pager(
config = PagingConfig(pageSize = 5, enablePlaceholders = false),

View File

@@ -29,6 +29,7 @@ import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
@@ -179,30 +180,36 @@ fun NotificationsScreen() {
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
.height(40.dp)
.background(
color = Color(0xFFF5F5F5),
shape = RoundedCornerShape(8.dp)
)
.noRippleClickable {//添加搜索逻辑实现
.noRippleClickable {
},
contentAlignment = Alignment.CenterStart
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(start = 16.dp)
modifier = Modifier
.height(36.dp)
.fillMaxWidth()
.clip(shape = RoundedCornerShape(8.dp))
.background(AppColors.inputBackground)
.padding(horizontal = 8.dp, vertical = 0.dp)
.noRippleClickable {
// 搜索框点击事件
},
verticalAlignment = Alignment.CenterVertically
) {
Image(
painter = painterResource(id = R.drawable.rider_pro_search_location),
contentDescription = "search",
modifier = Modifier.size(20.dp),
colorFilter = ColorFilter.tint(Color(0xFF999999))
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = stringResource(R.string.search),
fontSize = 14.sp,
color = Color(0xFF999999)
Icon(
painter = painterResource(id = R.drawable.rider_pro_nav_search),
contentDescription = null,
tint = AppColors.inputHint
)
Box {
androidx.compose.material.Text(
text = stringResource(R.string.search),
modifier = Modifier.padding(start = 8.dp),
color = AppColors.inputHint,
fontSize = 17.sp
)
}
}
}

View File

@@ -410,6 +410,7 @@ fun NewPostTextField(hint: String, value: String, onValueChange: (String) -> Uni
value = value,
onValueChange = onValueChange,
modifier = Modifier
.fillMaxWidth()
.height(160.dp)
.heightIn(160.dp)
.padding(horizontal = 16.dp, vertical = 10.dp),