diff options
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/withdraw')
11 files changed, 1126 insertions, 476 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt b/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt index 3cd2f49..9983409 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/PromptWithdrawFragment.kt @@ -20,11 +20,14 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ArrayAdapter import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar.LENGTH_LONG +import kotlinx.coroutines.launch import net.taler.common.Amount import net.taler.common.EventObserver import net.taler.common.fadeIn @@ -33,15 +36,21 @@ import net.taler.wallet.MainViewModel import net.taler.wallet.R import net.taler.wallet.cleanExchange import net.taler.wallet.databinding.FragmentPromptWithdrawBinding +import net.taler.wallet.exchanges.ExchangeItem +import net.taler.wallet.exchanges.SelectExchangeDialogFragment import net.taler.wallet.withdraw.WithdrawStatus.Loading import net.taler.wallet.withdraw.WithdrawStatus.ReceivedDetails import net.taler.wallet.withdraw.WithdrawStatus.TosReviewRequired import net.taler.wallet.withdraw.WithdrawStatus.Withdrawing +import net.taler.wallet.withdraw.WithdrawStatus.NeedsExchange class PromptWithdrawFragment : Fragment() { private val model: MainViewModel by activityViewModels() private val withdrawManager by lazy { model.withdrawManager } + private val transactionManager by lazy { model.transactionManager } + + private val selectExchangeDialog = SelectExchangeDialogFragment() private lateinit var ui: FragmentPromptWithdrawBinding @@ -60,22 +69,20 @@ class PromptWithdrawFragment : Fragment() { withdrawManager.withdrawStatus.observe(viewLifecycleOwner) { showWithdrawStatus(it) } - withdrawManager.exchangeSelection.observe(viewLifecycleOwner, EventObserver { - findNavController().navigate(R.id.action_promptWithdraw_to_selectExchangeFragment) + + selectExchangeDialog.exchangeSelection.observe(viewLifecycleOwner, EventObserver { + onExchangeSelected(it) }) } private fun showWithdrawStatus(status: WithdrawStatus?): Any = when (status) { null -> model.showProgressBar.value = false is Loading -> model.showProgressBar.value = true - is WithdrawStatus.NeedsExchange -> { + is NeedsExchange -> { model.showProgressBar.value = false - val exchangeSelection = status.exchangeSelection.getIfNotConsumed() - if (exchangeSelection == null) { // already consumed - findNavController().popBackStack() - } else { - withdrawManager.selectExchange(exchangeSelection) - } + if (selectExchangeDialog.dialog?.isShowing != true) { + selectExchange() + } else {} } is TosReviewRequired -> onTosReviewRequired(status) is ReceivedDetails -> onReceivedDetails(status) @@ -87,9 +94,15 @@ class PromptWithdrawFragment : Fragment() { is WithdrawStatus.Success -> { model.showProgressBar.value = false withdrawManager.withdrawStatus.value = null - findNavController().navigate(R.id.action_promptWithdraw_to_nav_main) - model.showTransactions(status.currency) - Snackbar.make(requireView(), R.string.withdraw_initiated, LENGTH_LONG).show() + lifecycleScope.launch { + // now select new transaction based on currency and ID + if (transactionManager.selectTransaction(status.transactionId)) { + findNavController().navigate(R.id.action_promptWithdraw_to_nav_transactions_detail_withdrawal) + } else { + findNavController().navigate(R.id.action_promptWithdraw_to_nav_main) + } + Snackbar.make(requireView(), R.string.withdraw_initiated, LENGTH_LONG).show() + } } is WithdrawStatus.Error -> { model.showProgressBar.value = false @@ -102,7 +115,13 @@ class PromptWithdrawFragment : Fragment() { if (s.showImmediately.getIfNotConsumed() == true) { findNavController().navigate(R.id.action_promptWithdraw_to_reviewExchangeTOS) } else { - showContent(s.amountRaw, s.amountEffective, s.exchangeBaseUrl, s.talerWithdrawUri) + showContent( + amountRaw = s.amountRaw, + amountEffective = s.amountEffective, + exchange = s.exchangeBaseUrl, + uri = s.talerWithdrawUri, + exchanges = s.possibleExchanges, + ) ui.confirmWithdrawButton.apply { text = getString(R.string.withdraw_button_tos) setOnClickListener { @@ -114,13 +133,24 @@ class PromptWithdrawFragment : Fragment() { } private fun onReceivedDetails(s: ReceivedDetails) { - showContent(s.amountRaw, s.amountEffective, s.exchangeBaseUrl, s.talerWithdrawUri) + showContent( + amountRaw = s.amountRaw, + amountEffective = s.amountEffective, + exchange = s.exchangeBaseUrl, + uri = s.talerWithdrawUri, + ageRestrictionOptions = s.ageRestrictionOptions, + exchanges = s.possibleExchanges, + ) ui.confirmWithdrawButton.apply { text = getString(R.string.withdraw_button_confirm) setOnClickListener { it.fadeOut() ui.confirmProgressBar.fadeIn() - withdrawManager.acceptWithdrawal() + val ageRestrict = (ui.ageSelector.selectedItem as String?)?.let { age -> + if (age == context.getString(R.string.withdraw_restrict_age_unrestricted)) null + else age.toIntOrNull() + } + withdrawManager.acceptWithdrawal(ageRestrict) } isEnabled = true } @@ -131,6 +161,8 @@ class PromptWithdrawFragment : Fragment() { amountEffective: Amount, exchange: String, uri: String?, + exchanges: List<ExchangeItem> = emptyList(), + ageRestrictionOptions: List<Int>? = null, ) { model.showProgressBar.value = false ui.progressBar.fadeOut() @@ -143,24 +175,75 @@ class PromptWithdrawFragment : Fragment() { ui.chosenAmountView.text = amountRaw.toString() ui.chosenAmountView.fadeIn() - ui.feeLabel.fadeIn() - ui.feeView.text = - getString(R.string.amount_negative, (amountRaw - amountEffective).toString()) - ui.feeView.fadeIn() + if (amountRaw > amountEffective) { + val fee = amountRaw - amountEffective + ui.feeLabel.fadeIn() + ui.feeView.text = getString(R.string.amount_negative, fee.toString()) + ui.feeView.fadeIn() + } ui.exchangeIntroView.fadeIn() ui.withdrawExchangeUrl.text = cleanExchange(exchange) ui.withdrawExchangeUrl.fadeIn() - if (uri != null) { // no Uri for manual withdrawals + // no Uri for manual withdrawals, no selection for single exchange + if (uri != null && exchanges.size > 1) { ui.selectExchangeButton.fadeIn() ui.selectExchangeButton.setOnClickListener { - val exchangeSelection = ExchangeSelection(amountRaw, uri) - withdrawManager.selectExchange(exchangeSelection) + selectExchange() } } + if (ageRestrictionOptions != null) { + ui.ageLabel.fadeIn() + val context = requireContext() + val items = listOf(context.getString(R.string.withdraw_restrict_age_unrestricted)) + + ageRestrictionOptions.map { it.toString() } + ui.ageSelector.adapter = ArrayAdapter(context, R.layout.list_item_age, items) + ui.ageSelector.fadeIn() + } + ui.withdrawCard.fadeIn() } + private fun selectExchange() { + val exchanges = when (val status = withdrawManager.withdrawStatus.value) { + is ReceivedDetails -> status.possibleExchanges + is NeedsExchange -> status.possibleExchanges + is TosReviewRequired -> status.possibleExchanges + else -> return + } + selectExchangeDialog.setExchanges(exchanges) + selectExchangeDialog.show(parentFragmentManager, "SELECT_EXCHANGE") + } + + private fun onExchangeSelected(exchange: ExchangeItem) { + val status = withdrawManager.withdrawStatus.value + val amount = when (status) { + is ReceivedDetails -> status.amountRaw + is NeedsExchange -> status.amount + is TosReviewRequired -> status.amountRaw + else -> return + } + val uri = when (status) { + is ReceivedDetails -> status.talerWithdrawUri + is NeedsExchange -> status.talerWithdrawUri + is TosReviewRequired -> status.talerWithdrawUri + else -> return + } + val exchanges = when (status) { + is ReceivedDetails -> status.possibleExchanges + is NeedsExchange -> status.possibleExchanges + is TosReviewRequired -> status.possibleExchanges + else -> return + } + + withdrawManager.getWithdrawalDetails( + exchangeBaseUrl = exchange.exchangeBaseUrl, + amount = amount, + showTosImmediately = false, + uri = uri, + possibleExchanges = exchanges, + ) + } } diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt b/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt new file mode 100644 index 0000000..9bfeda6 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt @@ -0,0 +1,157 @@ +/* + * This file is part of GNU Taler + * (C) 2023 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.withdraw + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import net.taler.common.Amount +import net.taler.common.Timestamp +import net.taler.common.toAbsoluteTime +import net.taler.wallet.R +import net.taler.wallet.backend.TalerErrorCode +import net.taler.wallet.backend.TalerErrorInfo +import net.taler.wallet.cleanExchange +import net.taler.common.CurrencySpecification +import net.taler.wallet.transactions.ActionButton +import net.taler.wallet.transactions.ActionListener +import net.taler.wallet.transactions.AmountType +import net.taler.wallet.transactions.ErrorTransactionButton +import net.taler.wallet.transactions.Transaction +import net.taler.wallet.transactions.TransactionAction +import net.taler.wallet.transactions.TransactionAction.Abort +import net.taler.wallet.transactions.TransactionAction.Retry +import net.taler.wallet.transactions.TransactionAction.Suspend +import net.taler.wallet.transactions.TransactionAmountComposable +import net.taler.wallet.transactions.TransactionInfoComposable +import net.taler.wallet.transactions.TransactionMajorState.Pending +import net.taler.wallet.transactions.TransactionState +import net.taler.wallet.transactions.TransactionWithdrawal +import net.taler.wallet.transactions.TransitionsComposable +import net.taler.wallet.transactions.WithdrawalDetails.ManualTransfer +import net.taler.wallet.transactions.WithdrawalExchangeAccountDetails + +@Composable +fun TransactionWithdrawalComposable( + t: TransactionWithdrawal, + devMode: Boolean, + spec: CurrencySpecification?, + actionListener: ActionListener, + onTransition: (t: TransactionAction) -> Unit, +) { + val scrollState = rememberScrollState() + Column( + modifier = Modifier + .fillMaxWidth() + .verticalScroll(scrollState), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + val context = LocalContext.current + Text( + modifier = Modifier.padding(16.dp), + text = t.timestamp.ms.toAbsoluteTime(context).toString(), + style = MaterialTheme.typography.bodyLarge, + ) + + ActionButton(tx = t, listener = actionListener) + + if (t.amountRaw != t.amountEffective) { + TransactionAmountComposable( + label = stringResource(R.string.amount_chosen), + amount = t.amountRaw.withSpec(spec), + amountType = AmountType.Neutral, + ) + } + + if (t.amountRaw > t.amountEffective) { + val fee = t.amountRaw - t.amountEffective + TransactionAmountComposable( + label = stringResource(id = R.string.amount_fee), + amount = fee.withSpec(spec), + amountType = AmountType.Negative, + ) + } + + TransactionAmountComposable( + label = stringResource(id = R.string.amount_total), + amount = t.amountEffective.withSpec(spec), + amountType = AmountType.Positive, + ) + + TransactionInfoComposable( + label = stringResource(id = R.string.withdraw_exchange), + info = cleanExchange(t.exchangeBaseUrl), + ) + + TransitionsComposable(t, devMode, onTransition) + + if (devMode && t.error != null) { + ErrorTransactionButton(error = t.error) + } + } +} + +@Preview +@Composable +fun TransactionWithdrawalComposablePreview() { + val t = TransactionWithdrawal( + transactionId = "transactionId", + timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000), + txState = TransactionState(Pending), + txActions = listOf(Retry, Suspend, Abort), + exchangeBaseUrl = "https://exchange.demo.taler.net/", + withdrawalDetails = ManualTransfer( + exchangeCreditAccountDetails = listOf( + WithdrawalExchangeAccountDetails( + paytoUri = "payto://IBAN/1231231231", + transferAmount = Amount.fromJSONString("NETZBON:42.23"), + status = WithdrawalExchangeAccountDetails.Status.Ok, + currencySpecification = CurrencySpecification( + name = "NETZBON", + numFractionalInputDigits = 2, + numFractionalNormalDigits = 2, + numFractionalTrailingZeroDigits = 2, + altUnitNames = mapOf(0 to "NETZBON"), + ), + ), + ), + ), + amountRaw = Amount.fromString("TESTKUDOS", "42.23"), + amountEffective = Amount.fromString("TESTKUDOS", "42.1337"), + error = TalerErrorInfo(code = TalerErrorCode.WALLET_WITHDRAWAL_KYC_REQUIRED), + ) + + val listener = object : ActionListener { + override fun onActionButtonClicked(tx: Transaction, type: ActionListener.Type) {} + } + + Surface { + TransactionWithdrawalComposable(t, true, null, listener) {} + } +} diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt index f21fd8c..e308b2a 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt @@ -16,37 +16,47 @@ package net.taler.wallet.withdraw -import Bech32 import android.net.Uri import android.util.Log import androidx.annotation.UiThread -import androidx.lifecycle.LiveData +import androidx.annotation.WorkerThread import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlinx.serialization.Serializable import net.taler.common.Amount +import net.taler.common.Bech32 import net.taler.common.Event import net.taler.common.toEvent import net.taler.wallet.TAG import net.taler.wallet.backend.TalerErrorInfo import net.taler.wallet.backend.WalletBackendApi +import net.taler.wallet.balances.ScopeInfo import net.taler.wallet.exchanges.ExchangeFees import net.taler.wallet.exchanges.ExchangeItem +import net.taler.wallet.transactions.WithdrawalExchangeAccountDetails import net.taler.wallet.withdraw.WithdrawStatus.ReceivedDetails sealed class WithdrawStatus { data class Loading(val talerWithdrawUri: String? = null) : WithdrawStatus() - data class NeedsExchange(val exchangeSelection: Event<ExchangeSelection>) : WithdrawStatus() + + data class NeedsExchange( + val talerWithdrawUri: String, + val amount: Amount, + val possibleExchanges: List<ExchangeItem>, + ) : WithdrawStatus() data class TosReviewRequired( val talerWithdrawUri: String? = null, val exchangeBaseUrl: String, val amountRaw: Amount, val amountEffective: Amount, + val withdrawalAccountList: List<WithdrawalExchangeAccountDetails>, + val ageRestrictionOptions: List<Int>? = null, val tosText: String, val tosEtag: String, val showImmediately: Event<Boolean>, + val possibleExchanges: List<ExchangeItem> = emptyList(), ) : WithdrawStatus() data class ReceivedDetails( @@ -54,37 +64,62 @@ sealed class WithdrawStatus { val exchangeBaseUrl: String, val amountRaw: Amount, val amountEffective: Amount, + val withdrawalAccountList: List<WithdrawalExchangeAccountDetails>, + val ageRestrictionOptions: List<Int>? = null, + val possibleExchanges: List<ExchangeItem> = emptyList(), ) : WithdrawStatus() - object Withdrawing : WithdrawStatus() - data class Success(val currency: String) : WithdrawStatus() - sealed class ManualTransferRequired: WithdrawStatus() { - abstract val uri: Uri - abstract val transactionId: String? - } + data object Withdrawing : WithdrawStatus() - data class ManualTransferRequiredIBAN( - val exchangeBaseUrl: String, - override val uri: Uri, - val iban: String, - val subject: String, - val amountRaw: Amount, - override val transactionId: String?, - ) : ManualTransferRequired() + data class Success(val currency: String, val transactionId: String) : WithdrawStatus() - data class ManualTransferRequiredBitcoin( + class ManualTransferRequired( + val transactionId: String?, + val transactionAmountRaw: Amount, + val transactionAmountEffective: Amount, val exchangeBaseUrl: String, - override val uri: Uri, - val account: String, - val segwitAddrs: List<String>, - val subject: String, - val amountRaw: Amount, - override val transactionId: String?, - ) : ManualTransferRequired() + val withdrawalTransfers: List<TransferData>, + ) : WithdrawStatus() data class Error(val message: String?) : WithdrawStatus() } +sealed class TransferData { + abstract val subject: String + abstract val amountRaw: Amount + abstract val amountEffective: Amount + abstract val withdrawalAccount: WithdrawalExchangeAccountDetails + + val currency get() = withdrawalAccount.transferAmount?.currency + + data class Taler( + override val subject: String, + override val amountRaw: Amount, + override val amountEffective: Amount, + override val withdrawalAccount: WithdrawalExchangeAccountDetails, + val receiverName: String? = null, + val account: String, + ): TransferData() + + data class IBAN( + override val subject: String, + override val amountRaw: Amount, + override val amountEffective: Amount, + override val withdrawalAccount: WithdrawalExchangeAccountDetails, + val receiverName: String? = null, + val iban: String, + ): TransferData() + + data class Bitcoin( + override val subject: String, + override val amountRaw: Amount, + override val amountEffective: Amount, + override val withdrawalAccount: WithdrawalExchangeAccountDetails, + val account: String, + val segwitAddresses: List<String>, + ): TransferData() +} + sealed class WithdrawTestStatus { object Withdrawing : WithdrawTestStatus() object Success : WithdrawTestStatus() @@ -99,20 +134,31 @@ data class WithdrawalDetailsForUri( ) @Serializable -data class WithdrawalDetails( +data class WithdrawExchangeResponse( + val exchangeBaseUrl: String, + val amount: Amount? = null, +) + +@Serializable +data class ManualWithdrawalDetails( val tosAccepted: Boolean, val amountRaw: Amount, val amountEffective: Amount, + val withdrawalAccountsList: List<WithdrawalExchangeAccountDetails>, + val ageRestrictionOptions: List<Int>? = null, + val scopeInfo: ScopeInfo, ) @Serializable -data class AcceptManualWithdrawalResponse( - val exchangePaytoUris: List<String>, +data class AcceptWithdrawalResponse( + val transactionId: String, ) -data class ExchangeSelection( - val amount: Amount, - val talerWithdrawUri: String, +@Serializable +data class AcceptManualWithdrawalResponse( + val reservePub: String, + val withdrawalAccountsList: List<WithdrawalExchangeAccountDetails>, + val transactionId: String, ) class WithdrawManager( @@ -123,8 +169,6 @@ class WithdrawManager( val withdrawStatus = MutableLiveData<WithdrawStatus>() val testWithdrawalStatus = MutableLiveData<WithdrawTestStatus>() - private val _exchangeSelection = MutableLiveData<Event<ExchangeSelection>>() - val exchangeSelection: LiveData<Event<ExchangeSelection>> = _exchangeSelection var exchangeFees: ExchangeFees? = null private set @@ -137,11 +181,6 @@ class WithdrawManager( } } - @UiThread - fun selectExchange(selection: ExchangeSelection) { - _exchangeSelection.value = selection.toEvent() - } - fun getWithdrawalDetails(uri: String) = scope.launch { withdrawStatus.value = WithdrawStatus.Loading(uri) api.request("getWithdrawalDetailsForUri", WithdrawalDetailsForUri.serializer()) { @@ -150,11 +189,18 @@ class WithdrawManager( handleError("getWithdrawalDetailsForUri", error) }.onSuccess { details -> if (details.defaultExchangeBaseUrl == null) { - val exchangeSelection = ExchangeSelection(details.amount, uri) - withdrawStatus.value = WithdrawStatus.NeedsExchange(exchangeSelection.toEvent()) - } else { - getWithdrawalDetails(details.defaultExchangeBaseUrl, details.amount, false, uri) - } + withdrawStatus.value = WithdrawStatus.NeedsExchange( + talerWithdrawUri = uri, + amount = details.amount, + possibleExchanges = details.possibleExchanges, + ) + } else getWithdrawalDetails( + exchangeBaseUrl = details.defaultExchangeBaseUrl, + amount = details.amount, + showTosImmediately = false, + uri = uri, + possibleExchanges = details.possibleExchanges, + ) } } @@ -163,9 +209,10 @@ class WithdrawManager( amount: Amount, showTosImmediately: Boolean = false, uri: String? = null, + possibleExchanges: List<ExchangeItem> = emptyList(), ) = scope.launch { withdrawStatus.value = WithdrawStatus.Loading(uri) - api.request("getWithdrawalDetailsForAmount", WithdrawalDetails.serializer()) { + api.request("getWithdrawalDetailsForAmount", ManualWithdrawalDetails.serializer()) { put("exchangeBaseUrl", exchangeBaseUrl) put("amount", amount.toJSONString()) }.onError { error -> @@ -177,16 +224,34 @@ class WithdrawManager( exchangeBaseUrl = exchangeBaseUrl, amountRaw = details.amountRaw, amountEffective = details.amountEffective, + withdrawalAccountList = details.withdrawalAccountsList, + ageRestrictionOptions = details.ageRestrictionOptions, + possibleExchanges = possibleExchanges, ) - } else getExchangeTos(exchangeBaseUrl, details, showTosImmediately, uri) + } else getExchangeTos(exchangeBaseUrl, details, showTosImmediately, uri, possibleExchanges) + } + } + + @WorkerThread + suspend fun prepareManualWithdrawal(uri: String): WithdrawExchangeResponse? { + withdrawStatus.postValue(WithdrawStatus.Loading(uri)) + var response: WithdrawExchangeResponse? = null + api.request("prepareWithdrawExchange", WithdrawExchangeResponse.serializer()) { + put("talerUri", uri) + }.onError { + handleError("prepareWithdrawExchange", it) + }.onSuccess { + response = it } + return response } private fun getExchangeTos( exchangeBaseUrl: String, - details: WithdrawalDetails, + details: ManualWithdrawalDetails, showImmediately: Boolean, uri: String?, + possibleExchanges: List<ExchangeItem>, ) = scope.launch { api.request("getExchangeTos", TosResponse.serializer()) { put("exchangeBaseUrl", exchangeBaseUrl) @@ -198,9 +263,12 @@ class WithdrawManager( exchangeBaseUrl = exchangeBaseUrl, amountRaw = details.amountRaw, amountEffective = details.amountEffective, + withdrawalAccountList = details.withdrawalAccountsList, + ageRestrictionOptions = details.ageRestrictionOptions, tosText = it.content, tosEtag = it.currentEtag, showImmediately = showImmediately.toEvent(), + possibleExchanges = possibleExchanges, ) } } @@ -221,52 +289,58 @@ class WithdrawManager( exchangeBaseUrl = s.exchangeBaseUrl, amountRaw = s.amountRaw, amountEffective = s.amountEffective, + withdrawalAccountList = s.withdrawalAccountList, + ageRestrictionOptions = s.ageRestrictionOptions, + possibleExchanges = s.possibleExchanges, ) } } @UiThread - fun acceptWithdrawal() = scope.launch { + fun acceptWithdrawal(restrictAge: Int? = null) = scope.launch { val status = withdrawStatus.value as ReceivedDetails withdrawStatus.value = WithdrawStatus.Withdrawing if (status.talerWithdrawUri == null) { - acceptManualWithdrawal(status) + acceptManualWithdrawal(status, restrictAge) } else { - acceptBankIntegratedWithdrawal(status) + acceptBankIntegratedWithdrawal(status, restrictAge) } } - private suspend fun acceptBankIntegratedWithdrawal(status: ReceivedDetails) { - api.request<Unit>("acceptBankIntegratedWithdrawal") { + private suspend fun acceptBankIntegratedWithdrawal( + status: ReceivedDetails, + restrictAge: Int? = null, + ) { + api.request("acceptBankIntegratedWithdrawal", AcceptWithdrawalResponse.serializer()) { + restrictAge?.let { put("restrictAge", restrictAge) } put("exchangeBaseUrl", status.exchangeBaseUrl) put("talerWithdrawUri", status.talerWithdrawUri) }.onError { handleError("acceptBankIntegratedWithdrawal", it) }.onSuccess { - withdrawStatus.value = WithdrawStatus.Success(status.amountRaw.currency) + withdrawStatus.value = + WithdrawStatus.Success(status.amountRaw.currency, it.transactionId) } } - private suspend fun acceptManualWithdrawal(status: ReceivedDetails) { + private suspend fun acceptManualWithdrawal(status: ReceivedDetails, restrictAge: Int? = null) { api.request("acceptManualWithdrawal", AcceptManualWithdrawalResponse.serializer()) { + restrictAge?.let { put("restrictAge", restrictAge) } put("exchangeBaseUrl", status.exchangeBaseUrl) put("amount", status.amountRaw.toJSONString()) }.onError { handleError("acceptManualWithdrawal", it) }.onSuccess { response -> withdrawStatus.value = createManualTransferRequired( - amount = status.amountRaw, - exchangeBaseUrl = status.exchangeBaseUrl, - // TODO what if there's more than one or no URI? - uriStr = response.exchangePaytoUris[0], + status = status, + response = response, ) } } - @UiThread private fun handleError(operation: String, error: TalerErrorInfo) { Log.e(TAG, "Error $operation $error") - withdrawStatus.value = WithdrawStatus.Error(error.userFacingMsg) + withdrawStatus.postValue(WithdrawStatus.Error(error.userFacingMsg)) } /** @@ -281,33 +355,60 @@ class WithdrawManager( } fun createManualTransferRequired( - amount: Amount, + transactionId: String, exchangeBaseUrl: String, - uriStr: String, - transactionId: String? = null, -): WithdrawStatus.ManualTransferRequired { - val uri = Uri.parse(uriStr.replace("receiver-name=", "receiver_name=")) - if ("bitcoin".equals(uri.authority, true)) { - val msg = uri.getQueryParameter("message").orEmpty() - val reg = "\\b([A-Z0-9]{52})\\b".toRegex().find(msg) - val reserve = reg?.value ?: uri.getQueryParameter("subject")!! - val segwitAddrs = Bech32.generateFakeSegwitAddress(reserve, uri.pathSegments.first()) - return WithdrawStatus.ManualTransferRequiredBitcoin( - exchangeBaseUrl = exchangeBaseUrl, - uri = uri, - account = uri.lastPathSegment!!, - segwitAddrs = segwitAddrs, - subject = reserve, - amountRaw = amount, - transactionId = transactionId, - ) - } - return WithdrawStatus.ManualTransferRequiredIBAN( - exchangeBaseUrl = exchangeBaseUrl, - uri = uri, - iban = uri.lastPathSegment!!, - subject = uri.getQueryParameter("subject") ?: "Error: No subject in URI", - amountRaw = amount, - transactionId = transactionId, - ) -} + amountRaw: Amount, + amountEffective: Amount, + withdrawalAccountList: List<WithdrawalExchangeAccountDetails>, +) = WithdrawStatus.ManualTransferRequired( + transactionId = transactionId, + transactionAmountRaw = amountRaw, + transactionAmountEffective = amountEffective, + exchangeBaseUrl = exchangeBaseUrl, + withdrawalTransfers = withdrawalAccountList.mapNotNull { + val uri = Uri.parse(it.paytoUri.replace("receiver-name=", "receiver_name=")) + if ("bitcoin".equals(uri.authority, true)) { + val msg = uri.getQueryParameter("message").orEmpty() + val reg = "\\b([A-Z0-9]{52})\\b".toRegex().find(msg) + val reserve = reg?.value ?: uri.getQueryParameter("subject")!! + val segwitAddresses = Bech32.generateFakeSegwitAddress(reserve, uri.pathSegments.first()) + TransferData.Bitcoin( + account = uri.lastPathSegment!!, + segwitAddresses = segwitAddresses, + subject = reserve, + amountRaw = amountRaw, + amountEffective = amountEffective, + withdrawalAccount = it.copy(paytoUri = uri.toString()) + ) + } else if (uri.authority.equals("x-taler-bank", true)) { + TransferData.Taler( + account = uri.lastPathSegment!!, + receiverName = uri.getQueryParameter("receiver_name"), + subject = uri.getQueryParameter("message") ?: "Error: No message in URI", + amountRaw = amountRaw, + amountEffective = amountEffective, + withdrawalAccount = it.copy(paytoUri = uri.toString()), + ) + } else if (uri.authority.equals("iban", true)) { + TransferData.IBAN( + iban = uri.lastPathSegment!!, + receiverName = uri.getQueryParameter("receiver_name"), + subject = uri.getQueryParameter("message") ?: "Error: No message in URI", + amountRaw = amountRaw, + amountEffective = amountEffective, + withdrawalAccount = it.copy(paytoUri = uri.toString()), + ) + } else null + }, +) + +fun createManualTransferRequired( + status: ReceivedDetails, + response: AcceptManualWithdrawalResponse, +): WithdrawStatus.ManualTransferRequired = createManualTransferRequired( + transactionId = response.transactionId, + exchangeBaseUrl = status.exchangeBaseUrl, + amountRaw = status.amountRaw, + amountEffective = status.amountEffective, + withdrawalAccountList = response.withdrawalAccountsList, +)
\ No newline at end of file diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawFragment.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawFragment.kt index 148b8c0..c499c3b 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawFragment.kt @@ -25,6 +25,7 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController import net.taler.common.Amount +import net.taler.common.AmountParserException import net.taler.common.hideKeyboard import net.taler.wallet.MainViewModel import net.taler.wallet.R @@ -67,19 +68,19 @@ class ManualWithdrawFragment : Fragment() { } private fun onCheckFees() { - if (ui.amountView.text?.isEmpty() != false) { + val currency = exchangeItem.currency + if (currency == null || ui.amountView.text?.isEmpty() != false) { ui.amountLayout.error = getString(R.string.withdraw_amount_error) return } ui.amountLayout.error = null - val value: Double + val amount: Amount try { - value = ui.amountView.text.toString().replace(',', '.').toDouble() - } catch (e: NumberFormatException) { + amount = Amount.fromString(currency, ui.amountView.text.toString()) + } catch (e: AmountParserException) { ui.amountLayout.error = getString(R.string.withdraw_amount_error) return } - val amount = Amount.fromDouble(exchangeItem.currency, value) ui.amountView.hideKeyboard() withdrawManager.getWithdrawalDetails(exchangeItem.exchangeBaseUrl, amount) diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawSuccessFragment.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawSuccessFragment.kt index f019a5b..63413c2 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawSuccessFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ManualWithdrawSuccessFragment.kt @@ -16,63 +16,78 @@ package net.taler.wallet.withdraw.manual -import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.compose.material.Surface +import androidx.appcompat.app.AppCompatActivity import androidx.compose.ui.platform.ComposeView import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController -import com.google.android.material.composethemeadapter.MdcTheme -import net.taler.common.startActivitySafe +import net.taler.common.openUri +import net.taler.common.shareText import net.taler.wallet.MainViewModel import net.taler.wallet.R +import net.taler.wallet.compose.TalerSurface +import net.taler.wallet.withdraw.TransferData import net.taler.wallet.withdraw.WithdrawStatus class ManualWithdrawSuccessFragment : Fragment() { private val model: MainViewModel by activityViewModels() - private val transactionManager by lazy { model.transactionManager } private val withdrawManager by lazy { model.withdrawManager } + private val balanceManager by lazy { model.balanceManager } + + private lateinit var status: WithdrawStatus.ManualTransferRequired + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?, ): View = ComposeView(requireContext()).apply { - val status = withdrawManager.withdrawStatus.value as WithdrawStatus.ManualTransferRequired - val intent = Intent().apply { - data = status.uri - } - // TODO test if this works with an actual payto:// handling app - val componentName = intent.resolveActivity(requireContext().packageManager) - val onBankAppClick = if (componentName == null) null else { - { startActivitySafe(intent) } - } - val tid = status.transactionId - val onCancelClick = if (tid == null) null else { - { - transactionManager.deleteTransaction(tid) - findNavController().navigate(R.id.action_nav_exchange_manual_withdrawal_success_to_nav_main) + status = withdrawManager.withdrawStatus.value as WithdrawStatus.ManualTransferRequired + + // Set action bar subtitle and unset on exit + if (status.withdrawalTransfers.size > 1) { + val activity = requireActivity() as AppCompatActivity + + activity.apply { + supportActionBar?.subtitle = getString(R.string.withdraw_subtitle) } - } - setContent { - MdcTheme { - Surface { - when (status) { - is WithdrawStatus.ManualTransferRequiredBitcoin -> { - ScreenBitcoin(status, onBankAppClick, onCancelClick) - } - is WithdrawStatus.ManualTransferRequiredIBAN -> { - ScreenIBAN(status, onBankAppClick, onCancelClick) - } + + findNavController().addOnDestinationChangedListener { controller, destination, args -> + if (destination.id != R.id.nav_exchange_manual_withdrawal_success) { + activity.apply { + supportActionBar?.subtitle = null } } + } + } + setContent { + TalerSurface { + ScreenTransfer( + status = status, + spec = balanceManager.getSpecForCurrency(status.transactionAmountRaw.currency), + bankAppClick = { onBankAppClick(it) }, + shareClick = { onShareClick(it) }, + ) } } } + private fun onBankAppClick(transfer: TransferData) { + requireContext().openUri( + uri = transfer.withdrawalAccount.paytoUri, + title = requireContext().getString(R.string.share_payment) + ) + } + + private fun onShareClick(transfer: TransferData) { + requireContext().shareText( + text = transfer.withdrawalAccount.paytoUri, + ) + } + override fun onStart() { super.onStart() activity?.setTitle(R.string.withdraw_title) diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenBitcoin.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenBitcoin.kt deleted file mode 100644 index 6820ba0..0000000 --- a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenBitcoin.kt +++ /dev/null @@ -1,170 +0,0 @@ -/* - * This file is part of GNU Taler - * (C) 2022 Taler Systems S.A. - * - * GNU Taler is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 3, or (at your option) any later version. - * - * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> - */ - -package net.taler.wallet.withdraw.manual - -import android.net.Uri -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.wrapContentWidth -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.verticalScroll -import androidx.compose.material.Button -import androidx.compose.material.ButtonDefaults -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Surface -import androidx.compose.material.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Alignment -import androidx.compose.ui.Alignment.Companion.End -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.colorResource -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.em -import net.taler.common.Amount -import net.taler.wallet.R -import net.taler.wallet.compose.CopyToClipboardButton -import net.taler.wallet.withdraw.WithdrawStatus - -@Composable -fun ScreenBitcoin( - status: WithdrawStatus.ManualTransferRequiredBitcoin, - bankAppClick: (() -> Unit)?, - onCancelClick: (() -> Unit)?, -) { - val scrollState = rememberScrollState() - Column(modifier = Modifier - .wrapContentWidth(Alignment.CenterHorizontally) - .verticalScroll(scrollState) - .padding(all = 16.dp) - ) { - Text( - text = stringResource(R.string.withdraw_manual_bitcoin_title), - style = MaterialTheme.typography.h5, - ) - Text( - text = stringResource(R.string.withdraw_manual_bitcoin_intro), - style = MaterialTheme.typography.body1, - modifier = Modifier - .padding(vertical = 8.dp) - ) - BitcoinSegwitAddrs( - amount = status.amountRaw, - addr = status.account, - segwitAddresses = status.segwitAddrs - ) - if (bankAppClick != null) { - Button( - onClick = bankAppClick, - modifier = Modifier - .padding(vertical = 16.dp) - .align(Alignment.CenterHorizontally), - ) { - Text(text = stringResource(R.string.withdraw_manual_ready_bank_button)) - } - } - if (onCancelClick != null) { - Button( - onClick = onCancelClick, - colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.red)), - modifier = Modifier - .padding(vertical = 16.dp) - .align(End), - ) { - Text( - text = stringResource(R.string.withdraw_manual_ready_cancel), - color = Color.White, - ) - } - } - } -} - -@Composable -fun BitcoinSegwitAddrs(amount: Amount, addr: String, segwitAddresses: List<String>) { - Column { - CopyToClipboardButton( - modifier = Modifier.align(End), - label = "Bitcoin", - content = getCopyText(amount, addr, segwitAddresses), - ) - Row(modifier = Modifier.padding(vertical = 8.dp)) { - Column(modifier = Modifier.weight(0.3f)) { - Text( - text = addr, - style = MaterialTheme.typography.body1, - fontWeight = FontWeight.Normal, - fontSize = 3.em - ) - Text( - text = amount.withCurrency("BTC").toString(), - style = MaterialTheme.typography.body1, - fontWeight = FontWeight.Bold, - ) - } - } - for (segwitAddress in segwitAddresses) { - Row(modifier = Modifier.padding(vertical = 8.dp)) { - Column(modifier = Modifier.weight(0.3f)) { - Text( - text = segwitAddress, - style = MaterialTheme.typography.body1, - fontWeight = FontWeight.Normal, - fontSize = 3.em, - ) - Text( - text = SEGWIT_MIN.toString(), - style = MaterialTheme.typography.body1, - fontWeight = FontWeight.Bold, - ) - } - } - } - } -} - -private val SEGWIT_MIN = Amount("BTC", 0, 294) - -private fun getCopyText(amount: Amount, addr: String, segwitAddresses: List<String>): String { - val sr = segwitAddresses.joinToString(separator = "\n") { s -> - "\n$s ${SEGWIT_MIN}\n" - } - return "$addr ${amount.withCurrency("BTC")}\n$sr" -} - -@Preview -@Composable -fun PreviewScreenBitcoin() { - Surface { - ScreenBitcoin(WithdrawStatus.ManualTransferRequiredBitcoin( - exchangeBaseUrl = "bitcoin.ice.bfh.ch", - uri = Uri.parse("https://taler.net"), - account = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", - segwitAddrs = listOf( - "bc1qqleages8702xvg9qcyu02yclst24xurdrynvxq", - "bc1qsleagehks96u7jmqrzcf0fw80ea5g57qm3m84c" - ), - subject = "0ZSX8SH0M30KHX8K3Y1DAMVGDQV82XEF9DG1HC4QMQ3QWYT4AF00", - amountRaw = Amount("BITCOINBTC", 0, 14000000), - transactionId = "", - ), {}) {} - } -} diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenIBAN.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenIBAN.kt deleted file mode 100644 index 79ca364..0000000 --- a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenIBAN.kt +++ /dev/null @@ -1,161 +0,0 @@ -/* - * This file is part of GNU Taler - * (C) 2022 Taler Systems S.A. - * - * GNU Taler is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 3, or (at your option) any later version. - * - * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> - */ - -package net.taler.wallet.withdraw.manual - -import android.net.Uri -import androidx.compose.foundation.BorderStroke -import androidx.compose.foundation.background -import androidx.compose.foundation.border -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.wrapContentWidth -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.verticalScroll -import androidx.compose.material.Button -import androidx.compose.material.ButtonDefaults -import androidx.compose.material.Icon -import androidx.compose.material.IconButton -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Surface -import androidx.compose.material.Text -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.ContentCopy -import androidx.compose.runtime.Composable -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.alpha -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.colorResource -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.font.FontWeight -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import net.taler.common.Amount -import net.taler.wallet.R -import net.taler.wallet.compose.copyToClipBoard -import net.taler.wallet.withdraw.WithdrawStatus - -@Composable -fun ScreenIBAN( - status: WithdrawStatus.ManualTransferRequiredIBAN, - bankAppClick: (() -> Unit)?, - onCancelClick: (() -> Unit)?, -) { - val scrollState = rememberScrollState() - Column(modifier = Modifier - .wrapContentWidth(Alignment.CenterHorizontally) - .verticalScroll(scrollState) - .padding(all = 16.dp) - ) { - Text( - text = stringResource(R.string.withdraw_manual_ready_title), - style = MaterialTheme.typography.h5, - ) - Text( - text = stringResource(R.string.withdraw_manual_ready_intro, - status.amountRaw.toString()), - style = MaterialTheme.typography.body1, - modifier = Modifier - .padding(vertical = 8.dp) - ) - DetailRow(stringResource(R.string.withdraw_manual_ready_iban), status.iban) - DetailRow(stringResource(R.string.withdraw_manual_ready_subject), status.subject) - DetailRow(stringResource(R.string.amount_chosen), status.amountRaw.toString()) - DetailRow(stringResource(R.string.withdraw_exchange), status.exchangeBaseUrl, false) - Text( - text = stringResource(R.string.withdraw_manual_ready_warning), - style = MaterialTheme.typography.body2, - color = colorResource(R.color.notice_text), - modifier = Modifier - .align(Alignment.CenterHorizontally) - .padding(all = 8.dp) - .background(colorResource(R.color.notice_background)) - .border(BorderStroke(2.dp, colorResource(R.color.notice_border))) - .padding(all = 16.dp) - ) - if (bankAppClick != null) { - Button( - onClick = bankAppClick, - modifier = Modifier - .padding(vertical = 16.dp) - .align(Alignment.CenterHorizontally), - ) { - Text(text = stringResource(R.string.withdraw_manual_ready_bank_button)) - } - } - if (onCancelClick != null) { - Button( - onClick = onCancelClick, - colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.red)), - modifier = Modifier - .padding(vertical = 16.dp) - .align(Alignment.End), - ) { - Text( - text = stringResource(R.string.withdraw_manual_ready_cancel), - color = Color.White, - ) - } - } - } -} - -@Composable -fun DetailRow(label: String, content: String, copy: Boolean = true) { - val context = LocalContext.current - Row { - Column( - modifier = Modifier - .weight(0.3f)) { - Text( - text = label, - style = MaterialTheme.typography.body1, - fontWeight = if (copy) FontWeight.Bold else FontWeight.Normal, - ) - if (copy) { - IconButton( - onClick = { copyToClipBoard(context, label, content) }, - ) { Icon(Icons.Default.ContentCopy, stringResource(R.string.copy)) } - } - } - Text( - text = content, - style = MaterialTheme.typography.body1, - modifier = Modifier - .padding(bottom = 8.dp) - .weight(0.7f) - .then(if (copy) Modifier else Modifier.alpha(0.7f)) - ) - } -} - -@Preview -@Composable -fun PreviewScreenIBAN() { - Surface { - ScreenIBAN(WithdrawStatus.ManualTransferRequiredIBAN( - exchangeBaseUrl = "test.exchange.taler.net", - uri = Uri.parse("https://taler.net"), - iban = "ASDQWEASDZXCASDQWE", - subject = "Taler Withdrawal P2T19EXRBY4B145JRNZ8CQTD7TCS03JE9VZRCEVKVWCP930P56WG", - amountRaw = Amount("KUDOS", 10, 0), - transactionId = "", - ), {}) {} - } -} diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenTransfer.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenTransfer.kt new file mode 100644 index 0000000..00495fb --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenTransfer.kt @@ -0,0 +1,326 @@ +/* + * This file is part of GNU Taler + * (C) 2023 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.withdraw.manual + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.ContentCopy +import androidx.compose.material3.Button +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.ScrollableTabRow +import androidx.compose.material3.Surface +import androidx.compose.material3.Tab +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import net.taler.common.Amount +import net.taler.wallet.CURRENCY_BTC +import net.taler.wallet.R +import net.taler.common.CurrencySpecification +import net.taler.wallet.compose.ShareButton +import net.taler.wallet.compose.copyToClipBoard +import net.taler.wallet.transactions.AmountType +import net.taler.wallet.transactions.TransactionAmountComposable +import net.taler.wallet.transactions.WithdrawalExchangeAccountDetails +import net.taler.wallet.transactions.WithdrawalExchangeAccountDetails.Status.* +import net.taler.wallet.withdraw.TransferData +import net.taler.wallet.withdraw.WithdrawStatus + +@Composable +fun ScreenTransfer( + status: WithdrawStatus.ManualTransferRequired, + spec: CurrencySpecification?, + bankAppClick: ((transfer: TransferData) -> Unit)?, + shareClick: ((transfer: TransferData) -> Unit)?, +) { + // TODO: show some placeholder + if (status.withdrawalTransfers.isEmpty()) return + + val transfers = status.withdrawalTransfers.filter { + // TODO: in dev mode, show debug info when status is `Error' + it.withdrawalAccount.status == Ok + }.sortedByDescending { + it.withdrawalAccount.priority + } + + val defaultTransfer = transfers[0] + var selectedTransfer by remember { mutableStateOf(defaultTransfer) } + + Column { + if (status.withdrawalTransfers.size > 1) { + TransferAccountChooser( + accounts = transfers.map { it.withdrawalAccount }, + selectedAccount = selectedTransfer.withdrawalAccount, + onSelectAccount = { account -> + status.withdrawalTransfers.find { + it.withdrawalAccount.paytoUri == account.paytoUri + }?.let { selectedTransfer = it } + } + ) + } + + val scrollState = rememberScrollState() + Column( + modifier = Modifier + .verticalScroll(scrollState), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + when (val transfer = selectedTransfer) { + is TransferData.Taler -> TransferTaler( + transfer = transfer, + exchangeBaseUrl = status.exchangeBaseUrl, + transactionAmountRaw = status.transactionAmountRaw.withSpec(spec), + transactionAmountEffective = status.transactionAmountEffective.withSpec(spec), + ) + + is TransferData.IBAN -> TransferIBAN( + transfer = transfer, + exchangeBaseUrl = status.exchangeBaseUrl, + transactionAmountRaw = status.transactionAmountRaw.withSpec(spec), + transactionAmountEffective = status.transactionAmountEffective.withSpec(spec), + ) + + is TransferData.Bitcoin -> TransferBitcoin( + transfer = transfer, + transactionAmountRaw = status.transactionAmountRaw.withSpec(spec), + transactionAmountEffective = status.transactionAmountEffective.withSpec(spec), + ) + } + + if (bankAppClick != null) { + Button( + onClick = { bankAppClick(selectedTransfer) }, + modifier = Modifier + .padding(bottom = 16.dp), + ) { + Text(text = stringResource(R.string.withdraw_manual_ready_bank_button)) + } + } + + if (shareClick != null) { + ShareButton( + content = selectedTransfer.withdrawalAccount.paytoUri, + modifier = Modifier + .padding(bottom = 16.dp), + ) + } + } + } +} + +@Composable +fun DetailRow( + label: String, + content: String, + copy: Boolean = true, +) { + val context = LocalContext.current + + Column( + modifier = Modifier.fillMaxWidth(), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + Text( + modifier = Modifier.padding(top = 16.dp, start = 6.dp, end = 6.dp), + text = label, + style = MaterialTheme.typography.bodyMedium, + ) + + Text( + modifier = Modifier.padding( + top = 8.dp, + start = 6.dp, + end = 6.dp, + ), + text = content, + style = MaterialTheme.typography.bodyLarge, + fontFamily = if (copy) FontFamily.Monospace else FontFamily.Default, + textAlign = TextAlign.Center, + ) + + if (copy) { + IconButton( + onClick = { copyToClipBoard(context, label, content) }, + ) { + Icon( + imageVector = Icons.Default.ContentCopy, + contentDescription = stringResource(R.string.copy), + ) + } + } + } +} + +@Composable +fun WithdrawalAmountTransfer( + amountRaw: Amount, + amountEffective: Amount, + conversionAmountRaw: Amount, +) { + Column( + modifier = Modifier.fillMaxWidth(), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + TransactionAmountComposable( + label = stringResource(R.string.amount_transfer), + amount = conversionAmountRaw, + amountType = AmountType.Neutral, + ) + + if (amountRaw.currency != conversionAmountRaw.currency) { + TransactionAmountComposable( + label = stringResource(R.string.amount_conversion), + amount = amountRaw, + amountType = AmountType.Neutral, + ) + } + + if (amountRaw > amountEffective) { + val fee = amountRaw - amountEffective + TransactionAmountComposable( + label = stringResource(id = R.string.amount_fee), + amount = fee, + amountType = AmountType.Negative, + ) + + TransactionAmountComposable( + label = stringResource(id = R.string.amount_total), + amount = amountEffective, + amountType = AmountType.Positive, + ) + } + } +} + +@Composable +fun TransferAccountChooser( + modifier: Modifier = Modifier, + accounts: List<WithdrawalExchangeAccountDetails>, + selectedAccount: WithdrawalExchangeAccountDetails, + onSelectAccount: (account: WithdrawalExchangeAccountDetails) -> Unit, +) { + val selectedIndex = accounts.indexOfFirst { + it.paytoUri == selectedAccount.paytoUri + } + + ScrollableTabRow( + selectedTabIndex = selectedIndex, + modifier = modifier, + edgePadding = 8.dp, + ) { + accounts.forEachIndexed { index, account -> + Tab( + selected = selectedAccount.paytoUri == account.paytoUri, + onClick = { onSelectAccount(account) }, + text = { + if (!account.bankLabel.isNullOrEmpty()) { + Text(account.bankLabel) + } else if (account.currencySpecification?.name != null) { + Text(stringResource( + R.string.withdraw_account_currency, + index + 1, + account.currencySpecification.name, + )) + } else if (account.transferAmount?.currency != null) { + Text(stringResource( + R.string.withdraw_account_currency, + index + 1, + account.transferAmount.currency, + )) + } else Text(stringResource(R.string.withdraw_account, index + 1)) + }, + ) + } + } +} + +@Preview +@Composable +fun ScreenTransferPreview() { + Surface { + ScreenTransfer( + status = WithdrawStatus.ManualTransferRequired( + transactionId = "", + transactionAmountRaw = Amount.fromJSONString("KUDOS:10"), + transactionAmountEffective = Amount.fromJSONString("KUDOS:9.5"), + exchangeBaseUrl = "test.exchange.taler.net", + withdrawalTransfers = listOf( + TransferData.IBAN( + iban = "ASDQWEASDZXCASDQWE", + subject = "Taler Withdrawal P2T19EXRBY4B145JRNZ8CQTD7TCS03JE9VZRCEVKVWCP930P56WG", + amountRaw = Amount("KUDOS", 10, 0), + amountEffective = Amount("KUDOS", 9, 5), + withdrawalAccount = WithdrawalExchangeAccountDetails( + paytoUri = "https://taler.net/kudos", + transferAmount = Amount("KUDOS", 10, 0), + status = Ok, + currencySpecification = CurrencySpecification( + "KUDOS", + numFractionalInputDigits = 2, + numFractionalNormalDigits = 2, + numFractionalTrailingZeroDigits = 2, + altUnitNames = emptyMap(), + ), + ), + ), + TransferData.Bitcoin( + account = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", + segwitAddresses = listOf( + "bc1qqleages8702xvg9qcyu02yclst24xurdrynvxq", + "bc1qsleagehks96u7jmqrzcf0fw80ea5g57qm3m84c" + ), + subject = "0ZSX8SH0M30KHX8K3Y1DAMVGDQV82XEF9DG1HC4QMQ3QWYT4AF00", + amountRaw = Amount(CURRENCY_BTC, 0, 14000000), + amountEffective = Amount(CURRENCY_BTC, 0, 14000000), + withdrawalAccount = WithdrawalExchangeAccountDetails( + paytoUri = "https://taler.net/btc", + transferAmount = Amount("BTC", 0, 14000000), + status = Ok, + currencySpecification = CurrencySpecification( + "Bitcoin", + numFractionalInputDigits = 2, + numFractionalNormalDigits = 2, + numFractionalTrailingZeroDigits = 2, + altUnitNames = emptyMap(), + ), + ), + ) + ), + ), + spec = null, + bankAppClick = {}, + shareClick = {}, + ) + } +}
\ No newline at end of file diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferBitcoin.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferBitcoin.kt new file mode 100644 index 0000000..c21ca7e --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferBitcoin.kt @@ -0,0 +1,112 @@ +/* + * This file is part of GNU Taler + * (C) 2023 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.withdraw.manual + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment.Companion.CenterHorizontally +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import net.taler.common.Amount +import net.taler.wallet.R +import net.taler.wallet.compose.CopyToClipboardButton +import net.taler.wallet.withdraw.TransferData + +@Composable +fun TransferBitcoin( + transfer: TransferData.Bitcoin, + transactionAmountRaw: Amount, + transactionAmountEffective: Amount, +) { + Column( + modifier = Modifier.padding(all = 16.dp), + horizontalAlignment = CenterHorizontally, + ) { + Text( + text = stringResource(R.string.withdraw_manual_bitcoin_intro), + style = MaterialTheme.typography.bodyLarge, + modifier = Modifier + .padding(vertical = 8.dp) + ) + + BitcoinSegwitAddresses( + amount = transfer.amountRaw, + address = transfer.account, + segwitAddresses = transfer.segwitAddresses, + ) + + transfer.withdrawalAccount.transferAmount?.let { amount -> + WithdrawalAmountTransfer( + amountRaw = transactionAmountRaw, + amountEffective = transactionAmountEffective, + conversionAmountRaw = amount.withSpec( + transfer.withdrawalAccount.currencySpecification, + ), + ) + } + } +} + +@Composable +fun BitcoinSegwitAddresses(amount: Amount, address: String, segwitAddresses: List<String>) { + Column { + val allSegwitAddresses = listOf(address) + segwitAddresses + for (segwitAddress in allSegwitAddresses) { + Row(modifier = Modifier.padding(vertical = 8.dp)) { + Column(modifier = Modifier.weight(0.3f)) { + Text( + text = segwitAddress, + fontWeight = FontWeight.Normal, + fontFamily = FontFamily.Monospace, + style = MaterialTheme.typography.bodySmall, + ) + Text( + text = if (segwitAddress == address) + amount.withCurrency("BTC").toString() + else SEGWIT_MIN.toString(), + style = MaterialTheme.typography.bodyLarge, + fontWeight = FontWeight.Bold, + ) + } + } + } + + CopyToClipboardButton( + modifier = Modifier + .padding(top = 16.dp, start = 6.dp, end = 6.dp) + .align(CenterHorizontally), + label = "Bitcoin", + content = getCopyText(amount, address, segwitAddresses), + ) + } +} + +private val SEGWIT_MIN = Amount("BTC", 0, 294) + +private fun getCopyText(amount: Amount, addr: String, segwitAddresses: List<String>): String { + val sr = segwitAddresses.joinToString(separator = "\n") { s -> + "\n$s ${SEGWIT_MIN}\n" + } + return "$addr ${amount.withCurrency("BTC")}\n$sr" +}
\ No newline at end of file diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferIBAN.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferIBAN.kt new file mode 100644 index 0000000..1698530 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferIBAN.kt @@ -0,0 +1,93 @@ +/* + * This file is part of GNU Taler + * (C) 2023 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.withdraw.manual + +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import net.taler.common.Amount +import net.taler.wallet.R +import net.taler.wallet.cleanExchange +import net.taler.wallet.transactions.TransactionInfoComposable +import net.taler.wallet.withdraw.TransferData + +@Composable +fun TransferIBAN( + transfer: TransferData.IBAN, + exchangeBaseUrl: String, + transactionAmountRaw: Amount, + transactionAmountEffective: Amount, +) { + val transferAmount = transfer + .withdrawalAccount + .transferAmount + ?.withSpec(transfer.withdrawalAccount.currencySpecification) + ?: transfer.amountRaw + + Column( + modifier = Modifier.padding(all = 16.dp), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + Text( + text = stringResource( + R.string.withdraw_manual_ready_intro, + transferAmount), + style = MaterialTheme.typography.bodyLarge, + modifier = Modifier + .padding(vertical = 8.dp) + ) + + Text( + text = stringResource(R.string.withdraw_manual_ready_warning), + style = MaterialTheme.typography.bodyMedium, + color = colorResource(R.color.notice_text), + modifier = Modifier + .align(Alignment.CenterHorizontally) + .padding(all = 8.dp) + .background(colorResource(R.color.notice_background)) + .border(BorderStroke(2.dp, colorResource(R.color.notice_border))) + .padding(all = 16.dp) + ) + + DetailRow(stringResource(R.string.withdraw_manual_ready_subject), transfer.subject) + transfer.receiverName?.let { + DetailRow(stringResource(R.string.withdraw_manual_ready_receiver), it) + } + DetailRow(stringResource(R.string.withdraw_manual_ready_iban), transfer.iban) + + TransactionInfoComposable( + label = stringResource(R.string.withdraw_exchange), + info = cleanExchange(exchangeBaseUrl), + ) + + WithdrawalAmountTransfer( + amountRaw = transactionAmountRaw, + amountEffective = transactionAmountEffective, + conversionAmountRaw = transferAmount, + ) + } +}
\ No newline at end of file diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferTaler.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferTaler.kt new file mode 100644 index 0000000..089d0de --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/withdraw/manual/TransferTaler.kt @@ -0,0 +1,93 @@ +/* + * This file is part of GNU Taler + * (C) 2024 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.withdraw.manual + +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import net.taler.common.Amount +import net.taler.wallet.R +import net.taler.wallet.cleanExchange +import net.taler.wallet.transactions.TransactionInfoComposable +import net.taler.wallet.withdraw.TransferData + +@Composable +fun TransferTaler( + transfer: TransferData.Taler, + exchangeBaseUrl: String, + transactionAmountRaw: Amount, + transactionAmountEffective: Amount, +) { + val transferAmount = transfer + .withdrawalAccount + .transferAmount + ?.withSpec(transfer.withdrawalAccount.currencySpecification) + ?: transfer.amountRaw + + Column( + modifier = Modifier.padding(all = 16.dp), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + Text( + text = stringResource( + R.string.withdraw_manual_ready_intro, + transferAmount), + style = MaterialTheme.typography.bodyLarge, + modifier = Modifier + .padding(vertical = 8.dp) + ) + + Text( + text = stringResource(R.string.withdraw_manual_ready_warning), + style = MaterialTheme.typography.bodyMedium, + color = colorResource(R.color.notice_text), + modifier = Modifier + .align(Alignment.CenterHorizontally) + .padding(all = 8.dp) + .background(colorResource(R.color.notice_background)) + .border(BorderStroke(2.dp, colorResource(R.color.notice_border))) + .padding(all = 16.dp) + ) + + DetailRow(stringResource(R.string.withdraw_manual_ready_subject), transfer.subject) + transfer.receiverName?.let { + DetailRow(stringResource(R.string.withdraw_manual_ready_receiver), it) + } + DetailRow(stringResource(R.string.withdraw_manual_ready_account), transfer.account) + + TransactionInfoComposable( + label = stringResource(R.string.withdraw_exchange), + info = cleanExchange(exchangeBaseUrl), + ) + + WithdrawalAmountTransfer( + amountRaw = transactionAmountRaw, + amountEffective = transactionAmountEffective, + conversionAmountRaw = transferAmount, + ) + } +}
\ No newline at end of file |