diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-07 20:48:11 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-07 20:48:11 +0100 |
commit | bf3f54de88679e06690f3da35be2543cb121efeb (patch) | |
tree | de1c007eb888508edd0ad33d405bc31e7ef65042 /app/src/main/java/net/taler/wallet/WalletViewModel.kt | |
parent | 80cd3c9d6d83798316a1222f3875d3a0e74ca278 (diff) | |
download | wallet-android-bf3f54de88679e06690f3da35be2543cb121efeb.tar.gz wallet-android-bf3f54de88679e06690f3da35be2543cb121efeb.tar.bz2 wallet-android-bf3f54de88679e06690f3da35be2543cb121efeb.zip |
pending ops, various tweaks
Diffstat (limited to 'app/src/main/java/net/taler/wallet/WalletViewModel.kt')
-rw-r--r-- | app/src/main/java/net/taler/wallet/WalletViewModel.kt | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/app/src/main/java/net/taler/wallet/WalletViewModel.kt b/app/src/main/java/net/taler/wallet/WalletViewModel.kt index dffc4a0..f8acc2d 100644 --- a/app/src/main/java/net/taler/wallet/WalletViewModel.kt +++ b/app/src/main/java/net/taler/wallet/WalletViewModel.kt @@ -7,7 +7,7 @@ import androidx.lifecycle.MutableLiveData import net.taler.wallet.backend.WalletBackendApi import org.json.JSONObject -val TAG = "taler-wallet" +const val TAG = "taler-wallet" data class Amount(val currency: String, val amount: String) { @@ -77,7 +77,8 @@ open class HistoryResult( open class HistoryEntry( val detail: JSONObject, - val type: String + val type: String, + val timestamp: JSONObject ) open class PendingOperationInfo( @@ -113,6 +114,11 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { value = PendingOperations(listOf()) } + private var activeGetBalance = 0 + private var activeGetPending = 0 + + private var currentPayRequestId = 0 + private val walletBackendApi = WalletBackendApi(app) fun init() { @@ -131,11 +137,22 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { getBalances() getPending() } + walletBackendApi.connectedHandler = { + activeGetBalance = 0 + activeGetPending = 0 + getBalances() + getPending() + } } fun getBalances() { + if (activeGetBalance > 0) { + return + } + activeGetBalance++ walletBackendApi.sendRequest("getBalances", null) { result -> + activeGetBalance-- val balanceList = mutableListOf<BalanceEntry>() val byCurrency = result.getJSONObject("byCurrency") val currencyList = byCurrency.keys().asSequence().toList().sorted() @@ -153,7 +170,12 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { } private fun getPending() { + if (activeGetPending > 0) { + return + } + activeGetPending++ walletBackendApi.sendRequest("getPendingOperations", null) { result -> + activeGetPending-- Log.i(TAG, "got getPending result") val pendingList = mutableListOf<PendingOperationInfo>() val pendingJson = result.getJSONArray("pendingOperations") @@ -177,7 +199,8 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { val type = h.getString("type") Log.v(TAG, "got history entry type $type") val detail = h.getJSONObject("detail") - historyEntries.add(HistoryEntry(detail, type)) + val timestamp = h.getJSONObject("timestamp") + historyEntries.add(HistoryEntry(detail, type, timestamp)) } cb(HistoryResult(historyEntries)) } @@ -191,14 +214,21 @@ class WalletViewModel(val app: Application) : AndroidViewModel(app) { } } + fun preparePay(url: String) { val args = JSONObject() args.put("url", url) + this.currentPayRequestId += 1 + val myPayRequestId = this.currentPayRequestId this.payStatus.value = PayStatus.Loading() walletBackendApi.sendRequest("preparePay", args) { result -> Log.v(TAG, "got preparePay result") + if (myPayRequestId != this.currentPayRequestId) { + Log.v(TAG, "preparePay result was for old request") + return@sendRequest + } val status = result.getString("status") var contractTerms: ContractTerms? = null var proposalId: String? = null |