Skip to content

Commit

Permalink
Make text in error dialogs selectable/copyable
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Sep 24, 2024
1 parent 42f7ffd commit 791b86e
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions app/src/main/java/eu/darken/sdmse/common/error/ErrorDialog.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
package eu.darken.sdmse.common.error

import android.app.Activity
import android.util.TypedValue
import androidx.navigation.findNavController
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textview.MaterialTextView
import eu.darken.sdmse.common.R
import eu.darken.sdmse.common.files.WriteException
import eu.darken.sdmse.exclusion.ui.editor.path.PathExclusionEditorOptions
import eu.darken.sdmse.exclusion.ui.editor.path.PathExclusionFragmentArgs

fun Throwable.asErrorDialogBuilder(
activity: Activity
context: Activity
): MaterialAlertDialogBuilder {
val context = activity
return MaterialAlertDialogBuilder(context).apply {
val error = this@asErrorDialogBuilder
val localizedError = error.localized(context)

setTitle(localizedError.label.get(context))
setMessage(localizedError.description.get(context))

val messageView = MaterialTextView(context).apply {
text = localizedError.description.get(context)
setTextAppearance(com.google.android.material.R.style.TextAppearance_MaterialComponents_Caption)
setTextIsSelectable(true)

val paddingHorizontal = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 24f, resources.displayMetrics
).toInt()
val paddingVertical = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 8f, resources.displayMetrics
).toInt()
setPadding(
paddingHorizontal,
paddingVertical,
paddingHorizontal,
0,
)
}
setView(messageView)

if (localizedError.fixAction != null) {
setPositiveButton(
localizedError.fixActionLabel?.get(context) ?: context.getString(android.R.string.ok)
) { _, _ ->
localizedError.fixAction!!.invoke(activity)
localizedError.fixAction!!.invoke(context)
}
setNegativeButton(R.string.general_cancel_action) { _, _ ->
}
Expand All @@ -35,13 +55,13 @@ fun Throwable.asErrorDialogBuilder(
when {
localizedError.infoAction != null -> {
setNeutralButton(R.string.general_show_details_action) { _, _ ->
localizedError.infoAction!!.invoke(activity)
localizedError.infoAction!!.invoke(context)
}
}

this@asErrorDialogBuilder is WriteException && path != null -> {
setNeutralButton(eu.darken.sdmse.R.string.exclusion_create_action) { _, _ ->
activity.findNavController(eu.darken.sdmse.R.id.nav_host).navigate(
context.findNavController(eu.darken.sdmse.R.id.nav_host).navigate(
resId = eu.darken.sdmse.R.id.goToPathExclusionEditor,
args = PathExclusionFragmentArgs(
initial = PathExclusionEditorOptions(targetPath = path!!)
Expand Down

0 comments on commit 791b86e

Please sign in to comment.