Skip to content

Commit

Permalink
perf: 优化APP更新逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 27, 2023
1 parent 2bbdb40 commit d4408e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/li/songe/gkd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import li.songe.gkd.notif.initChannel
import li.songe.gkd.util.initAppState
import li.songe.gkd.util.initStore
import li.songe.gkd.util.initSubsState
import li.songe.gkd.util.initUpgrade
import li.songe.gkd.util.isMainProcess
import li.songe.gkd.util.launchTry
import org.lsposed.hiddenapibypass.HiddenApiBypass
Expand Down Expand Up @@ -55,7 +54,6 @@ class App : Application() {
initSubsState()
if (isMainProcess) {
initChannel()
initUpgrade()
clearHttpSubs()
}
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/li/songe/gkd/ui/HomePageVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
import li.songe.gkd.appScope
import li.songe.gkd.data.GithubPoliciesAsset
Expand All @@ -27,7 +28,9 @@ import li.songe.gkd.util.FILE_UPLOAD_URL
import li.songe.gkd.util.FolderExt
import li.songe.gkd.util.LoadStatus
import li.songe.gkd.util.Singleton
import li.songe.gkd.util.checkUpdate
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.storeFlow
import java.io.File
import javax.inject.Inject

Expand Down Expand Up @@ -72,6 +75,16 @@ class HomePageVm @Inject constructor() : ViewModel() {
LogUtils.d("执行快照迁移")
}
}

if (storeFlow.value.autoCheckAppUpdate) {
appScope.launch {
try {
checkUpdate()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}

val uploadStatusFlow = MutableStateFlow<LoadStatus<GithubPoliciesAsset>?>(null)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/li/songe/gkd/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const val DEFAULT_SUBS_UPDATE_URL =

const val IMPORT_BASE_URL = "https://i.gkd.li/import/"

const val UPDATE_URL = "https://registry.npmmirror.com/@gkd-kit/app/latest/files/index.json"

val safeRemoteBaseUrls = arrayOf(
"https://registry.npmmirror.com/@gkd-kit/",
"https://cdn.jsdelivr.net/npm/@gkd-kit/",
Expand Down
43 changes: 21 additions & 22 deletions app/src/main/java/li/songe/gkd/util/Upgrade.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package li.songe.gkd.util

import android.os.Parcelable
import android.util.Log
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
Expand All @@ -26,24 +25,28 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
import li.songe.gkd.BuildConfig
import li.songe.gkd.appScope
import java.io.File
import java.net.URI

@Serializable
@Parcelize
data class NewVersion(
val versionCode: Int,
val versionName: String,
val changelog: String,
val downloadUrl: String,
val versionLogs: List<VersionLog> = emptyList(),
val fileSize: Long? = null,
) : Parcelable
)

private const val UPDATE_URL = "https://registry.npmmirror.com/@gkd-kit/app/latest/files/index.json"
@Serializable
data class VersionLog(
val name: String,
val code: Int,
val desc: String,
)

val checkUpdatingFlow by lazy { MutableStateFlow(false) }
val newVersionFlow by lazy { MutableStateFlow<NewVersion?>(null) }
Expand All @@ -54,7 +57,8 @@ suspend fun checkUpdate(): NewVersion? {
try {
val newVersion = Singleton.client.get(UPDATE_URL).body<NewVersion>()
if (newVersion.versionCode > BuildConfig.VERSION_CODE) {
newVersionFlow.value = newVersion
newVersionFlow.value =
newVersion.copy(versionLogs = newVersion.versionLogs.takeWhile { v -> v.code > BuildConfig.VERSION_CODE })
return newVersion
} else {
Log.d("Upgrade", "no new version")
Expand Down Expand Up @@ -106,16 +110,23 @@ fun startDownload(newVersion: NewVersion) {
fun UpgradeDialog() {
val newVersion by newVersionFlow.collectAsState()
newVersion?.let { newVersionVal ->

AlertDialog(title = {
Text(text = "检测到新版本")
}, text = {
Text(
text = "v${BuildConfig.VERSION_NAME} -> v${newVersionVal.versionName}\n\n${newVersionVal.changelog}\n${newVersionVal.changelog}".trimEnd(),
Text(text = "v${BuildConfig.VERSION_NAME} -> v${newVersionVal.versionName}\n\n${
if (newVersionVal.versionLogs.size > 1) {
newVersionVal.versionLogs.joinToString("\n\n") { v -> "v${v.name}\n${v.desc}" }
} else if (newVersionVal.versionLogs.isNotEmpty()) {
newVersionVal.versionLogs.first().desc
} else {
""
}
}".trimEnd(),
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 400.dp)
.verticalScroll(rememberScrollState())
)
.verticalScroll(rememberScrollState()))

}, onDismissRequest = { }, confirmButton = {
TextButton(onClick = {
Expand Down Expand Up @@ -194,18 +205,6 @@ fun UpgradeDialog() {
}
}

fun initUpgrade() {
if (storeFlow.value.autoCheckAppUpdate && isMainProcess) {
appScope.launch {
try {
checkUpdate()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}




Expand Down

0 comments on commit d4408e5

Please sign in to comment.