Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 8a0e1151 authored by Arnau Mora's avatar Arnau Mora Committed by GitHub
Browse files

Rewrite ExceptionInfoFragment into Compose (#485)



* Migrated dialog to Compose

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

* Added extended compose icons

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

---------

Signed-off-by: default avatarArnau Mora <arnyminerz@proton.me>
parent f2f40049
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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'
+81 −19
Original line number Diff line number Diff line
@@ -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) {
        val dialog = Dialog(requireContext()).apply {
            setContentView(
                ComposeView(requireContext()).apply {
                    setContent {
                        MdcTheme {
                            ExceptionInfoDialog(
                                account, exception
                            ) { dismiss() }
                        }
                    }
                }
            )
        }

        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
    }

        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())
    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()
                    startActivity(intent)
                    context.startActivity(intent)
                }
                .setPositiveButton(android.R.string.ok) { _, _ -> }
                .create()
        isCancelable = false
        return dialog
            ) {
                Text(stringResource(R.string.exception_show_details).uppercase())
            }

        },
        confirmButton = {
            TextButton(onClick = onDismissRequest) {
                Text(stringResource(android.R.string.ok).uppercase())
            }
        }
    )
}