aboutsummaryrefslogtreecommitdiff
path: root/wallet
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-05-15 11:06:46 -0300
committerTorsten Grote <t@grobox.de>2020-05-15 14:26:43 -0300
commit6028376333344c90ba8dbaa75aad759b2915962d (patch)
tree5536f4acf72aa432ec316228a28d8f125f2a240e /wallet
parent3565fc05bca6e55896ff7b5115e6b8021f2a9d4e (diff)
downloadtaler-android-6028376333344c90ba8dbaa75aad759b2915962d.tar.gz
taler-android-6028376333344c90ba8dbaa75aad759b2915962d.tar.bz2
taler-android-6028376333344c90ba8dbaa75aad759b2915962d.zip
[wallet] cache transactions per currency
Diffstat (limited to 'wallet')
-rw-r--r--wallet/src/main/java/net/taler/wallet/MainViewModel.kt6
-rw-r--r--wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt35
2 files changed, 30 insertions, 11 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt
index 8276823..f10666e 100644
--- a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt
+++ b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt
@@ -28,13 +28,13 @@ import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PRO
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import net.taler.common.Amount
+import net.taler.common.assertUiThread
import net.taler.wallet.backend.WalletBackendApi
import net.taler.wallet.history.DevHistoryManager
import net.taler.wallet.payment.PaymentManager
import net.taler.wallet.pending.PendingOperationsManager
import net.taler.wallet.refund.RefundManager
import net.taler.wallet.transactions.TransactionManager
-import net.taler.wallet.transactions.TransactionsResult
import net.taler.wallet.withdraw.WithdrawManager
import org.json.JSONObject
@@ -72,7 +72,9 @@ class MainViewModel(val app: Application) : AndroidViewModel(app) {
Log.i(TAG, "Received notification from wallet-core: ${payload.toString(2)}")
loadBalances()
if (payload.optString("type") in transactionNotifications) {
- // update transaction list
+ assertUiThread()
+ // TODO notification API should give us a currency to update
+ // update currently selected transaction list
transactionManager.loadTransactions()
}
// refresh pending ops and history with each notification
diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt
index c6c1ecd..f830aa1 100644
--- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt
+++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionManager.kt
@@ -17,6 +17,8 @@
package net.taler.wallet.transactions
import android.util.Log
+import androidx.annotation.UiThread
+import androidx.annotation.WorkerThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.fasterxml.jackson.databind.ObjectMapper
@@ -45,23 +47,38 @@ class TransactionManager(
var selectedCurrency: String? = null
var selectedTransaction: Transaction? = null
- // TODO maybe cache transactions per currency?
- private val mTransactions = MutableLiveData<TransactionsResult>()
- val transactions: LiveData<TransactionsResult> = mTransactions
+ private val mTransactions = HashMap<String, MutableLiveData<TransactionsResult>>()
+ val transactions: LiveData<TransactionsResult>
+ @UiThread
+ get() {
+ val currency = selectedCurrency
+ check(currency != null) { "Did not select currency before getting transactions" }
+ return mTransactions.getOrPut(currency) { MutableLiveData() }
+ }
+ @UiThread
fun loadTransactions() {
- mProgress.postValue(true)
- val request = JSONObject(mapOf("currency" to selectedCurrency))
+ val currency = selectedCurrency ?: return
+ val liveData = mTransactions.getOrPut(currency) {
+ MutableLiveData<TransactionsResult>()
+ }
+ if (liveData.value == null) mProgress.value = true
+ val request = JSONObject(mapOf("currency" to currency))
walletBackendApi.sendRequest("getTransactions", request) { isError, result ->
scope.launch(Dispatchers.Default) {
- onTransactionsLoaded(isError, result)
+ onTransactionsLoaded(liveData, isError, result)
}
}
}
- private fun onTransactionsLoaded(isError: Boolean, result: JSONObject) {
+ @WorkerThread
+ private fun onTransactionsLoaded(
+ liveData: MutableLiveData<TransactionsResult>,
+ isError: Boolean,
+ result: JSONObject
+ ) {
if (isError) {
- mTransactions.postValue(TransactionsResult.Error)
+ liveData.postValue(TransactionsResult.Error)
return
}
Log.e("TEST", result.toString(2)) // TODO remove once API finalized
@@ -69,7 +86,7 @@ class TransactionManager(
val transactions: LinkedList<Transaction> = mapper.readValue(transactionsArray)
transactions.reverse() // show latest first
mProgress.postValue(false)
- mTransactions.postValue(TransactionsResult.Success(transactions))
+ liveData.postValue(TransactionsResult.Success(transactions))
}
}