taler-android

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

ShareButton.kt (3019B)


      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.compose
     18 
     19 import android.content.Intent
     20 import android.content.Intent.ACTION_SEND
     21 import android.content.Intent.EXTRA_TEXT
     22 import android.graphics.Bitmap
     23 import androidx.compose.foundation.layout.Spacer
     24 import androidx.compose.foundation.layout.size
     25 import androidx.compose.material.icons.Icons
     26 import androidx.compose.material.icons.filled.Share
     27 import androidx.compose.material3.Button
     28 import androidx.compose.material3.ButtonColors
     29 import androidx.compose.material3.ButtonDefaults
     30 import androidx.compose.material3.Icon
     31 import androidx.compose.material3.Text
     32 import androidx.compose.runtime.Composable
     33 import androidx.compose.runtime.rememberCoroutineScope
     34 import androidx.compose.ui.Modifier
     35 import androidx.compose.ui.platform.LocalContext
     36 import androidx.compose.ui.res.stringResource
     37 import androidx.core.content.ContextCompat.startActivity
     38 import kotlinx.coroutines.launch
     39 import net.taler.lib.android.shareAsQrCode
     40 import net.taler.wallet.BuildConfig
     41 import net.taler.wallet.R
     42 
     43 @Composable
     44 fun ShareButton(
     45     content: String,
     46     modifier: Modifier = Modifier,
     47     buttonText: String = stringResource(R.string.share),
     48     colors: ButtonColors = ButtonDefaults.buttonColors(),
     49     shareAsQrCode: Boolean = false,
     50     qrBitmap: Bitmap? = null,
     51 ) {
     52     val context = LocalContext.current
     53     val scope = rememberCoroutineScope()
     54     Button(
     55         modifier = modifier,
     56         colors = colors,
     57         onClick = {
     58             if (shareAsQrCode) {
     59                 scope.launch { content.shareAsQrCode(
     60                     context,
     61                     "${BuildConfig.APPLICATION_ID}.fileprovider",
     62                     qrBitmap,
     63                 ) }
     64             } else {
     65                 val sendIntent: Intent = Intent().apply {
     66                     action = ACTION_SEND
     67                     putExtra(EXTRA_TEXT, content)
     68                     type = "text/plain"
     69                 }
     70                 val shareIntent = Intent.createChooser(sendIntent, null)
     71                 startActivity(context, shareIntent, null)
     72             }
     73         },
     74     ) {
     75         Icon(
     76             Icons.Default.Share,
     77             buttonText,
     78             modifier = Modifier.size(ButtonDefaults.IconSize),
     79         )
     80         Spacer(Modifier.size(ButtonDefaults.IconSpacing))
     81         Text(buttonText)
     82     }
     83 }