diff options
author | Iván Ávalos <avalos@disroot.org> | 2024-01-31 13:16:19 -0600 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2024-02-06 09:58:38 -0300 |
commit | da098a6bd5c7cdc4722a24ac28e0a87334341bbb (patch) | |
tree | a7534ca7f31ba82f92db8e9d1c96249de8e01864 /wallet/src/main/java/net/taler/wallet/exchanges | |
parent | 8062b84d95f759e435d6248c3523ac5c94dce743 (diff) | |
download | taler-android-da098a6bd5c7cdc4722a24ac28e0a87334341bbb.tar.gz taler-android-da098a6bd5c7cdc4722a24ac28e0a87334341bbb.tar.bz2 taler-android-da098a6bd5c7cdc4722a24ac28e0a87334341bbb.zip |
[wallet] Replace exchange selection fragment with dialog decoupled from withdrawals
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/exchanges')
-rw-r--r-- | wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeDialogFragment.kt | 111 | ||||
-rw-r--r-- | wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeFragment.kt | 55 |
2 files changed, 111 insertions, 55 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeDialogFragment.kt b/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeDialogFragment.kt new file mode 100644 index 0000000..2da7618 --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeDialogFragment.kt @@ -0,0 +1,111 @@ +/* + * This file is part of GNU Taler + * (C) 2024 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +package net.taler.wallet.exchanges + +import android.app.Dialog +import android.os.Bundle +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.ListItem +import androidx.compose.material3.ListItemDefaults +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.res.stringResource +import androidx.fragment.app.DialogFragment +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.asFlow +import com.google.accompanist.themeadapter.material3.Mdc3Theme +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import net.taler.common.Event +import net.taler.common.toEvent +import net.taler.wallet.R +import net.taler.wallet.cleanExchange +import net.taler.wallet.compose.collectAsStateLifecycleAware + +class SelectExchangeDialogFragment: DialogFragment() { + private var exchangeList = MutableLiveData<List<ExchangeItem>>() + + private var mExchangeSelection = MutableLiveData<Event<ExchangeItem>>() + val exchangeSelection: LiveData<Event<ExchangeItem>> = mExchangeSelection + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val view = ComposeView(requireContext()).apply { + setContent { + val exchanges = exchangeList.asFlow().collectAsStateLifecycleAware(initial = emptyList()) + SelectExchangeComposable(exchanges.value) { + onExchangeSelected(it) + } + } + } + + return MaterialAlertDialogBuilder(requireContext(), R.style.MaterialAlertDialog_Material3) + .setIcon(R.drawable.ic_account_balance) + .setTitle(R.string.exchange_list_add) + .setView(view) + .setNegativeButton(R.string.cancel) { _, _ -> + dismiss() + } + .create() + } + + fun setExchanges(exchanges: List<ExchangeItem>) { + exchangeList.value = exchanges + } + + private fun onExchangeSelected(exchange: ExchangeItem) { + mExchangeSelection.value = exchange.toEvent() + dismiss() + } +} + +@Composable +fun SelectExchangeComposable( + exchanges: List<ExchangeItem>, + onExchangeSelected: (exchange: ExchangeItem) -> Unit, +) { + Mdc3Theme { + LazyColumn( + modifier = Modifier.fillMaxSize(), + ) { + items(exchanges) { + ExchangeItemComposable(it) { + onExchangeSelected(it) + } + } + } + } +} + +@Composable +fun ExchangeItemComposable(exchange: ExchangeItem, onSelected: () -> Unit) { + ListItem( + modifier = Modifier.clickable { onSelected() }, + headlineContent = { Text(cleanExchange(exchange.exchangeBaseUrl)) }, + supportingContent = exchange.currency?.let { + { Text(stringResource(R.string.exchange_list_currency, it)) } + }, + colors = ListItemDefaults.colors( + containerColor = Color.Transparent, + ) + ) +}
\ No newline at end of file diff --git a/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeFragment.kt b/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeFragment.kt deleted file mode 100644 index 732e0c5..0000000 --- a/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeFragment.kt +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is part of GNU Taler - * (C) 2020 Taler Systems S.A. - * - * GNU Taler is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 3, or (at your option) any later version. - * - * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> - */ - -package net.taler.wallet.exchanges - -import androidx.navigation.fragment.findNavController -import net.taler.common.fadeOut -import net.taler.wallet.withdraw.WithdrawStatus.ReceivedDetails - -class SelectExchangeFragment : ExchangeListFragment() { - - private val withdrawManager by lazy { model.withdrawManager } - - override val isSelectOnly = true - private val exchangeSelection by lazy { - requireNotNull(withdrawManager.exchangeSelection.value?.getEvenIfConsumedAlready()) - } - - override fun onExchangeUpdate(exchanges: List<ExchangeItem>) { - ui.progressBar.fadeOut() - val status = withdrawManager.withdrawStatus.value - if (status is ReceivedDetails) { - super.onExchangeUpdate(status.possibleExchanges) - } else { - findNavController().navigateUp() - } - } - - override fun onExchangeSelected(item: ExchangeItem) { - val status = withdrawManager.withdrawStatus.value - val exchanges = (status as? ReceivedDetails)?.possibleExchanges ?: emptyList() - withdrawManager.getWithdrawalDetails( - exchangeBaseUrl = item.exchangeBaseUrl, - amount = exchangeSelection.amount, - showTosImmediately = true, - uri = exchangeSelection.talerWithdrawUri, - possibleExchanges = exchanges, - ) - findNavController().navigateUp() - } - -} |