1、代码更新
This commit is contained in:
shenzuqiang 2026-03-12 10:54:28 +08:00
parent 4883f4cba0
commit 8b6ffa9b7a
6 changed files with 44 additions and 32 deletions

View File

@ -73,6 +73,8 @@ import com.img.rabbit.utils.AppEventBus
import com.img.rabbit.utils.AppUpdate
import com.img.rabbit.utils.ChannelUtils
import com.img.rabbit.utils.FileUtils
import com.img.rabbit.utils.GlobalEvent
import com.img.rabbit.utils.GlobalEventBus
import com.img.rabbit.utils.LoginBindEvent
import com.img.rabbit.utils.UniAppUtils
import com.img.rabbit.utils.UniMpUpdate
@ -102,13 +104,14 @@ class MainActivity : ComponentActivity(), LoadingCallback {
setContent {
val coroutineScope = rememberCoroutineScope()
var showSplash by remember { mutableStateOf(false) }
val isGlobalLoading by generalViewModel.isLoading.collectAsState()
val context = LocalContext.current
val loginViewModel: LoginViewModel = viewModel()
val splashViewModel: SplashViewModel = viewModel()
val reportViewModel:ReportViewModel = viewModel()
generalViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application))[GeneralViewModel::class.java]
// isGlobalLoading请勿前置会导致初始化问题
val isGlobalLoading by generalViewModel.isLoading.collectAsState()
// 软件更新提示
var showUpdateDialog by remember { mutableStateOf(false) }
@ -120,19 +123,19 @@ class MainActivity : ComponentActivity(), LoadingCallback {
var updateUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) }
//以下为流事件收集订阅
LaunchedEffect(Unit) {
GeneralViewModel.GlobalEventBus.events.collect { event ->
GlobalEventBus.events.collect { event ->
when (event) {
// 软件更新提示
is GeneralViewModel.GlobalEvent.ShowAppUpdateNotify -> {
is GlobalEvent.ShowAppUpdateNotify -> {
showUpdateDialog = true
}
// 小程序资源下载提示
is GeneralViewModel.GlobalEvent.ShowUniDownloadNotify -> {
is GlobalEvent.ShowUniDownloadNotify -> {
downloadUniEntity = event.entity
showUniDownloadDialog = true
}
// 小程序资源更新提示
is GeneralViewModel.GlobalEvent.ShowUniUpdateNotify -> {
is GlobalEvent.ShowUniUpdateNotify -> {
updateUniEntity = event.entity
showUniUpdateDialog = true
}

View File

@ -56,6 +56,8 @@ import com.img.rabbit.components.CenterToast
import com.img.rabbit.pages.LoadingCallback
import com.img.rabbit.provider.storage.PreferenceUtil
import com.img.rabbit.route.ScreenRoute
import com.img.rabbit.utils.GlobalEvent
import com.img.rabbit.utils.GlobalEventBus
import com.img.rabbit.utils.UniAppUtils
import com.img.rabbit.utils.UniMpUpdate
import com.img.rabbit.viewmodel.GeneralViewModel
@ -251,8 +253,8 @@ fun HomeScreen(
// 提示更新1、更新释放新版本并启动2、直接启动现有版本
UniAppUtils.currentUpdateUniMp = uniMp
scope.launch {
GeneralViewModel.GlobalEventBus.emit(
GeneralViewModel.GlobalEvent.ShowUniUpdateNotify(uniMp))
GlobalEventBus.emit(
GlobalEvent.ShowUniUpdateNotify(uniMp))
}
} else {
loadingCallback?.showLoading()
@ -380,8 +382,8 @@ fun HomeScreen(
// 提示更新1、更新释放新版本并启动2、直接启动现有版本
UniAppUtils.currentUpdateUniMp = uniMp
scope.launch {
GeneralViewModel.GlobalEventBus.emit(
GeneralViewModel.GlobalEvent.ShowUniUpdateNotify(uniMp))
GlobalEventBus.emit(
GlobalEvent.ShowUniUpdateNotify(uniMp))
}
} else {
loadingCallback?.showLoading()
@ -478,8 +480,8 @@ fun HomeScreen(
if(!wgtExists){
//是否下载
scope.launch {
GeneralViewModel.GlobalEventBus.emit(
GeneralViewModel.GlobalEvent.ShowUniDownloadNotify(uniVersion))
GlobalEventBus.emit(
GlobalEvent.ShowUniDownloadNotify(uniVersion))
}
}else {
UniAppUtils.startUniMpPage(context = context, uniMpId = uniMpId, uniMpType = item.type, pagePath = item.url, reportViewModel = reportViewModel)

View File

@ -51,6 +51,8 @@ import com.img.rabbit.components.CenterToast
import com.img.rabbit.provider.storage.PreferenceUtil
import com.img.rabbit.route.ScreenRoute
import com.img.rabbit.utils.AppUpdate
import com.img.rabbit.utils.GlobalEvent
import com.img.rabbit.utils.GlobalEventBus
import com.img.rabbit.viewmodel.GeneralViewModel
import com.img.rabbit.viewmodel.LoginViewModel
import com.img.rabbit.viewmodel.MineViewModel
@ -449,8 +451,8 @@ fun MineScreen(
//提示执行更新
//startUpdate(result, fragment)
scope.launch {
GeneralViewModel.GlobalEventBus.emit(
GeneralViewModel.GlobalEvent.ShowAppUpdateNotify)
GlobalEventBus.emit(
GlobalEvent.ShowAppUpdateNotify)
}
} else {
CenterToast.show(tips)

View File

@ -1,5 +1,6 @@
package com.img.rabbit.utils
import com.img.rabbit.bean.response.UniVersionEntity
import com.img.rabbit.pages.LoginScreenType
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
@ -60,3 +61,22 @@ sealed class WXAuthEvent {
sealed class UniMpWXPayEvent {
data class PayResult(val code: Int) : UniMpWXPayEvent()
}
// 可以定义一个事件密封类,方便扩展
sealed class GlobalEvent {
object ShowAppUpdateNotify : GlobalEvent()
data class ShowUniDownloadNotify(val entity: UniVersionEntity) : GlobalEvent()
data class ShowUniUpdateNotify(val entity: UniVersionEntity) : GlobalEvent()
}
object GlobalEventBus {
// replay = 0 确保事件是瞬时的,不会给新订阅者发送旧事件
private val _events = MutableSharedFlow<GlobalEvent>(extraBufferCapacity = 64)
val events = _events.asSharedFlow()
suspend fun emit(event: GlobalEvent) {
_events.emit(event)
}
}

View File

@ -41,23 +41,6 @@ class GeneralViewModel: BaseViewModel(){
private val _isNavigationBarVisible = MutableLiveData<Boolean>()
val isNavigationBarVisible: LiveData<Boolean> = _isNavigationBarVisible
// 可以定义一个事件密封类,方便扩展
sealed class GlobalEvent {
object ShowAppUpdateNotify : GlobalEvent()
data class ShowUniDownloadNotify(val entity: UniVersionEntity) : GlobalEvent()
data class ShowUniUpdateNotify(val entity: UniVersionEntity) : GlobalEvent()
}
object GlobalEventBus {
// replay = 0 确保事件是瞬时的,不会给新订阅者发送旧事件
private val _events = MutableSharedFlow<GlobalEvent>(extraBufferCapacity = 64)
val events = _events.asSharedFlow()
suspend fun emit(event: GlobalEvent) {
_events.emit(event)
}
}
private val connectivityManager =
HeadParamUtils.applicationContext?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

View File

@ -29,6 +29,8 @@ import com.img.rabbit.provider.storage.PreferenceUtil
import com.img.rabbit.provider.utils.HeadParamUtils.applicationContext
import com.img.rabbit.utils.AppEventBus
import com.img.rabbit.utils.AppUpdate
import com.img.rabbit.utils.GlobalEvent
import com.img.rabbit.utils.GlobalEventBus
import com.img.rabbit.utils.LoginBindEvent
import com.img.rabbit.utils.MMKVUtils
import com.img.rabbit.utils.StringUtils
@ -114,8 +116,8 @@ class LoginViewModel : BaseViewModel() {
// 有更新,提示更新
viewModelScope.launch{
versionEntity?.let {
GeneralViewModel.GlobalEventBus.emit(
GeneralViewModel.GlobalEvent.ShowAppUpdateNotify)
GlobalEventBus.emit(
GlobalEvent.ShowAppUpdateNotify)
}
}
}