From e305ddba1455a33e8dec037d4fcef8498e0b21a5 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Tue, 20 Feb 2024 11:54:14 -0600 Subject: [wallet] Display transactions by scopeInfo instead of currency (cherry picked from commit 04da36054f8996b1e9d70f84506bf8be2ba3abe6) --- .../src/main/java/net/taler/wallet/MainActivity.kt | 5 ++-- .../src/main/java/net/taler/wallet/MainFragment.kt | 22 +++++++-------- .../main/java/net/taler/wallet/MainViewModel.kt | 11 ++++---- .../java/net/taler/wallet/ReceiveFundsFragment.kt | 12 ++++++-- .../java/net/taler/wallet/SendFundsFragment.kt | 12 +++++--- .../net/taler/wallet/balances/BalanceAdapter.kt | 2 +- .../net/taler/wallet/balances/BalancesFragment.kt | 6 ++-- .../taler/wallet/exchanges/ExchangeListFragment.kt | 2 +- .../wallet/transactions/TransactionManager.kt | 32 ++++++++++++---------- .../wallet/transactions/TransactionsFragment.kt | 7 ++--- 10 files changed, 63 insertions(+), 48 deletions(-) (limited to 'wallet') diff --git a/wallet/src/main/java/net/taler/wallet/MainActivity.kt b/wallet/src/main/java/net/taler/wallet/MainActivity.kt index 65e5c2a..5dfd920 100644 --- a/wallet/src/main/java/net/taler/wallet/MainActivity.kt +++ b/wallet/src/main/java/net/taler/wallet/MainActivity.kt @@ -346,8 +346,9 @@ class MainActivity : AppCompatActivity(), OnNavigationItemSelectedListener, val transactionId = status.response.transactionId val transaction = model.transactionManager.getTransactionById(transactionId) if (transaction != null) { - val currency = transaction.amountRaw.currency - model.showTransactions(currency) + // TODO: currency what? scopes are the cool thing now + // val currency = transaction.amountRaw.currency + // model.showTransactions(currency) Snackbar.make(ui.navView, getString(R.string.refund_success), LENGTH_LONG).show() } } diff --git a/wallet/src/main/java/net/taler/wallet/MainFragment.kt b/wallet/src/main/java/net/taler/wallet/MainFragment.kt index 656db63..9fa9838 100644 --- a/wallet/src/main/java/net/taler/wallet/MainFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/MainFragment.kt @@ -24,20 +24,20 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController import net.taler.common.EventObserver -import net.taler.wallet.CurrencyMode.MULTI -import net.taler.wallet.CurrencyMode.SINGLE +import net.taler.wallet.ScopeMode.MULTI +import net.taler.wallet.ScopeMode.SINGLE import net.taler.wallet.balances.BalanceState import net.taler.wallet.balances.BalanceState.Success import net.taler.wallet.balances.BalancesFragment import net.taler.wallet.databinding.FragmentMainBinding import net.taler.wallet.transactions.TransactionsFragment -enum class CurrencyMode { SINGLE, MULTI } +enum class ScopeMode { SINGLE, MULTI } class MainFragment : Fragment() { private val model: MainViewModel by activityViewModels() - private var currencyMode: CurrencyMode? = null + private var scopeMode: ScopeMode? = null private lateinit var ui: FragmentMainBinding @@ -54,10 +54,10 @@ class MainFragment : Fragment() { model.balanceManager.state.observe(viewLifecycleOwner) { onBalancesChanged(it) } - model.transactionsEvent.observe(viewLifecycleOwner, EventObserver { currency -> - // we only need to navigate to a dedicated list, when in multi-currency mode - if (currencyMode == MULTI) { - model.transactionManager.selectedCurrency = currency + model.transactionsEvent.observe(viewLifecycleOwner, EventObserver { scopeInfo -> + // we only need to navigate to a dedicated list, when in multi-scope mode + if (scopeMode == MULTI) { + model.transactionManager.selectedScope = scopeInfo findNavController().navigate(R.id.action_nav_main_to_nav_transactions) } }) @@ -80,14 +80,14 @@ class MainFragment : Fragment() { if (state !is Success) return val balances = state.balances val mode = if (balances.size == 1) SINGLE else MULTI - if (currencyMode != mode) { + if (scopeMode != mode) { val f = if (mode == SINGLE) { - model.transactionManager.selectedCurrency = balances[0].available.currency + model.transactionManager.selectedScope = balances[0].scopeInfo TransactionsFragment() } else { BalancesFragment() } - currencyMode = mode + scopeMode = mode childFragmentManager.beginTransaction() .replace(R.id.mainFragmentContainer, f, mode.name) .commitNow() diff --git a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt index fe11d6a..80e9ef7 100644 --- a/wallet/src/main/java/net/taler/wallet/MainViewModel.kt +++ b/wallet/src/main/java/net/taler/wallet/MainViewModel.kt @@ -36,6 +36,7 @@ import net.taler.wallet.backend.VersionReceiver import net.taler.wallet.backend.WalletBackendApi import net.taler.wallet.backend.WalletCoreVersion import net.taler.wallet.balances.BalanceManager +import net.taler.wallet.balances.ScopeInfo import net.taler.wallet.deposit.DepositManager import net.taler.wallet.exchanges.ExchangeManager import net.taler.wallet.payment.PaymentManager @@ -81,8 +82,8 @@ class MainViewModel( val accountManager: AccountManager = AccountManager(api, viewModelScope) val depositManager: DepositManager = DepositManager(api, viewModelScope) - private val mTransactionsEvent = MutableLiveData>() - val transactionsEvent: LiveData> = mTransactionsEvent + private val mTransactionsEvent = MutableLiveData>() + val transactionsEvent: LiveData> = mTransactionsEvent private val mScanCodeEvent = MutableLiveData>() val scanCodeEvent: LiveData> = mScanCodeEvent @@ -111,11 +112,11 @@ class MainViewModel( } /** - * Navigates to the given currency's transaction list, when [MainFragment] is shown. + * Navigates to the given scope info's transaction list, when [MainFragment] is shown. */ @UiThread - fun showTransactions(currency: String) { - mTransactionsEvent.value = currency.toEvent() + fun showTransactions(scopeInfo: ScopeInfo) { + mTransactionsEvent.value = scopeInfo.toEvent() } @UiThread diff --git a/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt b/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt index a25c352..7b9e985 100644 --- a/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt @@ -51,6 +51,7 @@ import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import net.taler.common.Amount +import net.taler.common.CurrencySpecification import net.taler.wallet.compose.AmountInputField import net.taler.wallet.compose.TalerSurface import net.taler.wallet.exchanges.ExchangeItem @@ -59,16 +60,20 @@ class ReceiveFundsFragment : Fragment() { private val model: MainViewModel by activityViewModels() private val exchangeManager get() = model.exchangeManager private val withdrawManager get() = model.withdrawManager + private val balanceManager get() = model.balanceManager private val peerManager get() = model.peerManager + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?, ): View = ComposeView(requireContext()).apply { setContent { TalerSurface { + val scopeInfo = model.transactionManager.selectedScope ?: error("No scope selected") ReceiveFundsIntro( - model.transactionManager.selectedCurrency ?: error("No currency selected"), + scopeInfo.currency, + model.balanceManager.getSpecForScopeInfo(scopeInfo), this@ReceiveFundsFragment::onManualWithdraw, this@ReceiveFundsFragment::onPeerPull, ) @@ -113,6 +118,7 @@ class ReceiveFundsFragment : Fragment() { @Composable private fun ReceiveFundsIntro( currency: String, + spec: CurrencySpecification?, onManualWithdraw: (Amount) -> Unit, onPeerPull: (Amount) -> Unit, ) { @@ -146,7 +152,7 @@ private fun ReceiveFundsIntro( ) Text( modifier = Modifier, - text = currency, + text = spec?.symbol(Amount.zero(currency)) ?: currency, softWrap = false, style = MaterialTheme.typography.titleLarge, ) @@ -189,6 +195,6 @@ private fun ReceiveFundsIntro( @Composable fun PreviewReceiveFundsIntro() { Surface { - ReceiveFundsIntro("TESTKUDOS", {}) {} + ReceiveFundsIntro("TESTKUDOS", null, {}) {} } } diff --git a/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt b/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt index 09d33e2..e4df8d8 100644 --- a/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/SendFundsFragment.kt @@ -48,11 +48,13 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController import net.taler.common.Amount +import net.taler.common.CurrencySpecification import net.taler.wallet.compose.AmountInputField import net.taler.wallet.compose.TalerSurface class SendFundsFragment : Fragment() { private val model: MainViewModel by activityViewModels() + private val balanceManager get() = model.balanceManager private val peerManager get() = model.peerManager override fun onCreateView( @@ -62,9 +64,10 @@ class SendFundsFragment : Fragment() { ): View = ComposeView(requireContext()).apply { setContent { TalerSurface { + val scopeInfo = model.transactionManager.selectedScope ?: error("No scope selected") SendFundsIntro( - currency = model.transactionManager.selectedCurrency - ?: error("No currency selected"), + currency = scopeInfo.currency, + spec = balanceManager.getSpecForScopeInfo(scopeInfo), hasSufficientBalance = model::hasSufficientBalance, onDeposit = this@SendFundsFragment::onDeposit, onPeerPush = this@SendFundsFragment::onPeerPush, @@ -93,6 +96,7 @@ class SendFundsFragment : Fragment() { @Composable private fun SendFundsIntro( currency: String, + spec: CurrencySpecification?, hasSufficientBalance: (Amount) -> Boolean, onDeposit: (Amount) -> Unit, onPeerPush: (Amount) -> Unit, @@ -132,7 +136,7 @@ private fun SendFundsIntro( ) Text( modifier = Modifier, - text = currency, + text = spec?.symbol(Amount.zero(currency)) ?: currency, softWrap = false, style = MaterialTheme.typography.titleLarge, ) @@ -185,6 +189,6 @@ private fun SendFundsIntro( @Composable fun PreviewSendFundsIntro() { Surface { - SendFundsIntro("TESTKUDOS", { true }, {}) {} + SendFundsIntro("TESTKUDOS", null, { true }, {}) {} } } diff --git a/wallet/src/main/java/net/taler/wallet/balances/BalanceAdapter.kt b/wallet/src/main/java/net/taler/wallet/balances/BalanceAdapter.kt index c7ee859..f40def4 100644 --- a/wallet/src/main/java/net/taler/wallet/balances/BalanceAdapter.kt +++ b/wallet/src/main/java/net/taler/wallet/balances/BalanceAdapter.kt @@ -65,7 +65,7 @@ class BalanceAdapter(private val listener: BalanceClickListener) : Adapter(null) private val mSelectedTransaction = MutableLiveData(null) val selectedTransaction: LiveData = mSelectedTransaction - private val allTransactions = HashMap>() - private val mTransactions = HashMap>() + private val allTransactions = HashMap>() + private val mTransactions = HashMap>() val transactions: LiveData @UiThread get() = searchQuery.switchMap { query -> - val currency = selectedCurrency - check(currency != null) { "Did not select currency before getting transactions" } + val scopeInfo = selectedScope + check(scopeInfo != null) { "Did not select scope before getting transactions" } loadTransactions(query) - mTransactions[currency]!! // non-null because filled in [loadTransactions] + mTransactions[scopeInfo]!! // non-null because filled in [loadTransactions] } @UiThread fun loadTransactions(searchQuery: String? = null) = scope.launch { - val currency = selectedCurrency ?: return@launch - val liveData = mTransactions.getOrPut(currency) { MutableLiveData() } - if (searchQuery == null && allTransactions.containsKey(currency)) { - liveData.value = TransactionsResult.Success(allTransactions[currency]!!) + val scopeInfo = selectedScope ?: return@launch + val liveData = mTransactions.getOrPut(scopeInfo) { MutableLiveData() } + if (searchQuery == null && allTransactions.containsKey(scopeInfo)) { + liveData.value = TransactionsResult.Success(allTransactions[scopeInfo]!!) } if (liveData.value == null) mProgress.value = true api.request("getTransactions", Transactions.serializer()) { if (searchQuery != null) put("search", searchQuery) - put("currency", currency) + put("scopeInfo", JSONObject(Json.encodeToString(scopeInfo))) }.onError { liveData.postValue(TransactionsResult.Error(it)) mProgress.postValue(false) @@ -91,8 +95,8 @@ class TransactionManager( mSelectedTransaction.value = it } - // update all transactions on UiThread if there was a currency - if (searchQuery == null) allTransactions[currency] = transactions + // update all transactions on UiThread if there was a scope info + if (searchQuery == null) allTransactions[scopeInfo] = transactions } } @@ -201,7 +205,7 @@ class TransactionManager( } fun deleteTransactions(transactionIds: List, onError: (it: TalerErrorInfo) -> Unit) { - allTransactions[selectedCurrency]?.filter { transaction -> + allTransactions[selectedScope]?.filter { transaction -> transaction.transactionId in transactionIds }?.forEach { toBeDeletedTx -> if (Delete in toBeDeletedTx.txActions) { diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt index 2e97484..5243427 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionsFragment.kt @@ -59,7 +59,7 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. private lateinit var ui: FragmentTransactionsBinding private val transactionAdapter by lazy { TransactionAdapter(this) } - private val currency by lazy { transactionManager.selectedCurrency!! } + private val scopeInfo by lazy { transactionManager.selectedScope!! } private var tracker: SelectionTracker? = null private var actionMode: ActionMode? = null @@ -114,8 +114,7 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. // hide extra fab when in single currency mode (uses MainFragment's FAB) if (balances.size == 1) ui.mainFab.visibility = INVISIBLE - // TODO: find via scopeInfo instead of currency - balances.find { it.currency == currency }?.let { balance -> + balances.find { it.scopeInfo == scopeInfo }?.let { balance -> ui.amount.text = balance.available.toString(showSymbol = false) transactionAdapter.setCurrencySpec(balance.available.spec) } @@ -154,7 +153,7 @@ class TransactionsFragment : Fragment(), OnTransactionClickListener, ActionMode. override fun onStart() { super.onStart() - requireActivity().title = getString(R.string.transactions_detail_title_currency, currency) + requireActivity().title = getString(R.string.transactions_detail_title_currency, scopeInfo.currency) } private fun setupSearch(item: MenuItem) { -- cgit v1.2.3