DeleteCollectionDialog: handle case that result may contain null value (#697)

This commit is contained in:
Ricki Hirner 2024-04-01 21:10:53 +02:00 committed by GitHub
parent 6d462ea9e4
commit 6b88052e10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -33,7 +33,7 @@ import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Collection import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.ui.widget.ExceptionInfoDialog import at.bitfire.davdroid.ui.widget.ExceptionInfoDialog
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import java.util.Optional import kotlin.jvm.optionals.getOrNull
@Composable @Composable
fun DeleteCollectionDialog( fun DeleteCollectionDialog(
@ -58,17 +58,18 @@ fun DeleteCollectionDialog(
} }
Dialog( Dialog(
properties = DialogProperties(dismissOnClickOutside = properties = DialogProperties(
result != null || // finished with error message dismissOnClickOutside =
!started // not started result != null || // finished with error message
), !started // not started
),
onDismissRequest = ::dismiss onDismissRequest = ::dismiss
) { ) {
Card { Card {
DeleteCollectionDialog_Content( DeleteCollectionDialog_Content(
collection = collection, collection = collection,
started = started, started = started,
result = result, result = result?.getOrNull(),
onDeleteCollection = { onDeleteCollection = {
started = true started = true
model.deleteCollection(collection) model.deleteCollection(collection)
@ -83,7 +84,7 @@ fun DeleteCollectionDialog(
fun DeleteCollectionDialog_Content( fun DeleteCollectionDialog_Content(
collection: Collection, collection: Collection,
started: Boolean, started: Boolean,
result: Optional<Exception>?, result: Exception?,
onDeleteCollection: () -> Unit = {}, onDeleteCollection: () -> Unit = {},
onCancel: () -> Unit = {} onCancel: () -> Unit = {}
) { ) {
@ -100,14 +101,11 @@ fun DeleteCollectionDialog_Content(
) )
when { when {
result != null -> { result != null -> ExceptionInfoDialog(
val exception = result.get() exception = result,
ExceptionInfoDialog( remoteResource = collection.url,
exception = exception, onDismiss = onCancel
remoteResource = collection.url, )
onDismiss = onCancel
)
}
started -> { started -> {
LinearProgressIndicator( LinearProgressIndicator(
@ -202,6 +200,6 @@ fun DeleteCollectionDialog_Preview_Error() {
url = "https://example.com/calendar".toHttpUrl(), url = "https://example.com/calendar".toHttpUrl(),
), ),
started = true, started = true,
result = Optional.of(Exception("Test error")) result = Exception("Test error")
) )
} }