From ffbb2d4950e7b87d9ff08a7ef103217e02d4f3dc Mon Sep 17 00:00:00 2001 From: lisonge Date: Mon, 4 Dec 2023 21:22:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20checkUpdateUrl=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/li/songe/gkd/data/SubsVersion.kt | 6 ++++++ .../li/songe/gkd/data/SubscriptionRaw.kt | 15 ++++++++++++-- .../li/songe/gkd/service/GkdAbService.kt | 20 +++++++++++++++++-- .../kotlin/li/songe/gkd/ui/SubsManageVm.kt | 15 ++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/li/songe/gkd/data/SubsVersion.kt diff --git a/app/src/main/kotlin/li/songe/gkd/data/SubsVersion.kt b/app/src/main/kotlin/li/songe/gkd/data/SubsVersion.kt new file mode 100644 index 0000000..bdfd116 --- /dev/null +++ b/app/src/main/kotlin/li/songe/gkd/data/SubsVersion.kt @@ -0,0 +1,6 @@ +package li.songe.gkd.data + +import kotlinx.serialization.Serializable + +@Serializable +data class SubsVersion(val id: Long, val version: Int) diff --git a/app/src/main/kotlin/li/songe/gkd/data/SubscriptionRaw.kt b/app/src/main/kotlin/li/songe/gkd/data/SubscriptionRaw.kt index dfb4626..ffa5c51 100644 --- a/app/src/main/kotlin/li/songe/gkd/data/SubscriptionRaw.kt +++ b/app/src/main/kotlin/li/songe/gkd/data/SubscriptionRaw.kt @@ -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 @@ -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 = emptyList(), ) { @@ -63,7 +73,7 @@ data class SubscriptionRaw( override val resetMatch: String?, override val activityIds: List?, override val excludeActivityIds: List?, - val rules: List = emptyList(), + val rules: List, val snapshotUrls: List?, val exampleUrls: List?, ) : CommonProps { @@ -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 diff --git a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt index 736125d..85da467 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt @@ -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 @@ -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 @@ -55,6 +57,8 @@ class GkdAbService : CompositionAbService({ val context = this as GkdAbService + context.resources + val scope = useScope() service = context @@ -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() + 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 } @@ -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() diff --git a/app/src/main/kotlin/li/songe/gkd/ui/SubsManageVm.kt b/app/src/main/kotlin/li/songe/gkd/ui/SubsManageVm.kt index b6a474a..65c7368 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/SubsManageVm.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/SubsManageVm.kt @@ -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 @@ -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 @@ -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() + 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() )