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" 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.CoroutineScope
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import me.ash.reader.R 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.ApplicationScope
import me.ash.reader.data.module.DispatcherIO import me.ash.reader.data.module.DispatcherIO
import me.ash.reader.data.source.AppNetworkDataSource import me.ash.reader.data.source.AppNetworkDataSource
@ -24,12 +24,12 @@ class AppRepository @Inject constructor(
private val dispatcherIO: CoroutineDispatcher, private val dispatcherIO: CoroutineDispatcher,
) { ) {
suspend fun checkUpdate(): Boolean = withContext(dispatcherIO) { suspend fun checkUpdate(): Boolean = withContext(dispatcherIO) {
return@withContext try { try {
val latest = val latest =
appNetworkDataSource.getReleaseLatest(context.getString(R.string.update_link)) appNetworkDataSource.getReleaseLatest(context.getString(R.string.update_link))
val latestVersion = Version(latest.tag_name) val latestVersion = latest.tag_name.toVersion()
// val latestVersion = Version("0.7.3") // val latestVersion = "0.7.3".toVersion()
val skipVersion = Version(context.skipVersionNumber) val skipVersion = context.skipVersionNumber.toVersion()
val currentVersion = context.getCurrentVersion() val currentVersion = context.getCurrentVersion()
val latestLog = latest.body ?: "" val latestLog = latest.body ?: ""
val latestPublishDate = latest.published_at ?: latest.created_at ?: "" 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.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import me.ash.reader.data.entity.Version import me.ash.reader.data.entity.Version
import me.ash.reader.data.entity.toVersion
fun Context.findActivity(): Activity? = when (this) { fun Context.findActivity(): Activity? = when (this) {
is Activity -> this is Activity -> this
@ -11,5 +12,7 @@ fun Context.findActivity(): Activity? = when (this) {
else -> null else -> null
} }
fun Context.getCurrentVersion(): Version = fun Context.getCurrentVersion(): Version = packageManager
Version(packageManager.getPackageInfo(packageName, 0).versionName) .getPackageInfo(packageName, 0)
.versionName
.toVersion()

View File

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

View File

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

View File

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

View File

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