fix: add try catch in some places

This commit is contained in:
arm64v8a 2023-05-28 11:31:54 +09:00
parent 0b0c6577b5
commit a18bbc3b22
5 changed files with 33 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package io.nekohasekai.sagernet.ktx package io.nekohasekai.sagernet.ktx
import android.app.Activity
import android.content.Context import android.content.Context
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -14,3 +15,14 @@ fun Context.alert(text: String): AlertDialog {
} }
fun Fragment.alert(text: String) = requireContext().alert(text) fun Fragment.alert(text: String) = requireContext().alert(text)
fun AlertDialog.tryToShow() {
try {
val activity = context as Activity
if (!activity.isFinishing) {
show()
}
} catch (e: Exception) {
Logs.e(e)
}
}

View File

@ -231,7 +231,7 @@ class AssetsActivity : ThemedActivity() {
updateAsset(file, versionFile, localVersion) updateAsset(file, versionFile, localVersion)
}.onFailure { }.onFailure {
onMainDispatcher { onMainDispatcher {
alert(it.readableMessage).show() alert(it.readableMessage).tryToShow()
} }
} }

View File

@ -236,7 +236,7 @@ class BackupFragment : NamedFragment(R.layout.layout_backup) {
}.onFailure { }.onFailure {
Logs.w(it) Logs.w(it)
onMainDispatcher { onMainDispatcher {
alert(it.readableMessage).show() alert(it.readableMessage).tryToShow()
} }
} }

View File

@ -91,7 +91,12 @@ class ConfigurationFragment @JvmOverloads constructor(
val alwaysShowAddress by lazy { DataStore.alwaysShowAddress } val alwaysShowAddress by lazy { DataStore.alwaysShowAddress }
fun getCurrentGroupFragment(): GroupFragment? { fun getCurrentGroupFragment(): GroupFragment? {
return childFragmentManager.findFragmentByTag("f" + DataStore.selectedGroup) as GroupFragment? return try {
childFragmentManager.findFragmentByTag("f" + DataStore.selectedGroup) as GroupFragment?
} catch (e: Exception) {
Logs.e(e)
null
}
} }
val updateSelectedCallback = object : ViewPager2.OnPageChangeCallback() { val updateSelectedCallback = object : ViewPager2.OnPageChangeCallback() {
@ -1534,7 +1539,7 @@ class ConfigurationFragment @JvmOverloads constructor(
val msg = Protocols.genFriendlyMsg(err) val msg = Protocols.genFriendlyMsg(err)
profileStatus.text = if (msg != err) msg else getString(R.string.unavailable) profileStatus.text = if (msg != err) msg else getString(R.string.unavailable)
profileStatus.setOnClickListener { profileStatus.setOnClickListener {
alert(err).show() alert(err).tryToShow()
} }
} else { } else {
profileStatus.setOnClickListener(null) profileStatus.setOnClickListener(null)

View File

@ -111,6 +111,7 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
R.id.action_new_group -> { R.id.action_new_group -> {
startActivity(Intent(context, GroupSettingsActivity::class.java)) startActivity(Intent(context, GroupSettingsActivity::class.java))
} }
R.id.action_update_all -> { R.id.action_update_all -> {
MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.confirm) MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.confirm)
.setMessage(R.string.update_all_subscription) .setMessage(R.string.update_all_subscription)
@ -339,9 +340,11 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
proxyGroup.toUniversalLink(), proxyGroup.displayName() proxyGroup.toUniversalLink(), proxyGroup.displayName()
).showAllowingStateLoss(parentFragmentManager) ).showAllowingStateLoss(parentFragmentManager)
} }
R.id.action_universal_clipboard -> { R.id.action_universal_clipboard -> {
export(proxyGroup.toUniversalLink()) export(proxyGroup.toUniversalLink())
} }
R.id.action_export_clipboard -> { R.id.action_export_clipboard -> {
runOnDefaultDispatcher { runOnDefaultDispatcher {
val profiles = SagerDatabase.proxyDao.getByGroup(selectedGroup.id) val profiles = SagerDatabase.proxyDao.getByGroup(selectedGroup.id)
@ -352,9 +355,11 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
} }
} }
} }
R.id.action_export_file -> { R.id.action_export_file -> {
startFilesForResult(exportProfiles, "profiles_${proxyGroup.displayName()}.txt") startFilesForResult(exportProfiles, "profiles_${proxyGroup.displayName()}.txt")
} }
R.id.action_clear -> { R.id.action_clear -> {
MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.confirm) MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.confirm)
.setMessage(R.string.clear_profiles_message) .setMessage(R.string.clear_profiles_message)
@ -436,17 +441,17 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
if (subscription != null && subscription.bytesUsed > 0L) { // SIP008 & Open Online Config if (subscription != null && subscription.bytesUsed > 0L) { // SIP008 & Open Online Config
groupTraffic.isVisible = true groupTraffic.isVisible = true
groupTraffic.text = if (subscription.bytesRemaining > 0L) { groupTraffic.text = if (subscription.bytesRemaining > 0L) {
getString( app.getString(
R.string.subscription_traffic, Formatter.formatFileSize( R.string.subscription_traffic, Formatter.formatFileSize(
context, subscription.bytesUsed app, subscription.bytesUsed
), Formatter.formatFileSize( ), Formatter.formatFileSize(
context, subscription.bytesRemaining app, subscription.bytesRemaining
) )
) )
} else { } else {
getString( app.getString(
R.string.subscription_used, Formatter.formatFileSize( R.string.subscription_used, Formatter.formatFileSize(
context, subscription.bytesUsed app, subscription.bytesUsed
) )
) )
} }
@ -506,6 +511,7 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
groupStatus.text = getString(R.string.group_status_proxies, size) groupStatus.text = getString(R.string.group_status_proxies, size)
} }
} }
GroupType.SUBSCRIPTION -> { GroupType.SUBSCRIPTION -> {
groupStatus.text = if (size == 0L) { groupStatus.text = if (size == 0L) {
getString(R.string.group_status_empty_subscription) getString(R.string.group_status_empty_subscription)