Skip to content

Commit

Permalink
fix: subsRefreshingFlow not reset false
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 13, 2024
1 parent f7d8d4a commit 9b4ba5b
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions app/src/main/kotlin/li/songe/gkd/util/SubsState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,15 @@ private fun refreshRawSubsList(items: List<SubsItem>) {
fun initSubsState() {
subsItemsFlow.value
appScope.launchTry(Dispatchers.IO) {
subsRefreshingFlow.value = true
updateSubsFileMutex.withLock {
val items = DbSet.subsItemDao.queryAll()
refreshRawSubsList(items)
subsRefreshingFlow.value = true
try {
val items = DbSet.subsItemDao.queryAll()
refreshRawSubsList(items)
} finally {
subsRefreshingFlow.value = false
}
}
subsRefreshingFlow.value = false
}
}

Expand Down Expand Up @@ -399,53 +402,56 @@ fun checkSubsUpdate(showToast: Boolean = false) = appScope.launchTry(Dispatchers
if (updateSubsMutex.isLocked || subsRefreshingFlow.value) {
return@launchTry
}
subsRefreshingFlow.value = true
updateSubsMutex.withLock {
if (!withContext(Dispatchers.IO) { NetworkUtils.isAvailable() }) {
if (showToast) {
toast("网络不可用")
subsRefreshingFlow.value = true
try {
if (!NetworkUtils.isAvailable()) {
if (showToast) {
toast("网络不可用")
}
return@withLock
}
return@withLock
}
LogUtils.d("开始检测更新")
val localSubsEntries =
subsEntriesFlow.value.filter { e -> e.subsItem.id < 0 && e.subscription == null }
val subsEntries = subsEntriesFlow.value.filter { e -> e.subsItem.id >= 0 }
refreshRawSubsList(localSubsEntries.map { e -> e.subsItem })
LogUtils.d("开始检测更新")
val localSubsEntries =
subsEntriesFlow.value.filter { e -> e.subsItem.id < 0 && e.subscription == null }
val subsEntries = subsEntriesFlow.value.filter { e -> e.subsItem.id >= 0 }
refreshRawSubsList(localSubsEntries.map { e -> e.subsItem })

var successNum = 0
subsEntries.forEach { subsEntry ->
try {
val newSubsRaw = updateSubs(subsEntry)
if (newSubsRaw != null) {
updateSubscription(newSubsRaw)
successNum++
}
if (subsRefreshErrorsFlow.value.contains(subsEntry.subsItem.id)) {
var successNum = 0
subsEntries.forEach { subsEntry ->
try {
val newSubsRaw = updateSubs(subsEntry)
if (newSubsRaw != null) {
updateSubscription(newSubsRaw)
successNum++
}
if (subsRefreshErrorsFlow.value.contains(subsEntry.subsItem.id)) {
subsRefreshErrorsFlow.update {
it.toMutableMap().apply {
remove(subsEntry.subsItem.id)
}
}
}
} catch (e: Exception) {
subsRefreshErrorsFlow.update {
it.toMutableMap().apply {
remove(subsEntry.subsItem.id)
set(subsEntry.subsItem.id, e)
}
}
LogUtils.d("检测更新失败", e)
}
} catch (e: Exception) {
subsRefreshErrorsFlow.update {
it.toMutableMap().apply {
set(subsEntry.subsItem.id, e)
}
}
LogUtils.d("检测更新失败", e)
}
}
if (showToast) {
if (successNum > 0) {
toast("更新 $successNum 条订阅")
} else {
toast("暂无更新")
if (showToast) {
if (successNum > 0) {
toast("更新 $successNum 条订阅")
} else {
toast("暂无更新")
}
}
LogUtils.d("结束检测更新")
delay(500)
} finally {
subsRefreshingFlow.value = false
}
LogUtils.d("结束检测更新")
delay(500)
subsRefreshingFlow.value = false
}
}

0 comments on commit 9b4ba5b

Please sign in to comment.