Skip to content

Commit 90f89de

Browse files
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.
1 parent 9612b86 commit 90f89de

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import io.reactivex.rxjava3.core.Observable
4646
import kotlinx.coroutines.Dispatchers
4747
import kotlinx.coroutines.delay
4848
import kotlinx.coroutines.launch
49+
import kotlinx.coroutines.withContext
4950
import me.drakeet.support.toast.ToastCompat
5051
import java.util.concurrent.TimeUnit
5152

@@ -489,30 +490,34 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
489490
}
490491

491492
private fun importBatchConfig(server: String?) {
492-
// val dialog = AlertDialog.Builder(this)
493-
// .setView(LayoutProgressBinding.inflate(layoutInflater).root)
494-
// .setCancelable(false)
495-
// .show()
496493
binding.pbWaiting.show()
497494

498495
lifecycleScope.launch(Dispatchers.IO) {
499-
val (count, countSub) = AngConfigManager.importBatchConfig(server, mainViewModel.subscriptionId, true)
500-
delay(500L)
501-
launch(Dispatchers.Main) {
502-
if (count > 0) {
503-
toast(R.string.toast_success)
504-
mainViewModel.reloadServerList()
505-
} else if (countSub > 0) {
506-
initGroupTab()
507-
} else {
496+
try {
497+
val (count, countSub) = AngConfigManager.importBatchConfig(server, mainViewModel.subscriptionId, true)
498+
delay(500L)
499+
withContext(Dispatchers.Main) {
500+
when {
501+
count > 0 -> {
502+
toast(R.string.toast_success)
503+
mainViewModel.reloadServerList()
504+
}
505+
countSub > 0 -> initGroupTab()
506+
else -> toast(R.string.toast_failure)
507+
}
508+
binding.pbWaiting.hide()
509+
}
510+
} catch (e: Exception) {
511+
withContext(Dispatchers.Main) {
508512
toast(R.string.toast_failure)
513+
binding.pbWaiting.hide()
509514
}
510-
//dialog.dismiss()
511-
binding.pbWaiting.hide()
515+
e.printStackTrace()
512516
}
513517
}
514518
}
515519

520+
516521
private fun importConfigCustomClipboard()
517522
: Boolean {
518523
try {

0 commit comments

Comments
 (0)