Skip to content

Commit

Permalink
Merge pull request #248 from GuoXiCheng/dev
Browse files Browse the repository at this point in the history
add ApkVersionButton
  • Loading branch information
GuoXiCheng authored Sep 23, 2024
2 parents 016c370 + edce977 commit 0efb28d
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 16 deletions.
6 changes: 5 additions & 1 deletion app/src/main/java/com/android/skip/data/SyncWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/android/skip/data/network/MyApiNetwork.kt
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import retrofit2.http.Url
interface MyApiService {
@GET
suspend fun getConfigFromUrl(@Url url: String): Response<String>

@GET("/latest_version.txt")
suspend fun getLatestVersion(): Response<String>
}
Original file line number Diff line number Diff line change
@@ -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<VersionPostSchema>()
val versionPostState: LiveData<VersionPostSchema> = _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))
)
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/android/skip/dataclass/VersionPostSchema.kt
Original file line number Diff line number Diff line change
@@ -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
)
20 changes: 5 additions & 15 deletions app/src/main/java/com/android/skip/ui/about/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ 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
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
Expand All @@ -32,6 +30,8 @@ class AboutActivity : AppCompatActivity() {

private val switchThemeViewModel by viewModels<SwitchThemeViewModel>()

private val apkVersionViewModel by viewModels<ApkVersionViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand All @@ -50,7 +50,7 @@ class AboutActivity : AppCompatActivity() {
}
startActivity(intent)
}
AboutAppVersion(AppUtils.getAppVersionName())
ApkVersionButton(apkVersionViewModel)
ConfigVersionButton(configViewModel)
}, {
DropdownMenuItem(
Expand Down Expand Up @@ -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
)
})
}
Original file line number Diff line number Diff line change
@@ -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()
})
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/android/skip/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +45,8 @@ class MainActivity : AppCompatActivity() {

private val configViewModel by viewModels<ConfigViewModel>()

private val apkVersionViewModel by viewModels<ApkVersionViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down Expand Up @@ -82,6 +85,8 @@ class MainActivity : AppCompatActivity() {
configViewModel.configPostState.observe(this) {
configViewModel.loadConfig(it)
}

apkVersionViewModel.checkVersion()
}

override fun onResume() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="invalid_config">未检测到有效的配置</string>
<string name="about_function_intro">功能介绍</string>
<string name="about_function_intro_url">https://skip.guoxicheng.top/guide/about/intro</string>
<string name="about_discover_latest">发现新版本</string>

<string name="toast_start_accessibility_first">请先开启无障碍服务</string>
<string name="toast_save_success">保存成功</string>
Expand Down

0 comments on commit 0efb28d

Please sign in to comment.