Improve update check

This commit is contained in:
Ash 2022-04-24 20:02:14 +08:00
parent 52d6b0698d
commit 370d159432
7 changed files with 30 additions and 23 deletions

View File

@ -28,3 +28,5 @@ class Version(identifiers: List<String>) {
override fun toString() = "$major.$minor.$point"
}
fun String?.toVersion(): Version = Version(this)

View File

@ -7,7 +7,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import me.ash.reader.R
import me.ash.reader.data.entity.Version
import me.ash.reader.data.entity.toVersion
import me.ash.reader.data.module.ApplicationScope
import me.ash.reader.data.module.DispatcherIO
import me.ash.reader.data.source.AppNetworkDataSource
@ -24,12 +24,12 @@ class AppRepository @Inject constructor(
private val dispatcherIO: CoroutineDispatcher,
) {
suspend fun checkUpdate(): Boolean = withContext(dispatcherIO) {
return@withContext try {
try {
val latest =
appNetworkDataSource.getReleaseLatest(context.getString(R.string.update_link))
val latestVersion = Version(latest.tag_name)
// val latestVersion = Version("0.7.3")
val skipVersion = Version(context.skipVersionNumber)
val latestVersion = latest.tag_name.toVersion()
// val latestVersion = "0.7.3".toVersion()
val skipVersion = context.skipVersionNumber.toVersion()
val currentVersion = context.getCurrentVersion()
val latestLog = latest.body ?: ""
val latestPublishDate = latest.published_at ?: latest.created_at ?: ""

View File

@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import me.ash.reader.data.entity.Version
import me.ash.reader.data.entity.toVersion
fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
@ -11,5 +12,7 @@ fun Context.findActivity(): Activity? = when (this) {
else -> null
}
fun Context.getCurrentVersion(): Version =
Version(packageManager.getPackageInfo(packageName, 0).versionName)
fun Context.getCurrentVersion(): Version = packageManager
.getPackageInfo(packageName, 0)
.versionName
.toVersion()

View File

@ -30,6 +30,7 @@ import androidx.work.WorkInfo
import kotlinx.coroutines.flow.map
import me.ash.reader.R
import me.ash.reader.data.entity.Version
import me.ash.reader.data.entity.toVersion
import me.ash.reader.data.repository.SyncWorker.Companion.getIsSyncing
import me.ash.reader.ui.component.Banner
import me.ash.reader.ui.component.DisplayText
@ -65,14 +66,14 @@ fun FeedsPage(
val skipVersion = context.dataStore.data
.map { it[DataStoreKeys.SkipVersionNumber.key] ?: "" }
.map { Version(it) }
.collectAsState(initial = Version())
.collectAsState(initial = "")
.value
.toVersion()
val latestVersion = context.dataStore.data
.map { it[DataStoreKeys.NewVersionNumber.key] ?: "" }
.map { Version(it) }
.collectAsState(initial = Version())
.collectAsState(initial = "")
.value
.toVersion()
val currentVersion by remember { mutableStateOf(context.getCurrentVersion()) }
val owner = LocalLifecycleOwner.current

View File

@ -18,7 +18,7 @@ import androidx.compose.ui.zIndex
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.map
import me.ash.reader.R
import me.ash.reader.data.entity.Version
import me.ash.reader.data.entity.toVersion
import me.ash.reader.ui.component.Banner
import me.ash.reader.ui.component.DisplayText
import me.ash.reader.ui.component.FeedbackIconButton
@ -38,14 +38,14 @@ fun SettingsPage(
var updateDialogVisible by remember { mutableStateOf(false) }
val skipVersion = context.dataStore.data
.map { it[DataStoreKeys.SkipVersionNumber.key] ?: "" }
.map { Version(it) }
.collectAsState(initial = Version())
.collectAsState(initial = "")
.value
.toVersion()
val latestVersion = context.dataStore.data
.map { it[DataStoreKeys.NewVersionNumber.key] ?: "" }
.map { Version(it) }
.collectAsState(initial = Version())
.collectAsState(initial = "")
.value
.toVersion()
val currentVersion by remember { mutableStateOf(context.getCurrentVersion()) }
Scaffold(

View File

@ -110,7 +110,6 @@ fun TipsAndSupport(
},
onTap = {
scope.launch {
context.dataStore.put(DataStoreKeys.SkipVersionNumber, "")
updateViewModel.dispatch(
UpdateViewAction.CheckUpdate(
{
@ -120,12 +119,14 @@ fun TipsAndSupport(
)
},
{
if (!it) Toast.makeText(
if (!it) {
Toast.makeText(
context,
isLatestVersion,
Toast.LENGTH_SHORT
).show()
}
}
)
)
}

View File

@ -36,7 +36,7 @@ class UpdateViewModel @Inject constructor(
viewModelScope.launch {
preProcessor()
appRepository.checkUpdate().let {
if (it) changeUpdateDialogVisible(true)
changeUpdateDialogVisible(it)
postProcessor(it)
}
}