diff --git a/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt b/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt index 8a5cb329a..c823dad8e 100644 --- a/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt +++ b/app/src/main/kotlin/li/songe/gkd/util/SubsState.kt @@ -337,12 +337,15 @@ private fun refreshRawSubsList(items: List) { 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 } } @@ -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 } }