From edce977f7f78de6411fc73d92a54e36661efd28b Mon Sep 17 00:00:00 2001 From: GuoXiCheng <1377994267@qq.com> Date: Mon, 23 Sep 2024 22:32:23 +0800 Subject: [PATCH] add ApkVersionButton --- .../java/com/android/skip/data/SyncWorker.kt | 6 ++- .../android/skip/data/network/MyApiNetwork.kt | 10 +++++ .../skip/data/network/api/MyApiService.kt | 3 ++ .../skip/data/version/ApkVersionRepository.kt | 44 +++++++++++++++++++ .../skip/data/version/ApkVersionViewModel.kt | 20 +++++++++ .../skip/dataclass/VersionPostSchema.kt | 10 +++++ .../android/skip/ui/about/AboutActivity.kt | 20 +++------ .../skip/ui/about/version/ApkVersionButton.kt | 22 ++++++++++ .../com/android/skip/ui/main/MainActivity.kt | 5 +++ app/src/main/res/values/strings.xml | 1 + 10 files changed, 125 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/android/skip/data/version/ApkVersionRepository.kt create mode 100644 app/src/main/java/com/android/skip/data/version/ApkVersionViewModel.kt create mode 100644 app/src/main/java/com/android/skip/dataclass/VersionPostSchema.kt create mode 100644 app/src/main/java/com/android/skip/ui/about/version/ApkVersionButton.kt diff --git a/app/src/main/java/com/android/skip/data/SyncWorker.kt b/app/src/main/java/com/android/skip/data/SyncWorker.kt index 867dba4d..9b3b82d4 100644 --- a/app/src/main/java/com/android/skip/data/SyncWorker.kt +++ b/app/src/main/java/com/android/skip/data/SyncWorker.kt @@ -5,6 +5,7 @@ import androidx.hilt.work.HiltWorker import androidx.work.WorkerParameters import androidx.work.CoroutineWorker import com.android.skip.data.config.ConfigReadRepository +import com.android.skip.data.version.ApkVersionRepository import com.blankj.utilcode.util.LogUtils import dagger.assisted.Assisted import dagger.assisted.AssistedInject @@ -13,13 +14,16 @@ import dagger.assisted.AssistedInject class SyncWorker@AssistedInject constructor( @Assisted context: Context, @Assisted workerParams: WorkerParameters, - private val configReadRepository: ConfigReadRepository + private val configReadRepository: ConfigReadRepository, + private val versionRepository: ApkVersionRepository ) : CoroutineWorker(context, workerParams) { override suspend fun doWork(): Result { return try { LogUtils.d("SyncWorker doWork") val configPostSchema = configReadRepository.readConfig() configReadRepository.changeConfigPostState(configPostSchema) + + versionRepository.checkVersion() Result.success() } catch (e: Exception) { LogUtils.e(e) diff --git a/app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt b/app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt index 268b68a3..565449b3 100644 --- a/app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt +++ b/app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt @@ -1,6 +1,7 @@ package com.android.skip.data.network import com.android.skip.data.network.api.MyApiService +import com.blankj.utilcode.util.AppUtils import javax.inject.Inject import javax.inject.Singleton @@ -16,4 +17,13 @@ class MyApiNetwork @Inject constructor() { throw RuntimeException("Failed with error code: ${response.code()}") } } + + suspend fun fetchLatestVersion(): String { + val response = myApiService.getLatestVersion() + return if (response.isSuccessful) { + response.body().toString() + } else { + AppUtils.getAppVersionName() + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/data/network/api/MyApiService.kt b/app/src/main/java/com/android/skip/data/network/api/MyApiService.kt index 45ada478..504acde1 100644 --- a/app/src/main/java/com/android/skip/data/network/api/MyApiService.kt +++ b/app/src/main/java/com/android/skip/data/network/api/MyApiService.kt @@ -7,4 +7,7 @@ import retrofit2.http.Url interface MyApiService { @GET suspend fun getConfigFromUrl(@Url url: String): Response + + @GET("/latest_version.txt") + suspend fun getLatestVersion(): Response } \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/data/version/ApkVersionRepository.kt b/app/src/main/java/com/android/skip/data/version/ApkVersionRepository.kt new file mode 100644 index 00000000..c6542354 --- /dev/null +++ b/app/src/main/java/com/android/skip/data/version/ApkVersionRepository.kt @@ -0,0 +1,44 @@ +package com.android.skip.data.version + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import com.android.skip.R +import com.android.skip.data.network.MyApiNetwork +import com.android.skip.dataclass.VersionPostSchema +import com.android.skip.dataclass.VersionState +import com.blankj.utilcode.util.AppUtils +import com.blankj.utilcode.util.StringUtils.getString +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class ApkVersionRepository @Inject constructor( + private val myApiNetwork: MyApiNetwork +) { + private val _versionPostState = MutableLiveData() + val versionPostState: LiveData = _versionPostState + + private suspend fun changeVersionState(versionPostState: VersionPostSchema) = + withContext(Dispatchers.IO) { + _versionPostState.postValue(versionPostState) + val version = myApiNetwork.fetchLatestVersion() + if (version == AppUtils.getAppVersionName()) { + _versionPostState.postValue(VersionPostSchema(VersionState.CURRENT_LATEST, version)) + } else { + _versionPostState.postValue( + VersionPostSchema( + VersionState.DISCOVER_LATEST, + getString(R.string.about_discover_latest) + ) + ) + } + } + + suspend fun checkVersion() { + changeVersionState( + VersionPostSchema(VersionState.PENDING, getString(R.string.checking)) + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/data/version/ApkVersionViewModel.kt b/app/src/main/java/com/android/skip/data/version/ApkVersionViewModel.kt new file mode 100644 index 00000000..419b1995 --- /dev/null +++ b/app/src/main/java/com/android/skip/data/version/ApkVersionViewModel.kt @@ -0,0 +1,20 @@ +package com.android.skip.data.version + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class ApkVersionViewModel @Inject constructor( + private val apkVersionRepository: ApkVersionRepository +) : ViewModel() { + val versionPostState = apkVersionRepository.versionPostState + + fun checkVersion() { + viewModelScope.launch { + apkVersionRepository.checkVersion() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/dataclass/VersionPostSchema.kt b/app/src/main/java/com/android/skip/dataclass/VersionPostSchema.kt new file mode 100644 index 00000000..7d893032 --- /dev/null +++ b/app/src/main/java/com/android/skip/dataclass/VersionPostSchema.kt @@ -0,0 +1,10 @@ +package com.android.skip.dataclass + +enum class VersionState { + PENDING, CURRENT_LATEST, DISCOVER_LATEST +} + +data class VersionPostSchema( + val status: VersionState, + val value: String +) \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/ui/about/AboutActivity.kt b/app/src/main/java/com/android/skip/ui/about/AboutActivity.kt index 9e7d5f61..fe6cc84e 100644 --- a/app/src/main/java/com/android/skip/ui/about/AboutActivity.kt +++ b/app/src/main/java/com/android/skip/ui/about/AboutActivity.kt @@ -5,17 +5,16 @@ import android.os.Bundle import androidx.activity.compose.setContent import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.outlined.Info import androidx.compose.material3.DropdownMenuItem -import androidx.compose.material3.Icon import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.res.stringResource import com.android.skip.MyApp import com.android.skip.R import com.android.skip.data.config.ConfigViewModel +import com.android.skip.data.version.ApkVersionViewModel import com.android.skip.ui.about.config.ConfigVersionButton +import com.android.skip.ui.about.version.ApkVersionButton import com.android.skip.ui.components.FlatButton import com.android.skip.ui.components.ResourceIcon import com.android.skip.ui.components.RowContent @@ -23,7 +22,6 @@ import com.android.skip.ui.components.ScaffoldPage import com.android.skip.ui.settings.theme.SwitchThemeViewModel import com.android.skip.ui.theme.AppTheme import com.android.skip.ui.webview.WebViewActivity -import com.blankj.utilcode.util.AppUtils import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint @@ -32,6 +30,8 @@ class AboutActivity : AppCompatActivity() { private val switchThemeViewModel by viewModels() + private val apkVersionViewModel by viewModels() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { @@ -50,7 +50,7 @@ class AboutActivity : AppCompatActivity() { } startActivity(intent) } - AboutAppVersion(AppUtils.getAppVersionName()) + ApkVersionButton(apkVersionViewModel) ConfigVersionButton(configViewModel) }, { DropdownMenuItem( @@ -86,14 +86,4 @@ fun AboutDocs(onClick: () -> Unit) { subTitle = R.string.about_docs_subtitle ) }, onClick = onClick) -} - -@Composable -fun AboutAppVersion(version: String) { - FlatButton(content = { - RowContent( - title = R.string.about_app_version, - subTitle = version - ) - }) } \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/ui/about/version/ApkVersionButton.kt b/app/src/main/java/com/android/skip/ui/about/version/ApkVersionButton.kt new file mode 100644 index 00000000..cdf62c04 --- /dev/null +++ b/app/src/main/java/com/android/skip/ui/about/version/ApkVersionButton.kt @@ -0,0 +1,22 @@ +package com.android.skip.ui.about.version + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.livedata.observeAsState +import com.android.skip.R +import com.android.skip.data.version.ApkVersionViewModel +import com.android.skip.ui.components.FlatButton +import com.android.skip.ui.components.RowContent + +@Composable +fun ApkVersionButton(apkVersionViewModel: ApkVersionViewModel) { + val versionPostState = apkVersionViewModel.versionPostState.observeAsState() + + FlatButton(content = { + RowContent( + title = R.string.about_app_version, + subTitle = versionPostState.value?.value + ) + }, { + apkVersionViewModel.checkVersion() + }) +} \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/ui/main/MainActivity.kt b/app/src/main/java/com/android/skip/ui/main/MainActivity.kt index 86cb2899..8771cac3 100644 --- a/app/src/main/java/com/android/skip/ui/main/MainActivity.kt +++ b/app/src/main/java/com/android/skip/ui/main/MainActivity.kt @@ -22,6 +22,7 @@ import androidx.compose.ui.unit.sp import com.android.skip.MyApp import com.android.skip.R import com.android.skip.data.config.ConfigViewModel +import com.android.skip.data.version.ApkVersionViewModel import com.android.skip.ui.about.AboutActivity import com.android.skip.ui.alive.AliveActivity import com.android.skip.ui.components.FlatButton @@ -44,6 +45,8 @@ class MainActivity : AppCompatActivity() { private val configViewModel by viewModels() + private val apkVersionViewModel by viewModels() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { @@ -82,6 +85,8 @@ class MainActivity : AppCompatActivity() { configViewModel.configPostState.observe(this) { configViewModel.loadConfig(it) } + + apkVersionViewModel.checkVersion() } override fun onResume() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 57b64a7e..2bb870c2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -68,6 +68,7 @@ 未检测到有效的配置 功能介绍 https://skip.guoxicheng.top/guide/about/intro + 发现新版本 请先开启无障碍服务 保存成功