Skip to content

Commit

Permalink
feat: 支持 checkUpdateUrl 字段
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 4, 2023
1 parent c0011a9 commit ffbb2d4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/SubsVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package li.songe.gkd.data

import kotlinx.serialization.Serializable

@Serializable
data class SubsVersion(val id: Long, val version: Int)
15 changes: 13 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/data/SubscriptionRaw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import blue.endless.jankson.Jankson
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.long
import li.songe.gkd.util.json
import li.songe.selector.Selector

Expand All @@ -17,6 +26,7 @@ data class SubscriptionRaw(
val author: String? = null,
val updateUrl: String? = null,
val supportUri: String? = null,
val checkUpdateUrl: String? = null,
val apps: List<AppRaw> = emptyList(),
) {

Expand Down Expand Up @@ -63,7 +73,7 @@ data class SubscriptionRaw(
override val resetMatch: String?,
override val activityIds: List<String>?,
override val excludeActivityIds: List<String>?,
val rules: List<RuleRaw> = emptyList(),
val rules: List<RuleRaw>,
val snapshotUrls: List<String>?,
val exampleUrls: List<String>?,
) : CommonProps {
Expand Down Expand Up @@ -291,6 +301,7 @@ data class SubscriptionRaw(
author = getString(rootJson, "author"),
updateUrl = getString(rootJson, "updateUrl"),
supportUri = getString(rootJson, "supportUri"),
checkUpdateUrl = getString(rootJson, "checkUpdateUrl"),
apps = rootJson["apps"]?.jsonArray?.mapIndexed { index, jsonElement ->
jsonToAppRaw(
jsonElement.jsonObject, index
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.accessibility.AccessibilityEvent
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ServiceUtils
import com.blankj.utilcode.util.ToastUtils
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.Dispatchers
Expand All @@ -33,6 +34,7 @@ import li.songe.gkd.data.ActionResult
import li.songe.gkd.data.GkdAction
import li.songe.gkd.data.NodeInfo
import li.songe.gkd.data.RpcError
import li.songe.gkd.data.SubsVersion
import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.data.getActionFc
import li.songe.gkd.db.DbSet
Expand All @@ -55,6 +57,8 @@ class GkdAbService : CompositionAbService({

val context = this as GkdAbService

context.resources

val scope = useScope()

service = context
Expand Down Expand Up @@ -235,13 +239,25 @@ class GkdAbService : CompositionAbService({
subsItemsFlow.value.forEach { subsItem ->
if (subsItem.updateUrl == null) return@forEach
try {
val oldSubsRaw = subsIdToRawFlow.value[subsItem.id]
if (oldSubsRaw?.checkUpdateUrl != null) {
try {
val subsVersion =
client.get(oldSubsRaw.checkUpdateUrl).body<SubsVersion>()
LogUtils.d("快速检测更新成功", subsVersion)
if (subsVersion.id == oldSubsRaw.id && subsVersion.version <= oldSubsRaw.version) {
return@forEach
}
} catch (e: Exception) {
LogUtils.d("快速检测更新失败", subsItem, e)
}
}
val newSubsRaw = SubscriptionRaw.parse(
client.get(subsItem.updateUrl).bodyAsText()
)
if (newSubsRaw.id != subsItem.id) {
return@forEach
}
val oldSubsRaw = subsIdToRawFlow.value[subsItem.id]
if (oldSubsRaw != null && newSubsRaw.version <= oldSubsRaw.version) {
return@forEach
}
Expand All @@ -258,7 +274,7 @@ class GkdAbService : CompositionAbService({
LogUtils.d("更新磁盘订阅文件:${newSubsRaw.name}")
} catch (e: Exception) {
e.printStackTrace()
LogUtils.d("更新失败", e)
LogUtils.d("检测更新失败", e)
}
}
lastUpdateSubsTime = System.currentTimeMillis()
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/ui/SubsManageVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package li.songe.gkd.ui
import android.webkit.URLUtil
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.Dispatchers
Expand All @@ -13,6 +15,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import li.songe.gkd.data.SubsItem
import li.songe.gkd.data.SubsVersion
import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.client
Expand Down Expand Up @@ -97,6 +100,18 @@ class SubsManageVm @Inject constructor() : ViewModel() {
if (oldItem.updateUrl == null) return@mapNotNull null
val oldSubsRaw = subsIdToRawFlow.value[oldItem.id]
try {
if (oldSubsRaw?.checkUpdateUrl != null) {
try {
val subsVersion =
client.get(oldSubsRaw.checkUpdateUrl).body<SubsVersion>()
LogUtils.d("快速检测更新成功", subsVersion)
if (subsVersion.id == oldSubsRaw.id && subsVersion.version <= oldSubsRaw.version) {
return@mapNotNull null
}
} catch (e: Exception) {
LogUtils.d("快速检测更新失败", oldItem, e)
}
}
val newSubsRaw = SubscriptionRaw.parse(
client.get(oldItem.updateUrl).bodyAsText()
)
Expand Down

0 comments on commit ffbb2d4

Please sign in to comment.