Skip to content

Commit

Permalink
change app updater
Browse files Browse the repository at this point in the history
  • Loading branch information
lings03 committed Nov 14, 2024
1 parent d71e88d commit 1b43ef7
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 171 deletions.
13 changes: 5 additions & 8 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
minSdk = 28
targetSdk = 35
versionCode = 31
versionName = "3.2.0"
versionName = "3.2.0-test"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -83,10 +83,6 @@ android {
isCoreLibraryDesugaringEnabled = true
}

kotlinOptions {
jvmTarget = "21"
}

buildFeatures {
buildConfig = true
compose = true
Expand All @@ -111,6 +107,10 @@ android {
}
}

kotlin {
jvmToolchain(21)
}

androidComponents {
onVariants(selector().withBuildType("release")) {
it.packaging.resources.excludes.addAll(
Expand Down Expand Up @@ -199,7 +199,4 @@ dependencies {
implementation(libs.accompanist.pager)
// 依赖注入
coreLibraryDesugaring(libs.desugar)

}


1 change: 0 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
-dontwarn androidx.window.sidecar.Sidecar*

-keep class net.sf.sevenzipjbinding.** { *; }
-keep class top.laoxin.modmanager.** { *; }
-keep class okhttp3.** { *; }
-keep class com.squareup.okhttp.** { *; }

Expand Down
25 changes: 12 additions & 13 deletions app/src/main/kotlin/top/laoxin/modmanager/bean/GithubBean.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package top.laoxin.modmanager.bean

import kotlinx.serialization.Serializable
import com.google.gson.annotations.SerializedName
import top.lings.updater.util.AppConfig

@Serializable
data class GithubBean(
val tag_name: String,
val name: String,
val body: String,
val published_at: String,
val assets: List<Asset>
)
@SerializedName("tag_name") val version: String,
@SerializedName("body") val info: String,
@SerializedName("assets") val assets: List<GitHubAssets>
) {
fun getDownloadLink(): String {
val asset = assets.find { AppConfig.matchVariant(it.downloadLink) } ?: assets[0]
return asset.downloadLink
}
}

@Serializable
data class Asset(
val name: String,
val browser_download_url: String
)
data class GitHubAssets(@SerializedName("browser_download_url") val downloadLink: String)

This file was deleted.

22 changes: 10 additions & 12 deletions app/src/main/kotlin/top/laoxin/modmanager/ui/view/Console.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,16 @@ fun ConsoleContent(viewModel: ConsoleViewModel) {
// 请求通知权限
RequestNotificationPermission()
// 升级提示
DialogCommonForUpdate(
title = stringResource(id = R.string.console_upgrade_title),
content = viewModel.updateContent,
onConfirm = {
// viewModel.setShowUpgradeDialog(false)
viewModel.openUrl(context, viewModel.downloadUrl)
},
// onCancel = {
// viewModel.setShowUpgradeDialog(false)
// },
showDialog = uiState.showUpgradeDialog
)
viewModel.updateContent?.let {
DialogCommonForUpdate(
title = stringResource(id = R.string.console_upgrade_title),
content = it,
onConfirm = {
viewModel.downloadUrl?.let { url -> viewModel.openUrl(context, url) }
},
showDialog = uiState.showUpgradeDialog
)
}

// 信息提示
DialogCommon(
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/kotlin/top/laoxin/modmanager/ui/view/Setting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ fun SettingPage() {
}
}

DialogCommonForUpdate(
title = stringResource(id = R.string.console_upgrade_title),
content = viewModel.updateDescription,
onConfirm = {
// viewModel.setShowUpgradeDialog(false)
viewModel.openUrl(context, viewModel.downloadUrl)
},
// onCancel = {
// viewModel.setShowUpgradeDialog(false)
// },
showDialog = uiState.showUpdateDialog
)
viewModel.updateContent?.let {
DialogCommonForUpdate(
title = stringResource(id = R.string.console_upgrade_title),
content = it,
onConfirm = {
viewModel.downloadUrl?.let { url -> viewModel.openUrl(context, url) }
},

showDialog = uiState.showUpdateDialog
)
}

DialogCommon(
title = stringResource(id = R.string.console_game_tips_title),
content = viewModel.gameInfo.tips,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import top.laoxin.modmanager.database.UserPreferencesRepository
import top.laoxin.modmanager.database.antiHarmony.AntiHarmonyRepository
import top.laoxin.modmanager.database.mods.ModRepository
import top.laoxin.modmanager.network.ModManagerApi
import top.laoxin.modmanager.network.ModManagerGithubApi
import top.laoxin.modmanager.observer.FlashModsObserver
import top.laoxin.modmanager.tools.LogTools
import top.laoxin.modmanager.tools.ModTools
Expand All @@ -54,6 +53,7 @@ import top.laoxin.modmanager.tools.ToastUtils
import top.laoxin.modmanager.ui.state.ConsoleUiState
import top.laoxin.modmanager.ui.state.UserPreferencesState
import top.laoxin.modmanager.userservice.gamestart.ProjectSnowStartService
import top.lings.updater.Updater
import java.io.File


Expand All @@ -67,18 +67,20 @@ class ConsoleViewModel(
private var _requestPermissionPath by mutableStateOf("")

// 下载地址
private var _downloadUrl by mutableStateOf("")
val downloadUrl: String
private var _downloadUrl: String? by mutableStateOf("")
val downloadUrl: String?
get() = _downloadUrl
val requestPermissionPath: String
get() = _requestPermissionPath

// 更新类容
private var _updateContent by mutableStateOf("")
val updateContent: String
// 更新内容
private var _updateContent: String? by mutableStateOf("")
val updateContent: String?
get() = _updateContent


val requestPermissionPath: String
get() = _requestPermissionPath


private val _uiState = MutableStateFlow<ConsoleUiState>(ConsoleUiState())

companion object {
Expand Down Expand Up @@ -177,6 +179,15 @@ class ConsoleViewModel(
}
}

private fun checkUpdate() {
viewModelScope.launch {
val update = Updater.checkUpdate()
_downloadUrl = update?.first
_updateContent = update?.second
setShowUpgradeDialog(true)
}
}

private fun getNewInfo() {
viewModelScope.launch {
kotlin.runCatching {
Expand Down Expand Up @@ -535,41 +546,6 @@ class ConsoleViewModel(
// }
// }
// }
private fun checkUpdate() {
viewModelScope.launch {
kotlin.runCatching {
ModManagerGithubApi.retrofitService.getLatestRelease()
}.onFailure { exception ->
Log.e("ConsoleViewModel", "checkUpdate failed: ${exception.message}", exception)
}.onSuccess { release ->
Log.d("ConsoleViewModel", "Update available: $release")
// 当前版本号
val currentVersion = ModTools.getVersionName()
// tag_name 是版本号,这里进行比较
val releaseVersion = release.tag_name

if (releaseVersion != currentVersion) {
Log.d("ConsoleViewModel", "Update available: $release")

// 获取v8a资产
val v8aAsset = release.assets.firstOrNull {
it.name.contains(
"arm64-v8a",
ignoreCase = true
) ||
it.browser_download_url.contains(
"arm64-v8a",
ignoreCase = true
)
}
_downloadUrl = v8aAsset?.browser_download_url ?: ""
_updateContent = release.body + "\n\n无法安装请前往github下载universal版本apk"
setShowUpgradeDialog(true)
}
}
}
}


fun startGameService() {
val intent = Intent(App.get(), ProjectSnowStartService::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import top.laoxin.modmanager.database.UserPreferencesRepository
import top.laoxin.modmanager.database.backups.BackupRepository
import top.laoxin.modmanager.database.mods.ModRepository
import top.laoxin.modmanager.network.ModManagerApi
import top.laoxin.modmanager.network.ModManagerGithubApi
import top.laoxin.modmanager.tools.ModTools
import top.laoxin.modmanager.tools.PermissionTools
import top.laoxin.modmanager.tools.ToastUtils
import top.laoxin.modmanager.ui.state.SettingUiState
import top.lings.updater.Updater


class SettingViewModel(
Expand Down Expand Up @@ -66,14 +66,15 @@ class SettingViewModel(
private var _gameInfo = mutableStateOf(GameInfoConstant.gameInfoList[0])
val gameInfo get() = _gameInfo.value

// 更新描述
private var _updateDescription by mutableStateOf("")
val updateDescription get() = _updateDescription

// 下载地址
private var _downloadUrl by mutableStateOf("")
private var _downloadUrl: String? by mutableStateOf("")
val downloadUrl: String?
get() = _downloadUrl

val downloadUrl get() = _downloadUrl
// 更新内容
private var _updateContent: String? by mutableStateOf("")
val updateContent: String?
get() = _updateContent

init {

Expand Down Expand Up @@ -304,39 +305,10 @@ class SettingViewModel(
// }
fun checkUpdate() {
viewModelScope.launch {
kotlin.runCatching {
ModManagerGithubApi.retrofitService.getLatestRelease()
}.onFailure { exception ->
Log.e("ConsoleViewModel", "checkUpdate failed: ${exception.message}", exception)
}.onSuccess { release ->
Log.d("ConsoleViewModel", "Update available: $release")
// 当前版本号
val currentVersion = ModTools.getVersionName()
// tag_name 是版本号,这里进行比较
val releaseVersion = release.tag_name

if (releaseVersion != currentVersion) {
Log.d("ConsoleViewModel", "Update available: $release")

// 获取v8a资产
val v8aAsset = release.assets.firstOrNull {
it.name.contains(
"arm64-v8a",
ignoreCase = true
) ||
it.browser_download_url.contains(
"arm64-v8a",
ignoreCase = true
)
}
_downloadUrl = v8aAsset?.browser_download_url ?: ""
_updateDescription =
release.body + "\n\n无法安装请前往github下载universal版本apk"
setShowUpgradeDialog(true)
} else {
ToastUtils.longCall(R.string.toast_no_update)
}
}
val update = Updater.checkUpdate()
_downloadUrl = update?.first
_updateContent = update?.second
setShowUpgradeDialog(true)
}
}

Expand Down
30 changes: 30 additions & 0 deletions app/src/main/kotlin/top/lings/updater/GithubApiService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package top.lings.updater

import com.google.gson.GsonBuilder
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import top.laoxin.modmanager.bean.GithubBean

private const val BASE_URL_GITHUB =
"https://api.github.com"

val gsonForGithub = GsonBuilder().create()

private val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gsonForGithub))
.baseUrl(BASE_URL_GITHUB)
.build()

interface GithubApiService {
@GET("repos/laoxinH/crosscore-mod-manager/releases/latest")
suspend fun getLatestRelease(): GithubBean

}

object GithubApi {
val retrofitService: GithubApiService by lazy {
retrofit.create(GithubApiService::class.java)
}
}

Loading

0 comments on commit 1b43ef7

Please sign in to comment.