mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-19 06:30:05 +08:00
fix: add try catch in some places
This commit is contained in:
parent
0b0c6577b5
commit
a18bbc3b22
@ -1,5 +1,6 @@
|
||||
package io.nekohasekai.sagernet.ktx
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.Fragment
|
||||
@ -13,4 +14,15 @@ fun Context.alert(text: String): AlertDialog {
|
||||
.create()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,7 +231,7 @@ class AssetsActivity : ThemedActivity() {
|
||||
updateAsset(file, versionFile, localVersion)
|
||||
}.onFailure {
|
||||
onMainDispatcher {
|
||||
alert(it.readableMessage).show()
|
||||
alert(it.readableMessage).tryToShow()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -236,7 +236,7 @@ class BackupFragment : NamedFragment(R.layout.layout_backup) {
|
||||
}.onFailure {
|
||||
Logs.w(it)
|
||||
onMainDispatcher {
|
||||
alert(it.readableMessage).show()
|
||||
alert(it.readableMessage).tryToShow()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -91,7 +91,12 @@ class ConfigurationFragment @JvmOverloads constructor(
|
||||
val alwaysShowAddress by lazy { DataStore.alwaysShowAddress }
|
||||
|
||||
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() {
|
||||
@ -1534,7 +1539,7 @@ class ConfigurationFragment @JvmOverloads constructor(
|
||||
val msg = Protocols.genFriendlyMsg(err)
|
||||
profileStatus.text = if (msg != err) msg else getString(R.string.unavailable)
|
||||
profileStatus.setOnClickListener {
|
||||
alert(err).show()
|
||||
alert(err).tryToShow()
|
||||
}
|
||||
} else {
|
||||
profileStatus.setOnClickListener(null)
|
||||
|
||||
@ -111,6 +111,7 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
|
||||
R.id.action_new_group -> {
|
||||
startActivity(Intent(context, GroupSettingsActivity::class.java))
|
||||
}
|
||||
|
||||
R.id.action_update_all -> {
|
||||
MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.confirm)
|
||||
.setMessage(R.string.update_all_subscription)
|
||||
@ -339,9 +340,11 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
|
||||
proxyGroup.toUniversalLink(), proxyGroup.displayName()
|
||||
).showAllowingStateLoss(parentFragmentManager)
|
||||
}
|
||||
|
||||
R.id.action_universal_clipboard -> {
|
||||
export(proxyGroup.toUniversalLink())
|
||||
}
|
||||
|
||||
R.id.action_export_clipboard -> {
|
||||
runOnDefaultDispatcher {
|
||||
val profiles = SagerDatabase.proxyDao.getByGroup(selectedGroup.id)
|
||||
@ -352,9 +355,11 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
R.id.action_export_file -> {
|
||||
startFilesForResult(exportProfiles, "profiles_${proxyGroup.displayName()}.txt")
|
||||
}
|
||||
|
||||
R.id.action_clear -> {
|
||||
MaterialAlertDialogBuilder(requireContext()).setTitle(R.string.confirm)
|
||||
.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
|
||||
groupTraffic.isVisible = true
|
||||
groupTraffic.text = if (subscription.bytesRemaining > 0L) {
|
||||
getString(
|
||||
app.getString(
|
||||
R.string.subscription_traffic, Formatter.formatFileSize(
|
||||
context, subscription.bytesUsed
|
||||
app, subscription.bytesUsed
|
||||
), Formatter.formatFileSize(
|
||||
context, subscription.bytesRemaining
|
||||
app, subscription.bytesRemaining
|
||||
)
|
||||
)
|
||||
} else {
|
||||
getString(
|
||||
app.getString(
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
GroupType.SUBSCRIPTION -> {
|
||||
groupStatus.text = if (size == 0L) {
|
||||
getString(R.string.group_status_empty_subscription)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user