Skip to content

Commit

Permalink
Improve Error Handling in Batch Config Import and Progress Bar Manage…
Browse files Browse the repository at this point in the history
…ment (#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.
  • Loading branch information
CodeWithTamim authored Oct 25, 2024
1 parent 9612b86 commit 90f89de
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 90f89de

Please sign in to comment.