parent
8877763a29
commit
14853a281f
|
|
@ -3,6 +3,7 @@ package com.img.rabbit
|
|||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
|
|
@ -16,6 +17,7 @@ import androidx.compose.runtime.LaunchedEffect
|
|||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableFloatStateOf
|
||||
import androidx.compose.runtime.mutableLongStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
|
|
@ -30,6 +32,7 @@ import androidx.lifecycle.lifecycleScope
|
|||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.img.rabbit.bean.response.UniVersionEntity
|
||||
import com.img.rabbit.components.CenterToast
|
||||
import com.img.rabbit.components.GlobalToast
|
||||
import com.img.rabbit.pages.LoadingCallback
|
||||
import com.img.rabbit.pages.LoginScreen
|
||||
|
|
@ -81,6 +84,8 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
val reportViewModel:ReportViewModel = viewModel()
|
||||
generalViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application))[GeneralViewModel::class.java]
|
||||
|
||||
var lastClickTime by remember { mutableLongStateOf(0L) }
|
||||
|
||||
// UniMp小程序资源下载更新实体
|
||||
var downloadUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) }
|
||||
var updateUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) }
|
||||
|
|
@ -107,11 +112,17 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
|
||||
// 是否加载splashScreen完成
|
||||
val isSplashDone = remember { mutableStateOf(false) }
|
||||
// 是否启动小程序完成
|
||||
val isUniMpDone = remember { mutableStateOf(false) }
|
||||
// 加载UniMp小程序
|
||||
var isLoadingUniMp by remember { mutableStateOf(false) }
|
||||
|
||||
// 是否退出登录
|
||||
var isLogout by remember { mutableStateOf(false) }
|
||||
// 加载显示MainScreen
|
||||
var showMainScreen by remember { mutableStateOf(false) }
|
||||
// 延迟加载MainScreen通知
|
||||
var delayLoadingMainScreen by remember { mutableStateOf(false) }
|
||||
|
||||
// 设置启动页显示条件
|
||||
splashScreen.setKeepOnScreenCondition {
|
||||
|
|
@ -149,6 +160,7 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
showLoginDialog = true
|
||||
}
|
||||
// ... 可以处理其他事件
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -175,9 +187,11 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
}
|
||||
}
|
||||
|
||||
// UniMp事件监听
|
||||
LaunchedEffect(Unit) {
|
||||
DCUniMPSDK.getInstance().setOnUniMPEventCallBack { appid, event, data, callback ->//appid, event, data, callback ->
|
||||
if(event == "start_combo_pay"){
|
||||
Log.i("MainScreen", "onUniMPEventCallBack: $event")
|
||||
if("start_combo_pay" == event){
|
||||
//拉起微信小程序来支付
|
||||
val weixinMpOriId = JSONObject(data.toString()).optString("weixinMpOriId")
|
||||
val outTradeNo = JSONObject(data.toString()).optString("outTradeNo")
|
||||
|
|
@ -186,6 +200,22 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
weixinMpOriId = weixinMpOriId,
|
||||
outTradeNo = outTradeNo
|
||||
)
|
||||
}else if("unimp_BackPress_goMe" == event){
|
||||
//这里处理切换到我的页面
|
||||
coroutineScope.launch {
|
||||
GlobalEventBus.emit(
|
||||
GlobalEvent.JumpMineNotify)
|
||||
}
|
||||
UniAppUtils.getCurrentUniMp()?.closeUniMP()
|
||||
}else if("unimp_BackPress_backbutton" == event){
|
||||
//这里需要处理手势、虚拟返回
|
||||
val currentTime = System.currentTimeMillis()
|
||||
if (currentTime - lastClickTime > 2000) {
|
||||
CenterToast.show("再按一次退出应用")
|
||||
lastClickTime = currentTime
|
||||
} else {
|
||||
(context as? Activity)?.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -237,6 +267,7 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
when (result) {
|
||||
-1 -> {
|
||||
//下载失败
|
||||
isUniMpDone.value = false
|
||||
isSplashDone.value = true
|
||||
}
|
||||
1 -> {
|
||||
|
|
@ -247,17 +278,22 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
//下载完成(关闭下载框)
|
||||
showUniDownloadDialog = false
|
||||
}
|
||||
3 -> {
|
||||
//启动成功
|
||||
isUniMpDone.value = true
|
||||
isSplashDone.value = true
|
||||
}
|
||||
4 -> {
|
||||
//启动失败
|
||||
isUniMpDone.value = false
|
||||
isSplashDone.value = true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//启动小程序(资源存在并且最新则直接启动,否则执行下载后启动)
|
||||
LaunchedEffect(isLoadingUniMp) {
|
||||
if(isLoadingUniMp){
|
||||
downloadUniEntity?.let { uniMp ->
|
||||
|
|
@ -266,17 +302,45 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
showUniDownloadDialog = true
|
||||
} else {
|
||||
// 执行分发逻辑(异步操作)
|
||||
distributeUniMp(context, uniMp, reportViewModel) {}
|
||||
distributeUniMp(context, uniMp, reportViewModel) {
|
||||
isUniMpDone.value = it
|
||||
isSplashDone.value = true
|
||||
}
|
||||
}
|
||||
}
|
||||
isLoadingUniMp = false
|
||||
}
|
||||
}
|
||||
//启动MainScreen(如果启动小程序则延迟2秒启动,否则直接启动主界面)
|
||||
LaunchedEffect(delayLoadingMainScreen) {
|
||||
if (delayLoadingMainScreen) {
|
||||
delay(2000) // 延迟 2 秒
|
||||
showMainScreen = true
|
||||
}
|
||||
}
|
||||
|
||||
// 页面显示逻辑
|
||||
AppTheme {
|
||||
//显示启动页
|
||||
SplashScreenContent(splashDone = isSplashDone, generalViewModel = generalViewModel){
|
||||
if(isLogout){// 退出登录后,显示登录页
|
||||
if(isUniMpDone.value){
|
||||
//延迟加载MainScreen
|
||||
delayLoadingMainScreen = true
|
||||
}else{
|
||||
showMainScreen = true
|
||||
}
|
||||
}
|
||||
|
||||
// 显示主界面,因为UniMp小程序启动时,要实现SplashScreen图片与小程序的UniMPSplashView无缝切换,所以延迟2秒
|
||||
if (showMainScreen) {
|
||||
MainScreen(
|
||||
generalViewModel = generalViewModel,
|
||||
loginViewModel = loginViewModel,
|
||||
isUniMpDone = isUniMpDone.value
|
||||
)
|
||||
}
|
||||
// 退出登录后,显示登录页
|
||||
if(isLogout){
|
||||
// 同意隐私协议政策,检验是否有一键登录权限
|
||||
loginViewModel.oneKeyLoginForGeTuiSdk(context as Activity) { isAllowShowOneKeyScreen ->
|
||||
if (isAllowShowOneKeyScreen) {
|
||||
|
|
@ -288,10 +352,6 @@ class MainActivity : ComponentActivity(), LoadingCallback {
|
|||
}
|
||||
// 显示登录页
|
||||
LoginScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel, isVisibilityBreak = false)
|
||||
}else if(isSplashDone.value){
|
||||
// 显示主界面
|
||||
MainScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel)
|
||||
}
|
||||
}
|
||||
|
||||
//提示登录
|
||||
|
|
|
|||
|
|
@ -19,5 +19,6 @@ object ReportType {
|
|||
|
||||
object ReportKey {
|
||||
const val EVENT_CLIENT_UNI_RELEASE_WGT: String = "client.uni.release.wgt" //释放资源
|
||||
const val EVENT_CLIENT_UNI_DELETE_UNI: String = "client.uni.delete.uni" //删除资源
|
||||
const val EVENT_CLIENT_UNI_SPEC_PAGE_LOAD: String = "client.uni.page.load" //页面加载
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,6 @@ import com.img.rabbit.utils.GlobalEventBus
|
|||
import com.img.rabbit.viewmodel.BindViewModel
|
||||
import com.img.rabbit.viewmodel.GeneralViewModel
|
||||
import com.img.rabbit.viewmodel.LoginViewModel
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
|
|
@ -74,7 +73,7 @@ sealed class TabItem(val title: String, val router:String, val normalIconRes: In
|
|||
*/
|
||||
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
|
||||
@Composable
|
||||
fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewModel) {
|
||||
fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewModel, isUniMpDone: Boolean) {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val navController = rememberNavController()
|
||||
val networkStatus by generalViewModel.networkStatus.observeAsState(initial = true)
|
||||
|
|
@ -121,6 +120,18 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
|
|||
}
|
||||
}
|
||||
|
||||
// 处理全局事件,类似与EventBus订阅
|
||||
LaunchedEffect(Unit) {
|
||||
GlobalEventBus.events.collect { event ->
|
||||
when (event) {
|
||||
is GlobalEvent.JumpMineNotify -> {
|
||||
selectedTab = TabItem.Mine
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
bottomBar = {
|
||||
if (isNavigationBarVisible) {
|
||||
|
|
@ -148,7 +159,7 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
|
|||
interactionSource = remember { MutableInteractionSource() },
|
||||
indication = null
|
||||
){
|
||||
if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true){
|
||||
if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){
|
||||
//跳转小程序
|
||||
coroutineScope.launch {
|
||||
GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify)
|
||||
|
|
@ -162,7 +173,7 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
|
|||
label = { Text(item.title, color = if (selectedTab == tabItems[index]) item.selectedColor else item.normalColor) },
|
||||
selected = selectedTab == tabItems[index],
|
||||
onClick = {
|
||||
if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true){
|
||||
if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){
|
||||
//跳转小程序
|
||||
coroutineScope.launch {
|
||||
GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify)
|
||||
|
|
@ -197,9 +208,8 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
|
|||
// 导航主机
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = ScreenRoute.Home.route
|
||||
) {
|
||||
// Tab页面
|
||||
startDestination = if(PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone) ScreenRoute.Mine.route else ScreenRoute.Home.route
|
||||
) { // Tab页面
|
||||
composable(ScreenRoute.Home.route) {
|
||||
HomeScreen(
|
||||
navController = navController,
|
||||
|
|
@ -335,6 +345,9 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
|
|||
|
||||
// 根据选中的Tab切换导航路由
|
||||
LaunchedEffect(selectedTab) {
|
||||
if(selectedTab == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){
|
||||
return@LaunchedEffect
|
||||
}
|
||||
when (selectedTab) {
|
||||
TabItem.Home -> navController.navigate(ScreenRoute.Home.route) {
|
||||
popUpTo(ScreenRoute.Home.route) { inclusive = true }
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
package com.img.rabbit.pages.screen
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
|
|
@ -21,8 +23,11 @@ import com.img.rabbit.utils.ChannelUtils
|
|||
import com.img.rabbit.viewmodel.GeneralViewModel
|
||||
import com.umeng.analytics.MobclickAgent
|
||||
import com.umeng.commonsdk.UMConfigure
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
@SuppressLint("CoroutineCreationDuringComposition")
|
||||
@Composable
|
||||
fun SplashScreenContent(
|
||||
splashDone: MutableState<Boolean>,
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ sealed class GlobalEvent {
|
|||
object ShowAppUpdateNotify : GlobalEvent()
|
||||
object ShowLoginNotify : GlobalEvent()
|
||||
object StartupUniMpNotify : GlobalEvent()
|
||||
object JumpMineNotify : GlobalEvent()
|
||||
data class ShowUniDownloadNotify(val entity: UniVersionEntity) : GlobalEvent()
|
||||
data class ShowUniUpdateNotify(val entity: UniVersionEntity) : GlobalEvent()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,7 +170,7 @@ object UniAppUtils {
|
|||
}else{
|
||||
startUniMpToPage(context, uniVersion.unimp_id, uniVersion.unimp_type, null)
|
||||
}
|
||||
onResult(false)
|
||||
onResult(true)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -250,16 +250,25 @@ object UniAppUtils {
|
|||
//事件提交
|
||||
reportViewModel.requestReport(
|
||||
ReportRequest(
|
||||
ReportType.ERROR,
|
||||
ReportKey.EVENT_CLIENT_UNI_RELEASE_WGT,
|
||||
uniMpId,
|
||||
"释放资源失败"
|
||||
type = ReportType.ERROR,
|
||||
key = ReportKey.EVENT_CLIENT_UNI_RELEASE_WGT,
|
||||
value = "释放资源失败",
|
||||
extra = uniMpId
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}else{
|
||||
CenterToast.show("加载失败,请重试或联系客服!")
|
||||
//事件提交
|
||||
reportViewModel.requestReport(
|
||||
ReportRequest(
|
||||
type = ReportType.ERROR,
|
||||
key = ReportKey.EVENT_CLIENT_UNI_DELETE_UNI,
|
||||
value = "删除资源失败",
|
||||
extra = uniMpId
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue