|
| 1 | +package br.com.dillmann.fireflycompanion.android.core.components.action |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.* |
| 4 | +import androidx.compose.foundation.rememberScrollState |
| 5 | +import androidx.compose.foundation.verticalScroll |
| 6 | +import androidx.compose.material3.* |
| 7 | +import androidx.compose.runtime.* |
| 8 | +import androidx.compose.ui.Alignment |
| 9 | +import androidx.compose.ui.Modifier |
| 10 | +import androidx.compose.ui.unit.dp |
| 11 | +import androidx.compose.ui.window.Dialog |
| 12 | +import br.com.dillmann.fireflycompanion.android.R |
| 13 | +import br.com.dillmann.fireflycompanion.android.core.compose.async |
| 14 | +import br.com.dillmann.fireflycompanion.android.core.compose.emptyVolatile |
| 15 | +import br.com.dillmann.fireflycompanion.android.core.compose.persistent |
| 16 | +import br.com.dillmann.fireflycompanion.android.core.compose.volatile |
| 17 | +import br.com.dillmann.fireflycompanion.android.core.i18n.i18n |
| 18 | +import br.com.dillmann.fireflycompanion.android.core.queue.ActionQueue |
| 19 | +import br.com.dillmann.fireflycompanion.core.validation.ConsistencyException |
| 20 | +import br.com.dillmann.fireflycompanion.thirdparty.firefly.infrastructure.ClientError |
| 21 | +import br.com.dillmann.fireflycompanion.thirdparty.firefly.infrastructure.ClientException |
| 22 | +import br.com.dillmann.fireflycompanion.thirdparty.firefly.infrastructure.ServerError |
| 23 | + |
| 24 | +typealias OnComplete = () -> Unit |
| 25 | +typealias OnError = (Exception) -> Unit |
| 26 | +typealias OnViolation = (ConsistencyException) -> Unit |
| 27 | +typealias AsyncActionTask = suspend () -> Unit |
| 28 | + |
| 29 | +@Composable |
| 30 | +fun AsyncAction( |
| 31 | + sink: AsyncActionSink, |
| 32 | + waitMessage: String = i18n(R.string.loading), |
| 33 | + onComplete: OnComplete? = null, |
| 34 | + onError: OnError? = null, |
| 35 | + onViolation: OnViolation? = null, |
| 36 | +) { |
| 37 | + val queue by persistent(ActionQueue()) |
| 38 | + var loading by volatile(false) |
| 39 | + var exception by emptyVolatile<Exception>() |
| 40 | + var lastTask by emptyVolatile<AsyncActionTask>() |
| 41 | + |
| 42 | + suspend fun launch(task: AsyncActionTask) { |
| 43 | + try { |
| 44 | + task() |
| 45 | + onComplete?.invoke() |
| 46 | + } catch (e: ConsistencyException) { |
| 47 | + onViolation?.invoke(e) |
| 48 | + } catch (ex: Exception) { |
| 49 | + onError?.invoke(ex) |
| 50 | + exception = ex |
| 51 | + } finally { |
| 52 | + loading = false |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + LaunchedEffect(Unit) { |
| 57 | + sink.attach { task -> |
| 58 | + loading = true |
| 59 | + lastTask = task |
| 60 | + |
| 61 | + queue.add { |
| 62 | + launch(task) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + DisposableEffect(Unit) { |
| 68 | + onDispose { |
| 69 | + sink.detach() |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (loading) { |
| 74 | + Dialog(onDismissRequest = {}) { |
| 75 | + Column( |
| 76 | + modifier = Modifier |
| 77 | + .padding(16.dp) |
| 78 | + .fillMaxWidth(), |
| 79 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 80 | + verticalArrangement = Arrangement.Center |
| 81 | + ) { |
| 82 | + CircularProgressIndicator() |
| 83 | + Spacer(modifier = Modifier.height(16.dp)) |
| 84 | + Text(text = waitMessage) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (exception != null) { |
| 90 | + var showDetails by volatile(false) |
| 91 | + |
| 92 | + AlertDialog( |
| 93 | + onDismissRequest = { exception = null }, |
| 94 | + title = { |
| 95 | + Text(text = i18n(R.string.error_action_failed)) |
| 96 | + }, |
| 97 | + text = { |
| 98 | + ErrorDetails(showDetails, exception!!) |
| 99 | + }, |
| 100 | + confirmButton = { |
| 101 | + Button( |
| 102 | + onClick = { |
| 103 | + exception = null |
| 104 | + loading = true |
| 105 | + async { launch(lastTask!!) } |
| 106 | + } |
| 107 | + ) { |
| 108 | + Text( |
| 109 | + text = i18n(R.string.try_again), |
| 110 | + ) |
| 111 | + } |
| 112 | + }, |
| 113 | + dismissButton = { |
| 114 | + TextButton( |
| 115 | + onClick = { showDetails = !showDetails }, |
| 116 | + ) { |
| 117 | + Text( |
| 118 | + text = |
| 119 | + if (showDetails) i18n(R.string.hide_details) |
| 120 | + else i18n(R.string.show_details), |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +@Composable |
| 129 | +private fun ErrorDetails( |
| 130 | + techDetailsVisible: Boolean, |
| 131 | + exception: Exception, |
| 132 | +) { |
| 133 | + Column(modifier = Modifier.fillMaxWidth()) { |
| 134 | + Text(text = exception.message ?: i18n(R.string.error_unexpected)) |
| 135 | + |
| 136 | + if (!techDetailsVisible) |
| 137 | + return@Column |
| 138 | + |
| 139 | + val scroll = rememberScrollState() |
| 140 | + |
| 141 | + Column( |
| 142 | + modifier = Modifier |
| 143 | + .fillMaxHeight() |
| 144 | + .verticalScroll(scroll), |
| 145 | + ) { |
| 146 | + if (exception is ClientException) { |
| 147 | + val response = exception.response |
| 148 | + val responseBody = |
| 149 | + when (response) { |
| 150 | + is ServerError<*> -> response.body?.toString() |
| 151 | + is ClientError<*> -> response.body?.toString() |
| 152 | + else -> null |
| 153 | + } |
| 154 | + |
| 155 | + if (responseBody != null) { |
| 156 | + Spacer(modifier = Modifier.height(12.dp)) |
| 157 | + |
| 158 | + Text( |
| 159 | + text = responseBody, |
| 160 | + ) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + Spacer(modifier = Modifier.height(12.dp)) |
| 165 | + |
| 166 | + Text( |
| 167 | + text = exception.stackTraceToString(), |
| 168 | + ) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments