summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-04-02 13:48:33 -0300
committerTorsten Grote <t@grobox.de>2020-04-02 13:48:33 -0300
commita6e5a8d9f210618d779d43c859234b94827bf172 (patch)
tree77ea746665a3d32b2f3e68e33f2f586ea7b8d2e3 /wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
parentc3f777e6154d33d26b2431fa886678edf029dd34 (diff)
downloadtaler-android-a6e5a8d9f210618d779d43c859234b94827bf172.tar.gz
taler-android-a6e5a8d9f210618d779d43c859234b94827bf172.tar.bz2
taler-android-a6e5a8d9f210618d779d43c859234b94827bf172.zip
[wallet] Show withdraw summary before ToS and do one-click accept
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt')
-rw-r--r--wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt68
1 files changed, 31 insertions, 37 deletions
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 d686465..e252627 100644
--- a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
+++ b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
@@ -21,6 +21,7 @@ import androidx.lifecycle.MutableLiveData
import net.taler.common.Amount
import net.taler.wallet.TAG
import net.taler.wallet.backend.WalletBackendApi
+import net.taler.wallet.withdraw.WithdrawStatus.ReceivedDetails
import org.json.JSONObject
sealed class WithdrawStatus {
@@ -62,9 +63,9 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) {
}
fun getWithdrawalInfo(talerWithdrawUri: String) {
- val args = JSONObject()
- args.put("talerWithdrawUri", talerWithdrawUri)
-
+ val args = JSONObject().apply {
+ put("talerWithdrawUri", talerWithdrawUri)
+ }
withdrawStatus.value = WithdrawStatus.Loading(talerWithdrawUri)
this.currentWithdrawRequestId++
@@ -97,12 +98,13 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) {
}
private fun getWithdrawalInfoWithExchange(talerWithdrawUri: String, selectedExchange: String) {
- val args = JSONObject()
- args.put("talerWithdrawUri", talerWithdrawUri)
- args.put("selectedExchange", selectedExchange)
+ val args = JSONObject().apply {
+ put("talerWithdrawUri", talerWithdrawUri)
+ put("selectedExchange", selectedExchange)
+ }
- this.currentWithdrawRequestId++
- val myWithdrawRequestId = this.currentWithdrawRequestId
+ currentWithdrawRequestId++
+ val myWithdrawRequestId = currentWithdrawRequestId
walletBackendApi.sendRequest("getWithdrawDetailsForUri", args) { isError, result ->
if (isError) {
@@ -111,15 +113,15 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) {
withdrawStatus.postValue(WithdrawStatus.Error(message))
return@sendRequest
}
- if (myWithdrawRequestId != this.currentWithdrawRequestId) {
- val mismatch = "$myWithdrawRequestId != ${this.currentWithdrawRequestId}"
+ if (myWithdrawRequestId != currentWithdrawRequestId) {
+ val mismatch = "$myWithdrawRequestId != $currentWithdrawRequestId"
Log.w(TAG, "Got withdraw result for different request id $mismatch")
return@sendRequest
}
Log.v(TAG, "got getWithdrawDetailsForUri result (with exchange details)")
val status = withdrawStatus.value
if (status !is WithdrawStatus.Loading) {
- Log.v(TAG, "ignoring withdrawal info result, not loading.")
+ Log.w(TAG, "ignoring withdrawal info result, not loading.")
return@sendRequest
}
val wi = result.getJSONObject("bankWithdrawDetails")
@@ -145,7 +147,7 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) {
)
} else {
withdrawStatus.postValue(
- WithdrawStatus.ReceivedDetails(
+ ReceivedDetails(
status.talerWithdrawUri,
amount,
suggestedExchange
@@ -162,15 +164,16 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) {
withdrawStatus.value = WithdrawStatus.Withdrawing(talerWithdrawUri)
- walletBackendApi.sendRequest("acceptWithdrawal", args) { isError, _ ->
+ walletBackendApi.sendRequest("acceptWithdrawal", args) { isError, result ->
if (isError) {
- Log.v(TAG, "got acceptWithdrawal error result")
+ Log.v(TAG, "got acceptWithdrawal error result: ${result.toString(4)}")
return@sendRequest
}
Log.v(TAG, "got acceptWithdrawal result")
val status = withdrawStatus.value
if (status !is WithdrawStatus.Withdrawing) {
- Log.v(TAG, "ignoring acceptWithdrawal result, invalid state")
+ Log.w(TAG, "ignoring acceptWithdrawal result, invalid state: $status")
+ return@sendRequest
}
withdrawStatus.postValue(WithdrawStatus.Success)
}
@@ -180,30 +183,21 @@ class WithdrawManager(private val walletBackendApi: WalletBackendApi) {
* Accept the currently displayed terms of service.
*/
fun acceptCurrentTermsOfService() {
- when (val s = withdrawStatus.value) {
- is WithdrawStatus.TermsOfServiceReviewRequired -> {
- val args = JSONObject()
- args.put("exchangeBaseUrl", s.exchangeBaseUrl)
- args.put("etag", s.tosEtag)
- walletBackendApi.sendRequest("acceptExchangeTermsOfService", args) { isError, _ ->
- if (isError) {
- return@sendRequest
- }
- withdrawStatus.postValue(
- WithdrawStatus.ReceivedDetails(
- s.talerWithdrawUri,
- s.amount,
- s.suggestedExchange
- )
- )
- }
+ val s = withdrawStatus.value
+ check(s is WithdrawStatus.TermsOfServiceReviewRequired)
+
+ val args = JSONObject().apply {
+ put("exchangeBaseUrl", s.exchangeBaseUrl)
+ put("etag", s.tosEtag)
+ }
+ walletBackendApi.sendRequest("acceptExchangeTermsOfService", args) { isError, result ->
+ if (isError) {
+ Log.e(TAG, "Error acceptExchangeTermsOfService ${result.toString(4)}")
+ return@sendRequest
}
+ val status = ReceivedDetails(s.talerWithdrawUri, s.amount, s.suggestedExchange)
+ withdrawStatus.postValue(status)
}
}
- fun cancelCurrentWithdraw() {
- currentWithdrawRequestId++
- withdrawStatus.value = null
- }
-
}