diff options
author | Torsten Grote <t@grobox.de> | 2024-01-08 17:26:16 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2024-01-08 17:51:53 -0300 |
commit | 0f32834dbbea6737793a7b55190ab58fdfa0bc3d (patch) | |
tree | 379499a3acb47ace2ad8d0d5588e6dce1ff34fdb /wallet/src/main/java/net/taler | |
parent | 22029cd23b4e21df568a87dd9f759e7ccb3880fb (diff) | |
download | taler-android-0f32834dbbea6737793a7b55190ab58fdfa0bc3d.tar.gz taler-android-0f32834dbbea6737793a7b55190ab58fdfa0bc3d.tar.bz2 taler-android-0f32834dbbea6737793a7b55190ab58fdfa0bc3d.zip |
[wallet] support withdraw-exchange URI
Diffstat (limited to 'wallet/src/main/java/net/taler')
3 files changed, 71 insertions, 0 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/MainActivity.kt b/wallet/src/main/java/net/taler/wallet/MainActivity.kt index 16f0efa..868ebe3 100644 --- a/wallet/src/main/java/net/taler/wallet/MainActivity.kt +++ b/wallet/src/main/java/net/taler/wallet/MainActivity.kt @@ -55,6 +55,7 @@ import com.journeyapps.barcodescanner.ScanOptions import com.journeyapps.barcodescanner.ScanOptions.QR_CODE import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import net.taler.common.EventObserver import net.taler.common.isOnline import net.taler.common.showError @@ -283,6 +284,37 @@ class MainActivity : AppCompatActivity(), OnNavigationItemSelectedListener, nav.navigate(R.id.action_global_promptWithdraw) model.withdrawManager.getWithdrawalDetails(u2) } + + action.startsWith("withdraw-exchange/", ignoreCase = true) -> { + model.showProgressBar.value = true + lifecycleScope.launch(Dispatchers.IO) { + val response = model.withdrawManager.prepareManualWithdrawal(u2) + if (response == null) withContext(Dispatchers.Main) { + model.showProgressBar.value = false + nav.navigate(R.id.errorFragment) + } else { + val exchange = + model.exchangeManager.findExchangeByUrl(response.exchangeBaseUrl) + if (exchange == null) withContext(Dispatchers.Main) { + model.showProgressBar.value = false + showError(R.string.exchange_add_error) + } else { + model.exchangeManager.withdrawalExchange = exchange + withContext(Dispatchers.Main) { + model.showProgressBar.value = false + val args = Bundle().apply { + if (response.amount != null) { + putString("amount", response.amount.toJSONString()) + } + } + // there's more than one entry point, so use global action + nav.navigate(R.id.action_global_manual_withdrawal, args) + } + } + } + } + } + action.startsWith("refund/", ignoreCase = true) -> { model.showProgressBar.value = true model.refundManager.refund(u2).observe(this, Observer(::onRefundResponse)) diff --git a/wallet/src/main/java/net/taler/wallet/exchanges/ExchangeManager.kt b/wallet/src/main/java/net/taler/wallet/exchanges/ExchangeManager.kt index 2048b7c..0e16d7a 100644 --- a/wallet/src/main/java/net/taler/wallet/exchanges/ExchangeManager.kt +++ b/wallet/src/main/java/net/taler/wallet/exchanges/ExchangeManager.kt @@ -37,6 +37,11 @@ data class ExchangeListResponse( val exchanges: List<ExchangeItem>, ) +@Serializable +data class ExchangeDetailedResponse( + val exchange: ExchangeItem, +) + class ExchangeManager( private val api: WalletBackendApi, private val scope: CoroutineScope, @@ -104,6 +109,19 @@ class ExchangeManager( return exchange } + @WorkerThread + suspend fun findExchangeByUrl(exchangeUrl: String): ExchangeItem? { + var exchange: ExchangeItem? = null + api.request("getExchangeDetailedInfo", ExchangeDetailedResponse.serializer()) { + put("exchangeBaseUrl", exchangeUrl) + }.onError { + Log.e(TAG, "Error getExchangeDetailedInfo: $it") + }.onSuccess { + exchange = it.exchange + } + return exchange + } + fun addDevExchanges() { scope.launch { listOf( 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 9fd1f9d..ceae7e1 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt @@ -19,6 +19,7 @@ package net.taler.wallet.withdraw import android.net.Uri import android.util.Log import androidx.annotation.UiThread +import androidx.annotation.WorkerThread import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineScope @@ -117,6 +118,12 @@ data class WithdrawalDetailsForUri( ) @Serializable +data class WithdrawExchangeResponse( + val exchangeBaseUrl: String, + val amount: Amount? = null, +) + +@Serializable data class ManualWithdrawalDetails( val tosAccepted: Boolean, val amountRaw: Amount, @@ -214,6 +221,20 @@ class WithdrawManager( } } + @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: ManualWithdrawalDetails, |