From a899d7f44411d8c16671b45206250055435df707 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 2 Aug 2026 14:42:00 +0800 Subject: [PATCH] Automated testing takes a long time. If enabled, the task will run in the background; otherwise, it will run on the current page. https://github.com/2dust/v2rayNG/issues/5999 --- .../ui/subscription/SubscriptionsViewModel.kt | 44 +++++++++++++++++++ .../src/main/res/values-zh-rCN/strings.xml | 2 +- .../src/main/res/values-zh-rTW/strings.xml | 2 +- V2rayNG/app/src/main/res/values/strings.xml | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/subscription/SubscriptionsViewModel.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/subscription/SubscriptionsViewModel.kt index 1587959d..889daf30 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/subscription/SubscriptionsViewModel.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/subscription/SubscriptionsViewModel.kt @@ -7,14 +7,19 @@ import com.v2ray.ang.dto.SubscriptionUpdateMessage import com.v2ray.ang.dto.entities.SubscriptionCache import com.v2ray.ang.dto.entities.SubscriptionItem import com.v2ray.ang.extension.moveItem +import com.v2ray.ang.handler.AngConfigManager import com.v2ray.ang.handler.MmkvManager import com.v2ray.ang.handler.SettingsChangeManager import com.v2ray.ang.handler.SettingsManager import com.v2ray.ang.helper.MessageHelper import com.v2ray.ang.ui.base.BaseViewModel +import com.v2ray.ang.util.LogUtil +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.withContext class SubscriptionsViewModel(application: Application) : BaseViewModel(application) { private val subscriptions: MutableList = @@ -59,6 +64,45 @@ class SubscriptionsViewModel(application: Application) : BaseViewModel(applicati } fun updateSubscriptions() { + val updateSubscription = MmkvManager.decodeSettingsBool(AppConfig.PREF_UPDATE_SUBSCRIPTION, false) + val autoTestAfterUpdateSubscription = MmkvManager.decodeSettingsBool(AppConfig.PREF_AUTO_TEST_AFTER_UPDATE_SUBSCRIPTION, false) + + when { + // If auto test is enabled, trigger background service for long-running task + autoTestAfterUpdateSubscription -> updateSubscriptionsMore() + // If only update is enabled, perform local update with UI loading state + updateSubscription -> updateSubscriptionsOnly() + } + } + + fun updateSubscriptionsOnly() { + launchLoading { + try { + val result = withContext(Dispatchers.IO) { + AngConfigManager.updateConfigViaSubAll() + } + + when { + result.successCount + result.failureCount + result.skipCount == 0 -> + toast(R.string.title_update_subscription_no_subscription) + + result.successCount > 0 && result.failureCount + result.skipCount == 0 -> + toast(getString(R.string.title_update_config_count, result.configCount)) + + else -> + toast(getString(R.string.title_update_subscription_result, result.configCount, result.successCount, result.failureCount, result.skipCount)) + } + reload() + } catch (cancelled: CancellationException) { + throw cancelled + } catch (e: Exception) { + LogUtil.e(AppConfig.TAG, "Subscription update failed", e) + toastError(R.string.toast_failure) + } + } + } + + fun updateSubscriptionsMore() { SettingsChangeManager.makeSetupGroupTab() val subIds = MmkvManager.decodeSubscriptions() .filter { it.subscription.enabled && it.subscription.url.isNotEmpty() } diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml index 82075c63..94a96249 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -171,7 +171,7 @@ 任务将在后台运行,在通知中会显示进度信息 更新订阅后自动测试 - 自动测试需要比较长的时间,请耐心等待 + 自动测试需要比较长的时间,开启后任务将在后台运行;不开启则在当前页面运行 测试后自动删除无效配置 测试结果可能不准确且已删除的配置无法恢复 测试后自动排序 diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml index a2efafcc..783d6b52 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -170,7 +170,7 @@ 任務將在背景運行,在通知中會顯示進度資訊 更新訂閱後自動測試 - 自動測試需要比較長的時間,請耐心等待 + 自動測試需要較長的時間,開啟後任務將在後台運行;不開啟則在目前頁面運行 測試後自動刪除無效配置 測試結果可能不準確且已刪除的配置無法復原 測試後自動排序 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index b8c8b9a4..3c184a68 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -176,7 +176,7 @@ The task will run in the background, and progress information will be displayed in the notification. Auto test after updating subscription - Automated testing takes a relatively long time, please be patient. + Automated testing takes a long time. If enabled, the task will run in the background; otherwise, it will run on the current page. Auto delete invalid config after testing Test results may not be accurate; deleted config cannot be recovered. Auto sort after testing