1、添加程序更新逻辑
This commit is contained in:
shenzuqiang 2026-03-11 10:42:52 +08:00
parent 5f0a7b3a22
commit 31834b320d
5 changed files with 66 additions and 43 deletions

View File

@ -135,7 +135,7 @@ class MainActivity : ComponentActivity(), LoadingCallback {
LaunchedEffect(generalViewModel.serverTime.value) {
if (generalViewModel.serverTime.value != null){
// 获取用户配置
loginViewModel.requestUserConfig()
loginViewModel.requestUserConfig(isInitConfig = true)
initUM()
}
}
@ -328,6 +328,7 @@ class MainActivity : ComponentActivity(), LoadingCallback {
desc = PreferenceUtil.getUserConfig()?.config?.versionEntity?.description?:"",
url = PreferenceUtil.getUserConfig()?.config?.versionEntity?.url?:"",
scope = coroutineScope,
isForce = PreferenceUtil.getUserConfig()?.config?.versionEntity?.force?:false,
isStartDown = isStartDownload,
downProgress = progressState
){ state, isCancel, url ->
@ -351,8 +352,12 @@ class MainActivity : ComponentActivity(), LoadingCallback {
filePath?.let {
UpdateUtils.install(context,it)
}
coroutineScope.launch {
GlobalStateManager(context).storeGlobalUpdateNotify(state)
if(PreferenceUtil.getUserConfig()?.config?.versionEntity?.force == false) {
coroutineScope.launch {
GlobalStateManager(context).storeGlobalUpdateNotify(
state
)
}
}
}
}

View File

@ -54,6 +54,7 @@ fun UpdateDialog(
desc: String = "修复了一些问题,新增了一些功能",
url: String,
scope: CoroutineScope,
isForce: Boolean = false,
isStartDown: MutableState<Boolean>,
downProgress: MutableState<Float>,
onStatusChange: (state: Boolean, isCancel: Boolean, url:String) -> Unit
@ -170,45 +171,47 @@ fun UpdateDialog(
}else{
Box(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.weight(1f)
.background(
Color(0x00000000),
shape = RoundedCornerShape(359.dp),
)
.border(
width = 1.dp,
color = Color(0xFF000000),
shape = RoundedCornerShape(359.dp)
)
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() }
) {
onStatusChange(false, true, url)
}
) {
Text(
"暂不",
color = Color(0xFF1A1A1A),
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
if(!isForce){
Box(
modifier = Modifier
.wrapContentWidth()
.fillMaxWidth()
.wrapContentHeight()
.padding(vertical = 12.dp)
.align(Alignment.Center)
.weight(1f)
.background(
Color(0x00000000),
shape = RoundedCornerShape(359.dp),
)
.border(
width = 1.dp,
color = Color(0xFF000000),
shape = RoundedCornerShape(359.dp)
)
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() }
) {
onStatusChange(false, true, url)
}
) {
Text(
"暂不",
color = Color(0xFF1A1A1A),
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.wrapContentWidth()
.wrapContentHeight()
.padding(vertical = 12.dp)
.align(Alignment.Center)
)
}
Box(
modifier = Modifier
.width(11.dp)
)
}
Box(
modifier = Modifier
.width(11.dp)
)
Box(
modifier = Modifier
.fillMaxWidth()

View File

@ -447,7 +447,7 @@ fun MineScreen(
val versionConfig =
PreferenceUtil.getUserConfig()?.config?.versionEntity
AppUpdate.checkUpdate(versionConfig) { isUpdate, tips ->
AppUpdate.checkUpdate(versionConfig) { isUpdate, _, tips ->
if (isUpdate) {
//提示执行更新
//startUpdate(result, fragment)

View File

@ -5,10 +5,10 @@ import com.img.rabbit.BuildConfig
import com.img.rabbit.bean.response.VersionEntity
object AppUpdate {
fun checkUpdate(versionInfo: VersionEntity?, onResult: (isUpdate: Boolean, tips: String) -> Unit) {
fun checkUpdate(versionInfo: VersionEntity?, onResult: (isUpdate: Boolean, isForce: Boolean, tips: String) -> Unit) {
if(versionInfo == null){
onResult.invoke(false, "已是最新版本")
onResult.invoke(false, false, "已是最新版本")
return
}
versionInfo.apply {
@ -18,11 +18,11 @@ object AppUpdate {
} else if (checkVersion(last_version_force, BuildConfig.VERSION_NAME)) {
force = true
}
onResult.invoke(true, "检测到新版本")
onResult.invoke(true, force, "检测到新版本")
//提示执行更新
//startUpdate(result, fragment)
} else {
onResult.invoke(false, "当前版本为最新版本")
onResult.invoke(false, force, "当前版本为最新版本")
}
}
}

View File

@ -7,6 +7,7 @@ import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.State
import androidx.lifecycle.viewModelScope
import com.alipay.sdk.app.AuthTask
import com.img.rabbit.pages.LoginScreenType
import com.g.gysdk.GYManager
@ -27,13 +28,17 @@ import com.img.rabbit.provider.storage.GlobalStateManager
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.LoginBindEvent
import com.img.rabbit.utils.MMKVUtils
import com.img.rabbit.utils.StringUtils
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import okhttp3.RequestBody.Companion.toRequestBody
@ -102,7 +107,7 @@ class LoginViewModel : BaseViewModel() {
errorState.value = null
}
fun requestUserConfig(){
fun requestUserConfig(isInitConfig: Boolean = false){
mLaunch {
//先置空配置
PreferenceUtil.saveUserConfig(null)
@ -114,6 +119,16 @@ class LoginViewModel : BaseViewModel() {
PreferenceUtil.saveUserConfig(response.data)
userConfigResult.value = response
if(isInitConfig){
AppUpdate.checkUpdate(response.data.config?.versionEntity) { isUpdate, isForce, _ ->
if(isUpdate && isForce){
//强制更新
viewModelScope.launch{
applicationContext?.let { GlobalStateManager(it) }?.storeGlobalUpdateNotify(true)
}
}
}
}
//以下是刷新状态
applicationContext?.let { GlobalStateManager(it) }?.storeGlobalUserConfigNotify(true)
}else{