summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/exchanges/SelectExchangeDialogFragment.kt
blob: 136738b646c03eabc7a45747956b9c47769e94d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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_select)
            .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,
        )
    )
}