Rewrite ExceptionInfoFragment into Compose (#485)

* Migrated dialog to Compose

Signed-off-by: Arnau Mora <arnyminerz@proton.me>

* Added extended compose icons

Signed-off-by: Arnau Mora <arnyminerz@proton.me>

---------

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
This commit is contained in:
Arnau Mora 2023-12-09 12:24:36 +01:00 committed by GitHub
parent f2f40049b8
commit 8a0e1151ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 19 deletions

View file

@ -161,6 +161,7 @@ dependencies {
// Jetpack Compose
implementation platform("androidx.compose:compose-bom:${versions.composeBom}")
implementation 'androidx.compose.material:material'
implementation 'androidx.compose.material:material-icons-extended'
implementation 'androidx.compose.runtime:runtime-livedata'
debugImplementation 'androidx.compose.ui:ui-tooling'
implementation 'androidx.compose.ui:ui-tooling-preview'

View file

@ -7,10 +7,27 @@ package at.bitfire.davdroid.ui
import android.accounts.Account
import android.app.Dialog
import android.os.Bundle
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Error
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.os.BundleCompat
import androidx.fragment.app.DialogFragment
import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.davdroid.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.accompanist.themeadapter.material.MdcTheme
import java.io.IOException
class ExceptionInfoFragment: DialogFragment() {
@ -32,29 +49,74 @@ class ExceptionInfoFragment: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val args = requireNotNull(arguments)
val exception = args.getSerializable(ARG_EXCEPTION) as Exception
val account: Account? = args.getParcelable(ARG_ACCOUNT)
val account: Account? = BundleCompat.getParcelable(args, ARG_ACCOUNT, Account::class.java)
val title = when (exception) {
is HttpException -> R.string.exception_httpexception
is IOException -> R.string.exception_ioexception
else -> R.string.exception
val dialog = Dialog(requireContext()).apply {
setContentView(
ComposeView(requireContext()).apply {
setContent {
MdcTheme {
ExceptionInfoDialog(
account, exception
) { dismiss() }
}
}
}
)
}
val dialog = MaterialAlertDialogBuilder(requireActivity())
.setIcon(R.drawable.ic_error)
.setTitle(title)
.setMessage(exception::class.java.name + "\n" + exception.localizedMessage)
.setNegativeButton(R.string.exception_show_details) { _, _ ->
val intent = DebugInfoActivity.IntentBuilder(requireActivity())
.withAccount(account)
.withCause(exception)
.build()
startActivity(intent)
}
.setPositiveButton(android.R.string.ok) { _, _ -> }
.create()
isCancelable = false
return dialog
}
}
@Composable
fun ExceptionInfoDialog(
account: Account?,
exception: Throwable,
onDismissRequest: () -> Unit
) {
val context = LocalContext.current
val titleRes = when (exception) {
is HttpException -> R.string.exception_httpexception
is IOException -> R.string.exception_ioexception
else -> R.string.exception
}
AlertDialog(
onDismissRequest = onDismissRequest,
title = {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Icon(Icons.Rounded.Error, null)
Text(
text = stringResource(titleRes),
modifier = Modifier.weight(1f).padding(start = 8.dp)
)
}
},
text = { Text(exception::class.java.name + "\n" + exception.localizedMessage) },
dismissButton = {
TextButton(
onClick = {
val intent = DebugInfoActivity.IntentBuilder(context)
.withAccount(account)
.withCause(exception)
.build()
context.startActivity(intent)
}
) {
Text(stringResource(R.string.exception_show_details).uppercase())
}
},
confirmButton = {
TextButton(onClick = onDismissRequest) {
Text(stringResource(android.R.string.ok).uppercase())
}
}
)
}