From f0670e2f3936f0223c02e9ec0d0de52f31a3539f Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 11 Aug 2020 09:41:40 -0300 Subject: [pos] Improve coroutine-based merchant library access --- .../net/taler/merchantpos/history/HistoryManager.kt | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'merchant-terminal/src/main/java/net/taler/merchantpos/history') diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt index aabe4cc..d880eaa 100644 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt @@ -20,8 +20,8 @@ import androidx.annotation.UiThread import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import net.taler.common.assertUiThread import net.taler.merchantlib.MerchantApi import net.taler.merchantlib.OrderHistoryEntry import net.taler.merchantpos.config.ConfigManager @@ -44,20 +44,19 @@ class HistoryManager( val items: LiveData = mItems @UiThread - internal fun fetchHistory() { + internal fun fetchHistory() = scope.launch { mIsLoading.value = true val merchantConfig = configManager.merchantConfig!! - scope.launch(Dispatchers.IO) { - api.getOrderHistory(merchantConfig).handle(::onHistoryError) { - mIsLoading.postValue(false) - mItems.postValue(HistoryResult.Success(it.orders)) - } + api.getOrderHistory(merchantConfig).handle(::onHistoryError) { + assertUiThread() + mIsLoading.value = false + mItems.value = HistoryResult.Success(it.orders) } } private fun onHistoryError(msg: String) { - mIsLoading.postValue(false) - mItems.postValue(HistoryResult.Error(msg)) + assertUiThread() + mIsLoading.value = false + mItems.value = HistoryResult.Error(msg) } - } -- cgit v1.2.3