ExpirationComposable.kt (4498B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2023 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.peer 18 19 import androidx.compose.foundation.layout.Arrangement 20 import androidx.compose.foundation.layout.Column 21 import androidx.compose.foundation.layout.Row 22 import androidx.compose.foundation.layout.fillMaxWidth 23 import androidx.compose.foundation.layout.padding 24 import androidx.compose.foundation.lazy.LazyRow 25 import androidx.compose.foundation.lazy.items 26 import androidx.compose.material3.Text 27 import androidx.compose.runtime.Composable 28 import androidx.compose.ui.Modifier 29 import androidx.compose.ui.res.stringResource 30 import androidx.compose.ui.tooling.preview.Preview 31 import androidx.compose.ui.unit.dp 32 import net.taler.wallet.R 33 import net.taler.wallet.compose.NumericInputField 34 import net.taler.wallet.compose.SelectionChip 35 import net.taler.wallet.compose.TalerSurface 36 37 enum class ExpirationOption(val hours: Long) { 38 DAYS_1(24), 39 DAYS_7(24 * 7), 40 DAYS_30(24 * 30), 41 CUSTOM(-1) 42 } 43 44 @Composable 45 fun ExpirationComposable( 46 modifier: Modifier = Modifier, 47 option: ExpirationOption, 48 hours: Long, 49 onOptionChange: (ExpirationOption) -> Unit, 50 onHoursChange: (Long) -> Unit, 51 ) { 52 val options = listOf( 53 ExpirationOption.DAYS_1 to stringResource(R.string.send_peer_expiration_1d), 54 ExpirationOption.DAYS_7 to stringResource(R.string.send_peer_expiration_7d), 55 ExpirationOption.DAYS_30 to stringResource(R.string.send_peer_expiration_30d), 56 ExpirationOption.CUSTOM to stringResource(R.string.send_peer_expiration_custom), 57 ) 58 Column( 59 modifier = modifier, 60 ) { 61 LazyRow( 62 modifier = Modifier.fillMaxWidth(), 63 horizontalArrangement = Arrangement.SpaceBetween, 64 ) { 65 items(items = options, key = { it.first }) { 66 SelectionChip( 67 label = { Text(it.second) }, 68 modifier = Modifier.padding(horizontal = 4.dp), 69 selected = it.first == option, 70 value = it.first, 71 onSelected = { o -> 72 onOptionChange(o) 73 if (o != ExpirationOption.CUSTOM) { 74 onHoursChange(o.hours) 75 } 76 }, 77 ) 78 } 79 } 80 81 if (option == ExpirationOption.CUSTOM) { 82 val d = hours / 24L 83 val h = hours - d * 24L 84 Row( 85 modifier = Modifier.fillMaxWidth(), 86 ) { 87 NumericInputField( 88 modifier = Modifier 89 .fillMaxWidth() 90 .weight(1f) 91 .padding(end = 4.dp), 92 value = d, 93 onValueChange = { 94 onHoursChange(it * 24 + h) 95 }, 96 label = { Text(stringResource(R.string.send_peer_expiration_days)) }, 97 maxValue = 365, 98 ) 99 NumericInputField( 100 modifier = Modifier 101 .fillMaxWidth() 102 .weight(1f) 103 .padding(start = 4.dp), 104 value = h, 105 onValueChange = { 106 onHoursChange(d * 24 + it) 107 }, 108 label = { Text(stringResource(R.string.send_peer_expiration_hours)) }, 109 maxValue = 23, 110 ) 111 } 112 } 113 } 114 } 115 116 @Preview 117 @Composable 118 fun ExpirationComposablePreview() { 119 TalerSurface { 120 var option = ExpirationOption.CUSTOM 121 var hours = 25L 122 ExpirationComposable( 123 option = option, 124 hours = hours, 125 onOptionChange = { option = it } 126 ) { hours = it } 127 } 128 }