PaymentOptionsFragment.kt
/*
* This file is part of GNU Taler
* (C) 2026 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.merchantpos.paymentoptions
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import net.taler.merchantpos.MainActivity
import net.taler.merchantpos.MainViewModel
import net.taler.merchantpos.PosDestination
import net.taler.merchantpos.R
import net.taler.merchantpos.compose.PosOutlinedCard
import net.taler.merchantpos.compose.PosTheme
class PaymentOptionsFragment : Fragment()
{
private val viewModel: MainViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View =
ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
val manager = viewModel.paymentOptions
setContent {
val summary by manager.summary.observeAsState(PaymentOptionsSummary())
PaymentOptionsScreen(
mixedPaymentsUnsupportedBy =
viewModel.configManager.backendVersion
?.takeUnless { viewModel.checkoutManager.backendSupportsMixedPayments() },
options = manager.options,
summary = summary,
onEnabled = manager::setEnabled,
onOpen = { optionId ->
(requireActivity() as MainActivity).navigateTo(
PosDestination.PaymentOptionDetail.forOption(optionId),
)
},
)
}
}
}
@Composable
private fun PaymentOptionsScreen(
mixedPaymentsUnsupportedBy: String?,
options: List<PaymentOption>,
summary: PaymentOptionsSummary,
onEnabled: (String, Boolean) -> Unit,
onOpen: (String) -> Unit,
)
{
var pending by remember { mutableStateOf<Pair<PaymentOption, Int>?>(null) }
PosTheme {
pending?.let { (option, noticeRes) ->
ActivationNoticeDialog(
noticeRes = noticeRes,
onConfirm = {
pending = null
onEnabled(option.id, true)
},
onDismiss = { pending = null },
)
}
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.TopCenter) {
LazyColumn(
modifier = Modifier.widthIn(max = 960.dp).fillMaxSize().padding(20.dp),
verticalArrangement = Arrangement.spacedBy(14.dp),
) {
item {
Text(
text = stringResource(R.string.payment_options_description),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
if (mixedPaymentsUnsupportedBy != null)
{
item {
Text(
text =
stringResource(
R.string.payment_options_mixed_unsupported,
mixedPaymentsUnsupportedBy,
),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.error,
)
}
}
items(options, key = { it.id }) { option ->
val state =
summary.options.firstOrNull { it.id == option.id }
?: PaymentOptionState(option.id)
PosOutlinedCard(
modifier = Modifier.clickable { onOpen(option.id) },
contentPadding = PaddingValues(16.dp),
) { contentPadding ->
Column(modifier = Modifier.padding(contentPadding)) {
PaymentOptionRow(
option = option,
state = state,
onEnabled = { enable ->
val notice = option.activationNoticeRes
if (enable &&
notice != null)
{
pending = option to notice
}
else
{
onEnabled(option.id, enable)
}
},
)
}
}
}
}
}
}
}
/** Production payment-options list with offline fixture state for screenshot rendering. */
@Composable
internal fun PaymentOptionsScreenshotContent(
options: List<PaymentOption>,
summary: PaymentOptionsSummary,
)
{
PaymentOptionsScreen(
mixedPaymentsUnsupportedBy = null,
options = options,
summary = summary,
onEnabled = { _, _ -> },
onOpen = {},
)
}