Skip to content

Commit

Permalink
fix: add try catch in some places
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed May 31, 2023
1 parent 00b6254 commit 12ec3c9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
14 changes: 13 additions & 1 deletion app/src/main/java/io/nekohasekai/sagernet/ktx/Dialogs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package io.nekohasekai.sagernet.ktx

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
Expand All @@ -32,4 +33,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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class AssetsActivity : ThemedActivity() {
updateAsset(file, versionFile, localVersion)
}.onFailure {
onMainDispatcher {
alert(it.readableMessage).show()
alert(it.readableMessage).tryToShow()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class BackupFragment : NamedFragment(R.layout.layout_backup) {
}.onFailure {
Logs.w(it)
onMainDispatcher {
alert(it.readableMessage).show()
alert(it.readableMessage).tryToShow()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ class ConfigurationFragment @JvmOverloads constructor(
val securityAdvisory by lazy { DataStore.securityAdvisory }

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() {
Expand Down Expand Up @@ -1737,7 +1742,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)
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/io/nekohasekai/sagernet/ui/GroupFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,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
)
)
}
Expand Down

0 comments on commit 12ec3c9

Please sign in to comment.