SelectExchangeDialogFragment.kt (3988B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2024 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.exchanges 18 19 import android.app.Dialog 20 import android.os.Bundle 21 import androidx.compose.foundation.clickable 22 import androidx.compose.foundation.layout.fillMaxSize 23 import androidx.compose.foundation.lazy.LazyColumn 24 import androidx.compose.foundation.lazy.items 25 import androidx.compose.material3.ListItem 26 import androidx.compose.material3.ListItemDefaults 27 import androidx.compose.material3.Text 28 import androidx.compose.runtime.Composable 29 import androidx.compose.ui.Modifier 30 import androidx.compose.ui.graphics.Color 31 import androidx.compose.ui.platform.ComposeView 32 import androidx.compose.ui.res.stringResource 33 import androidx.fragment.app.DialogFragment 34 import androidx.lifecycle.LiveData 35 import androidx.lifecycle.MutableLiveData 36 import androidx.lifecycle.asFlow 37 import com.google.accompanist.themeadapter.material3.Mdc3Theme 38 import com.google.android.material.dialog.MaterialAlertDialogBuilder 39 import net.taler.common.Event 40 import net.taler.common.toEvent 41 import net.taler.wallet.R 42 import net.taler.wallet.cleanExchange 43 import net.taler.wallet.compose.collectAsStateLifecycleAware 44 45 class SelectExchangeDialogFragment: DialogFragment() { 46 private var exchangeList = MutableLiveData<List<ExchangeItem>>() 47 48 private var mExchangeSelection = MutableLiveData<Event<ExchangeItem>>() 49 val exchangeSelection: LiveData<Event<ExchangeItem>> = mExchangeSelection 50 51 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 52 val view = ComposeView(requireContext()).apply { 53 setContent { 54 val exchanges = exchangeList.asFlow().collectAsStateLifecycleAware(initial = emptyList()) 55 SelectExchangeComposable(exchanges.value) { 56 onExchangeSelected(it) 57 } 58 } 59 } 60 61 return MaterialAlertDialogBuilder(requireContext(), R.style.MaterialAlertDialog_Material3) 62 .setIcon(R.drawable.ic_account_balance) 63 .setTitle(R.string.exchange_list_select) 64 .setView(view) 65 .setNegativeButton(R.string.cancel) { _, _ -> 66 dismiss() 67 } 68 .create() 69 } 70 71 fun setExchanges(exchanges: List<ExchangeItem>) { 72 exchangeList.value = exchanges 73 } 74 75 private fun onExchangeSelected(exchange: ExchangeItem) { 76 mExchangeSelection.value = exchange.toEvent() 77 dismiss() 78 } 79 } 80 81 @Composable 82 fun SelectExchangeComposable( 83 exchanges: List<ExchangeItem>, 84 onExchangeSelected: (exchange: ExchangeItem) -> Unit, 85 ) { 86 Mdc3Theme { 87 LazyColumn( 88 modifier = Modifier.fillMaxSize(), 89 ) { 90 items(exchanges) { 91 ExchangeItemComposable(it) { 92 onExchangeSelected(it) 93 } 94 } 95 } 96 } 97 } 98 99 @Composable 100 fun ExchangeItemComposable(exchange: ExchangeItem, onSelected: () -> Unit) { 101 ListItem( 102 modifier = Modifier.clickable { onSelected() }, 103 headlineContent = { Text(cleanExchange(exchange.exchangeBaseUrl)) }, 104 supportingContent = exchange.currency?.let { 105 { Text(stringResource(R.string.exchange_list_currency, it)) } 106 }, 107 colors = ListItemDefaults.colors( 108 containerColor = Color.Transparent, 109 ) 110 ) 111 }