资源清理管理
This commit is contained in:
78
app/src/main/java/com/aiosman/ravenow/utils/DebounceUtils.kt
Normal file
78
app/src/main/java/com/aiosman/ravenow/utils/DebounceUtils.kt
Normal file
@@ -0,0 +1,78 @@
|
||||
package com.aiosman.ravenow.utils
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
/**
|
||||
* 防抖工具类
|
||||
* 用于防止用户快速重复点击
|
||||
*/
|
||||
object DebounceUtils {
|
||||
|
||||
/**
|
||||
* 防抖点击处理
|
||||
* @param scope 协程作用域
|
||||
* @param delayMillis 防抖延迟时间(毫秒),默认500ms
|
||||
* @param action 要执行的操作
|
||||
*/
|
||||
fun debounceClick(
|
||||
scope: CoroutineScope,
|
||||
delayMillis: Long = 500L,
|
||||
action: () -> Unit
|
||||
) {
|
||||
scope.launch {
|
||||
delay(delayMillis)
|
||||
action()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 带状态检查的防抖点击处理
|
||||
* @param scope 协程作用域
|
||||
* @param delayMillis 防抖延迟时间(毫秒),默认500ms
|
||||
* @param isProcessing 是否正在处理中的状态
|
||||
* @param action 要执行的操作
|
||||
*/
|
||||
fun debounceClickWithState(
|
||||
scope: CoroutineScope,
|
||||
delayMillis: Long = 500L,
|
||||
isProcessing: AtomicBoolean,
|
||||
action: () -> Unit
|
||||
) {
|
||||
if (isProcessing.get()) {
|
||||
return
|
||||
}
|
||||
|
||||
isProcessing.set(true)
|
||||
scope.launch {
|
||||
delay(delayMillis)
|
||||
try {
|
||||
action()
|
||||
} finally {
|
||||
isProcessing.set(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的防抖点击处理(无协程)
|
||||
* @param lastClickTime 上次点击时间
|
||||
* @param delayMillis 防抖延迟时间(毫秒),默认500ms
|
||||
* @param action 要执行的操作
|
||||
* @return 是否执行了操作
|
||||
*/
|
||||
fun simpleDebounceClick(
|
||||
lastClickTime: Long,
|
||||
delayMillis: Long = 500L,
|
||||
action: () -> Unit
|
||||
): Boolean {
|
||||
val currentTime = System.currentTimeMillis()
|
||||
if (currentTime - lastClickTime < delayMillis) {
|
||||
return false
|
||||
}
|
||||
action()
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
package com.aiosman.ravenow.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.aiosman.ravenow.AppState
|
||||
import com.aiosman.ravenow.ui.index.IndexViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.ai.AgentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.ai.tabs.hot.HotAgentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.ai.tabs.mine.MineAgentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.dynamic.DynamicViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.hot.HotMomentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.timeline.TimelineMomentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.search.DiscoverViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.search.SearchViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.message.MessageListViewModel
|
||||
import com.aiosman.ravenow.ui.like.LikeNoticeViewModel
|
||||
import com.aiosman.ravenow.ui.favourite.FavouriteListViewModel
|
||||
import com.aiosman.ravenow.ui.favourite.FavouriteNoticeViewModel
|
||||
import com.aiosman.ravenow.ui.follower.FollowerNoticeViewModel
|
||||
import com.aiosman.ravenow.ui.post.NewPostViewModel
|
||||
import com.aiosman.ravenow.ui.imageviewer.ImageViewerViewModel
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
|
||||
/**
|
||||
* 资源清理管理器
|
||||
* 用于在Index页面退出时清理所有相关资源
|
||||
*/
|
||||
object ResourceCleanupManager {
|
||||
|
||||
/**
|
||||
* 清理所有Index页面相关的资源
|
||||
* @param context 上下文
|
||||
*/
|
||||
fun cleanupAllResources(context: Context) {
|
||||
try {
|
||||
// 1. 清理ViewModel资源
|
||||
cleanupViewModels()
|
||||
|
||||
// 2. 清理EventBus注册
|
||||
cleanupEventBus()
|
||||
|
||||
// 3. 清理服务
|
||||
cleanupServices(context)
|
||||
|
||||
// 4. 清理缓存和临时数据
|
||||
cleanupCacheAndTempData()
|
||||
|
||||
// 5. 重置应用状态
|
||||
resetAppState()
|
||||
|
||||
} catch (e: Exception) {
|
||||
// 记录错误但不抛出异常,确保清理过程不会中断
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有ViewModel资源
|
||||
*/
|
||||
private fun cleanupViewModels() {
|
||||
// 重置Index相关ViewModel
|
||||
IndexViewModel.ResetModel()
|
||||
|
||||
// 重置AI相关ViewModel
|
||||
// AgentViewModel的属性是私有的,无法直接访问,通过其他方式清理
|
||||
|
||||
HotAgentViewModel.let {
|
||||
it.agentList = emptyList()
|
||||
it.refreshing = false
|
||||
it.isLoading = false
|
||||
it.hasNext = true
|
||||
it.currentPage = 1
|
||||
it.error = null
|
||||
it.clearPreloadedImages()
|
||||
}
|
||||
|
||||
MineAgentViewModel.let {
|
||||
it.refreshing = false
|
||||
it.isLoading = false
|
||||
it.hasNext = true
|
||||
it.currentPage = 1
|
||||
it.error = null
|
||||
}
|
||||
|
||||
// 重置动态相关ViewModel
|
||||
TimelineMomentViewModel.ResetModel()
|
||||
DynamicViewModel.ResetModel()
|
||||
HotMomentViewModel.ResetModel()
|
||||
|
||||
// 重置个人资料相关ViewModel
|
||||
MyProfileViewModel.ResetModel()
|
||||
|
||||
// 重置搜索相关ViewModel
|
||||
// DiscoverViewModel的属性是私有的,无法直接访问,通过其他方式清理
|
||||
|
||||
SearchViewModel.ResetModel()
|
||||
|
||||
// 重置消息相关ViewModel
|
||||
MessageListViewModel.ResetModel()
|
||||
|
||||
// 重置通知相关ViewModel
|
||||
LikeNoticeViewModel.ResetModel()
|
||||
FavouriteNoticeViewModel.ResetModel()
|
||||
FollowerNoticeViewModel.ResetModel()
|
||||
|
||||
// 重置收藏相关ViewModel
|
||||
FavouriteListViewModel.ResetModel()
|
||||
|
||||
// 重置发布相关ViewModel
|
||||
NewPostViewModel.asNewPost()
|
||||
|
||||
// 重置图片查看器ViewModel
|
||||
ImageViewerViewModel.let {
|
||||
it.imageList.clear()
|
||||
it.initialIndex = 0
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理EventBus注册
|
||||
*/
|
||||
private fun cleanupEventBus() {
|
||||
try {
|
||||
// 取消所有ViewModel的EventBus注册
|
||||
val eventBus = EventBus.getDefault()
|
||||
|
||||
// 检查并取消注册各种ViewModel
|
||||
if (eventBus.isRegistered(TimelineMomentViewModel)) {
|
||||
eventBus.unregister(TimelineMomentViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(DynamicViewModel)) {
|
||||
eventBus.unregister(DynamicViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(HotMomentViewModel)) {
|
||||
eventBus.unregister(HotMomentViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(MyProfileViewModel)) {
|
||||
eventBus.unregister(MyProfileViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(DiscoverViewModel)) {
|
||||
eventBus.unregister(DiscoverViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(SearchViewModel)) {
|
||||
eventBus.unregister(SearchViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(MessageListViewModel)) {
|
||||
eventBus.unregister(MessageListViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(LikeNoticeViewModel)) {
|
||||
eventBus.unregister(LikeNoticeViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(FavouriteListViewModel)) {
|
||||
eventBus.unregister(FavouriteListViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(FavouriteNoticeViewModel)) {
|
||||
eventBus.unregister(FavouriteNoticeViewModel)
|
||||
}
|
||||
if (eventBus.isRegistered(FollowerNoticeViewModel)) {
|
||||
eventBus.unregister(FollowerNoticeViewModel)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// EventBus清理失败不影响其他清理
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理服务
|
||||
*/
|
||||
private fun cleanupServices(context: Context) {
|
||||
try {
|
||||
// 停止TRTC服务
|
||||
// val trtcService = Intent(context, TrtcService::class.java)
|
||||
// context.stopService(trtcService)
|
||||
} catch (e: Exception) {
|
||||
// 服务停止失败不影响其他清理
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理缓存和临时数据
|
||||
*/
|
||||
private fun cleanupCacheAndTempData() {
|
||||
try {
|
||||
// 清理图片缓存
|
||||
// 这里可以添加图片缓存清理逻辑
|
||||
|
||||
// 清理临时文件
|
||||
// 这里可以添加临时文件清理逻辑
|
||||
|
||||
} catch (e: Exception) {
|
||||
// 缓存清理失败不影响其他清理
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置应用状态
|
||||
*/
|
||||
private fun resetAppState() {
|
||||
try {
|
||||
// 重置用户ID
|
||||
AppState.UserId = null
|
||||
|
||||
// 重置其他应用状态
|
||||
// 这里可以添加其他应用状态的重置逻辑
|
||||
|
||||
} catch (e: Exception) {
|
||||
// 状态重置失败不影响其他清理
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理特定页面的资源
|
||||
* @param pageType 页面类型
|
||||
*/
|
||||
fun cleanupPageResources(pageType: String) {
|
||||
when (pageType) {
|
||||
"ai" -> {
|
||||
// AgentViewModel的属性是私有的,无法直接访问
|
||||
HotAgentViewModel.let {
|
||||
it.agentList = emptyList()
|
||||
it.refreshing = false
|
||||
it.isLoading = false
|
||||
it.hasNext = true
|
||||
it.currentPage = 1
|
||||
it.error = null
|
||||
it.clearPreloadedImages()
|
||||
}
|
||||
MineAgentViewModel.let {
|
||||
it.refreshing = false
|
||||
it.isLoading = false
|
||||
it.hasNext = true
|
||||
it.currentPage = 1
|
||||
it.error = null
|
||||
}
|
||||
}
|
||||
"moment" -> {
|
||||
TimelineMomentViewModel.ResetModel()
|
||||
DynamicViewModel.ResetModel()
|
||||
HotMomentViewModel.ResetModel()
|
||||
}
|
||||
"profile" -> {
|
||||
MyProfileViewModel.ResetModel()
|
||||
}
|
||||
"search" -> {
|
||||
// DiscoverViewModel的属性是私有的,无法直接访问
|
||||
SearchViewModel.ResetModel()
|
||||
}
|
||||
"message" -> {
|
||||
MessageListViewModel.ResetModel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user