Skip to content

Commit

Permalink
perf: 优化自动检测订阅更新逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 5, 2023
1 parent 3a79990 commit 36bac5d
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import li.songe.gkd.db.DbSet
import li.songe.gkd.shizuku.useSafeGetTasksFc
import li.songe.gkd.util.client
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.launchWhile
import li.songe.gkd.util.map
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.subsIdToRawFlow
Expand Down Expand Up @@ -232,10 +231,9 @@ class GkdAbService : CompositionAbService({
}
}


var lastUpdateSubsTime = 0L
scope.launchWhile(Dispatchers.IO) { // 自动从网络更新订阅文件
if (storeFlow.value.updateSubsInterval > 0 && System.currentTimeMillis() - lastUpdateSubsTime < storeFlow.value.updateSubsInterval) {
fun checkSubsUpdate() {
scope.launchTry(Dispatchers.IO) { // 自动从网络更新订阅文件
LogUtils.d("开始自动检测更新")
subsItemsFlow.value.forEach { subsItem ->
if (subsItem.updateUrl == null) return@forEach
try {
Expand Down Expand Up @@ -277,9 +275,20 @@ class GkdAbService : CompositionAbService({
LogUtils.d("检测更新失败", e)
}
}
lastUpdateSubsTime = System.currentTimeMillis()
}
delay(30 * 60_000) // 每 30 分钟检查一次
}

var lastUpdateSubsTime = 0L
onAccessibilityEvent {
if (it?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {// 筛选降低判断频率
// 借助 无障碍事件 触发自动检测更新
val i = storeFlow.value.updateSubsInterval
val t = System.currentTimeMillis()
if (i > 0 && t - lastUpdateSubsTime > i.coerceAtLeast(60 * 60_000)) {
lastUpdateSubsTime = t
checkSubsUpdate()
}
}
}

scope.launch(Dispatchers.IO) {
Expand Down

0 comments on commit 36bac5d

Please sign in to comment.