Skip to content

Commit

Permalink
优化检查更新
Browse files Browse the repository at this point in the history
  • Loading branch information
wilinz committed Aug 12, 2022
1 parent 8ab1c49 commit 629e8d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.autojs.autoxjs.network.entity

import com.google.gson.annotations.SerializedName
import org.autojs.autoxjs.BuildConfig
import org.joda.time.Instant

class GithubReleaseInfoList : ArrayList<GithubReleaseInfo>()
Expand Down Expand Up @@ -50,10 +51,13 @@ data class GithubReleaseInfo(
* Check latest version
* @return Returns null if targetCommitish is prerelease or not "dev-test"
*/
fun GithubReleaseInfo.isLatestVersion(anotherReleaseInfo: GithubReleaseInfo): Boolean? {
fun GithubReleaseInfo.isLatestVersion(): Boolean? {
if (targetCommitish != "dev-test" || prerelease) return null
return Instant.parse(createdAt).toDateTime().millis >=
Instant.parse(anotherReleaseInfo.createdAt).toDateTime().millis
return this.name.getVersionByName() <= BuildConfig.VERSION_NAME.getVersionByName()
}

private fun String.getVersionByName(): Long {
return this.replace(".", "").toLongOrNull() ?: -1
}

data class Asset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.autojs.autoxjs.BuildConfig
import org.autojs.autoxjs.R
import org.autojs.autoxjs.network.VersionService2
import org.autojs.autoxjs.network.entity.GithubReleaseInfo
Expand All @@ -29,30 +28,20 @@ class DrawerViewModel(private val context: Application) : AndroidViewModel(conte
viewModelScope.launch {
try {
var releaseInfo = VersionService2.gitUpdateCheckApi.getGithubLastReleaseInfo()
val currentInfoResponse =
VersionService2.gitUpdateCheckApi.getGithubLastReleaseInfo(BuildConfig.VERSION_NAME)

if (currentInfoResponse.code() != 200) {
versionInformationFound()
return@launch
}
val currentInfo = currentInfoResponse.body() ?: kotlin.run {
versionInformationFound()
return@launch
}
var isLatestVersion = currentInfo.isLatestVersion(releaseInfo)
var isLatestVersion = releaseInfo.isLatestVersion()
if (isLatestVersion == null) {
//Get release list
VersionService2.gitUpdateCheckApi.getGithubReleaseInfoList()
.firstOrNull { it.targetCommitish == "dev-test" && !it.prerelease }
?.let {
releaseInfo = it
isLatestVersion = currentInfo.isLatestVersion(releaseInfo)
isLatestVersion = releaseInfo.isLatestVersion()
}
}
if (isLatestVersion == null) {
//Can't find information
versionInformationFound()
versionInformationNotFound()
return@launch
}
if (isLatestVersion == true) {
Expand Down Expand Up @@ -84,7 +73,7 @@ class DrawerViewModel(private val context: Application) : AndroidViewModel(conte

}

private fun versionInformationFound() {
private fun versionInformationNotFound() {
Toast.makeText(
context,
context.getString(
Expand Down

0 comments on commit 629e8d9

Please sign in to comment.