taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

ErrorTransactionComposable.kt (4106B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2023 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.transactions
     18 
     19 import androidx.compose.foundation.layout.Column
     20 import androidx.compose.foundation.layout.Spacer
     21 import androidx.compose.foundation.layout.size
     22 import androidx.compose.foundation.rememberScrollState
     23 import androidx.compose.foundation.verticalScroll
     24 import androidx.compose.material.icons.Icons
     25 import androidx.compose.material.icons.filled.Error
     26 import androidx.compose.material3.AlertDialog
     27 import androidx.compose.material3.Button
     28 import androidx.compose.material3.ButtonDefaults
     29 import androidx.compose.material3.Icon
     30 import androidx.compose.material3.MaterialTheme
     31 import androidx.compose.material3.Text
     32 import androidx.compose.material3.TextButton
     33 import androidx.compose.runtime.Composable
     34 import androidx.compose.runtime.mutableStateOf
     35 import androidx.compose.runtime.remember
     36 import androidx.compose.ui.Modifier
     37 import androidx.compose.ui.platform.LocalContext
     38 import androidx.compose.ui.res.stringResource
     39 import androidx.compose.ui.text.font.FontFamily
     40 import androidx.compose.ui.unit.sp
     41 import kotlinx.serialization.json.Json
     42 import net.taler.lib.android.copyToClipBoard
     43 import net.taler.wallet.R
     44 import net.taler.wallet.backend.TalerErrorInfo
     45 
     46 @Composable
     47 fun ErrorTransactionButton(
     48     modifier: Modifier = Modifier,
     49     error: TalerErrorInfo,
     50 ) {
     51     val showDialog = remember { mutableStateOf(false) }
     52 
     53     if (showDialog.value) {
     54         @Suppress("OPT_IN_USAGE")
     55         val json = Json {
     56             prettyPrint = true
     57             prettyPrintIndent = "  "
     58         }
     59         val message = json.encodeToString(error)
     60         AlertDialog(
     61             onDismissRequest = {
     62                 showDialog.value = false
     63             },
     64             title = {
     65                 Text(stringResource(R.string.nav_error))
     66             },
     67             text = {
     68                 val scrollState = rememberScrollState()
     69                 Column(
     70                     modifier = Modifier.verticalScroll(scrollState),
     71                 ) {
     72                     Text(
     73                         fontFamily = FontFamily.Monospace,
     74                         fontSize = 10.sp,
     75                         text = message,
     76                     )
     77                 }
     78             },
     79             dismissButton = {
     80                 TextButton(onClick = {
     81                     showDialog.value = false
     82                 }) {
     83                     Text(stringResource(R.string.close))
     84                 }
     85             },
     86             confirmButton = {
     87                 val context = LocalContext.current
     88                 val navError = stringResource(R.string.nav_error)
     89                 TextButton(onClick = {
     90                     copyToClipBoard(context, navError, message)
     91                 }) {
     92                     Text(stringResource(R.string.copy))
     93                 }
     94             })
     95     }
     96 
     97     Button(
     98         modifier = modifier,
     99         colors = ButtonDefaults.buttonColors(
    100             contentColor = MaterialTheme.colorScheme.onError,
    101             containerColor = MaterialTheme.colorScheme.error,
    102         ),
    103         onClick = {
    104             showDialog.value = true
    105         }
    106     ) {
    107         val label = stringResource(R.string.nav_error)
    108         Icon(
    109             imageVector = Icons.Default.Error,
    110             contentDescription = label,
    111             modifier = Modifier.size(ButtonDefaults.IconSize),
    112         )
    113         Spacer(Modifier.size(ButtonDefaults.IconSpacing))
    114         Text(label)
    115     }
    116 }