summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt
diff options
context:
space:
mode:
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt')
-rw-r--r--wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt86
1 files changed, 46 insertions, 40 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt b/wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt
index 898dcfd..5bfd030 100644
--- a/wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt
+++ b/wallet/src/main/java/net/taler/wallet/peer/PeerManager.kt
@@ -16,18 +16,15 @@
package net.taler.wallet.peer
-import android.graphics.Bitmap
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
-import kotlinx.serialization.Serializable
import net.taler.common.Amount
import net.taler.common.QrCodeManager
import net.taler.wallet.TAG
-import net.taler.wallet.backend.TalerErrorInfo
import net.taler.wallet.backend.WalletBackendApi
import net.taler.wallet.exchanges.ExchangeItem
import org.json.JSONObject
@@ -37,14 +34,17 @@ class PeerManager(
private val scope: CoroutineScope,
) {
- private val _pullState = MutableStateFlow<PeerPaymentState>(PeerPaymentIntro)
- val pullState: StateFlow<PeerPaymentState> = _pullState
+ private val _pullState = MutableStateFlow<PeerOutgoingState>(PeerOutgoingIntro)
+ val pullState: StateFlow<PeerOutgoingState> = _pullState
- private val _pushState = MutableStateFlow<PeerPaymentState>(PeerPaymentIntro)
- val pushState: StateFlow<PeerPaymentState> = _pushState
+ private val _pushState = MutableStateFlow<PeerOutgoingState>(PeerOutgoingIntro)
+ val pushState: StateFlow<PeerOutgoingState> = _pushState
+
+ private val _paymentState = MutableStateFlow<PeerIncomingState>(PeerIncomingChecking)
+ val paymentState: StateFlow<PeerIncomingState> = _paymentState
fun initiatePullPayment(amount: Amount, exchange: ExchangeItem) {
- _pullState.value = PeerPaymentCreating
+ _pullState.value = PeerOutgoingCreating
scope.launch(Dispatchers.IO) {
api.request("initiatePeerPullPayment", InitiatePeerPullPaymentResponse.serializer()) {
put("exchangeBaseUrl", exchange.exchangeBaseUrl)
@@ -54,20 +54,20 @@ class PeerManager(
})
}.onSuccess {
val qrCode = QrCodeManager.makeQrCode(it.talerUri)
- _pullState.value = PeerPaymentResponse(it.talerUri, qrCode)
+ _pullState.value = PeerOutgoingResponse(it.talerUri, qrCode)
}.onError { error ->
Log.e(TAG, "got initiatePeerPullPayment error result $error")
- _pullState.value = PeerPaymentError(error)
+ _pullState.value = PeerOutgoingError(error)
}
}
}
fun resetPullPayment() {
- _pullState.value = PeerPaymentIntro
+ _pullState.value = PeerOutgoingIntro
}
fun initiatePeerPushPayment(amount: Amount, summary: String) {
- _pushState.value = PeerPaymentCreating
+ _pushState.value = PeerOutgoingCreating
scope.launch(Dispatchers.IO) {
api.request("initiatePeerPushPayment", InitiatePeerPushPaymentResponse.serializer()) {
put("amount", amount.toJSONString())
@@ -76,42 +76,48 @@ class PeerManager(
})
}.onSuccess { response ->
val qrCode = QrCodeManager.makeQrCode(response.talerUri)
- _pushState.value = PeerPaymentResponse(response.talerUri, qrCode)
+ _pushState.value = PeerOutgoingResponse(response.talerUri, qrCode)
}.onError { error ->
Log.e(TAG, "got initiatePeerPushPayment error result $error")
- _pushState.value = PeerPaymentError(error)
+ _pushState.value = PeerOutgoingError(error)
}
}
}
fun resetPushPayment() {
- _pushState.value = PeerPaymentIntro
+ _pushState.value = PeerOutgoingIntro
}
-}
-
-sealed class PeerPaymentState
-object PeerPaymentIntro : PeerPaymentState()
-object PeerPaymentCreating : PeerPaymentState()
-data class PeerPaymentResponse(
- val talerUri: String,
- val qrCode: Bitmap,
-) : PeerPaymentState()
-
-data class PeerPaymentError(
- val info: TalerErrorInfo,
-) : PeerPaymentState()
+ fun checkPeerPullPayment(talerUri: String) {
+ _paymentState.value = PeerIncomingChecking
+ scope.launch(Dispatchers.IO) {
+ api.request("checkPeerPullPayment", CheckPeerPullPaymentResponse.serializer()) {
+ put("talerUri", talerUri)
+ }.onSuccess { response ->
+ _paymentState.value = PeerIncomingTerms(
+ amount = response.amount,
+ contractTerms = response.contractTerms,
+ id = response.peerPullPaymentIncomingId,
+ )
+ }.onError { error ->
+ Log.e(TAG, "got checkPeerPushPayment error result $error")
+ _paymentState.value = PeerIncomingError(error)
+ }
+ }
+ }
-@Serializable
-data class InitiatePeerPullPaymentResponse(
- /**
- * Taler URI for the other party to make the payment that was requested.
- */
- val talerUri: String,
-)
+ fun acceptPeerPullPayment(terms: PeerIncomingTerms) {
+ _paymentState.value = PeerIncomingAccepting(terms)
+ scope.launch(Dispatchers.IO) {
+ api.request<Unit>("acceptPeerPullPayment") {
+ put("peerPullPaymentIncomingId", terms.id)
+ }.onSuccess {
+ _paymentState.value = PeerIncomingAccepted
+ }.onError { error ->
+ Log.e(TAG, "got checkPeerPushPayment error result $error")
+ _paymentState.value = PeerIncomingError(error)
+ }
+ }
+ }
-@Serializable
-data class InitiatePeerPushPaymentResponse(
- val exchangeBaseUrl: String,
- val talerUri: String,
-)
+}