From 90f89de957bfac023d6a339a4df7b399e2636b6b Mon Sep 17 00:00:00 2001 From: Tamim Hossain <132823494+CodeWithTamim@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:58:20 +0600 Subject: [PATCH] Improve Error Handling in Batch Config Import and Progress Bar Management (#3771) Enhanced error handling in the importBatchConfig method by adding a try-catch block. Ensured that the progress bar is hidden in both success and failure cases. Replaced nested if statements with a cleaner when clause for better readability. --- .../kotlin/com/v2ray/ang/ui/MainActivity.kt | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 880a38220..35cbaed3e 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -46,6 +46,7 @@ import io.reactivex.rxjava3.core.Observable import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import me.drakeet.support.toast.ToastCompat import java.util.concurrent.TimeUnit @@ -489,30 +490,34 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } private fun importBatchConfig(server: String?) { -// val dialog = AlertDialog.Builder(this) -// .setView(LayoutProgressBinding.inflate(layoutInflater).root) -// .setCancelable(false) -// .show() binding.pbWaiting.show() lifecycleScope.launch(Dispatchers.IO) { - val (count, countSub) = AngConfigManager.importBatchConfig(server, mainViewModel.subscriptionId, true) - delay(500L) - launch(Dispatchers.Main) { - if (count > 0) { - toast(R.string.toast_success) - mainViewModel.reloadServerList() - } else if (countSub > 0) { - initGroupTab() - } else { + try { + val (count, countSub) = AngConfigManager.importBatchConfig(server, mainViewModel.subscriptionId, true) + delay(500L) + withContext(Dispatchers.Main) { + when { + count > 0 -> { + toast(R.string.toast_success) + mainViewModel.reloadServerList() + } + countSub > 0 -> initGroupTab() + else -> toast(R.string.toast_failure) + } + binding.pbWaiting.hide() + } + } catch (e: Exception) { + withContext(Dispatchers.Main) { toast(R.string.toast_failure) + binding.pbWaiting.hide() } - //dialog.dismiss() - binding.pbWaiting.hide() + e.printStackTrace() } } } + private fun importConfigCustomClipboard() : Boolean { try {