|
| 1 | +/* |
| 2 | + * Copyright 2023 ACINQ SAS |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package fr.acinq.phoenix.android.components |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.Column |
| 20 | +import androidx.compose.foundation.layout.PaddingValues |
| 21 | +import androidx.compose.foundation.layout.Spacer |
| 22 | +import androidx.compose.foundation.layout.height |
| 23 | +import androidx.compose.material.MaterialTheme |
| 24 | +import androidx.compose.material.Slider |
| 25 | +import androidx.compose.material.SliderDefaults |
| 26 | +import androidx.compose.runtime.* |
| 27 | +import androidx.compose.ui.Modifier |
| 28 | +import androidx.compose.ui.graphics.Color |
| 29 | +import androidx.compose.ui.platform.LocalContext |
| 30 | +import androidx.compose.ui.unit.dp |
| 31 | +import fr.acinq.bitcoin.Satoshi |
| 32 | +import fr.acinq.lightning.utils.sat |
| 33 | +import fr.acinq.phoenix.android.R |
| 34 | +import fr.acinq.phoenix.android.components.feedback.ErrorMessage |
| 35 | +import kotlin.math.log10 |
| 36 | +import kotlin.math.pow |
| 37 | + |
| 38 | +/** A logarithmic slider to get a Satoshi value. Can be used to get a feerate for example. */ |
| 39 | +@Composable |
| 40 | +fun SatoshiLogSlider( |
| 41 | + modifier: Modifier = Modifier, |
| 42 | + amount: Satoshi, |
| 43 | + onAmountChange: (Satoshi) -> Unit, |
| 44 | + minAmount: Satoshi = 1.sat, |
| 45 | + maxAmount: Satoshi = 500.sat, |
| 46 | + enabled: Boolean = true, |
| 47 | + steps: Int = 30, |
| 48 | +) { |
| 49 | + val context = LocalContext.current |
| 50 | + val minAmountLog = remember { log10(minAmount.sat.toFloat()) } |
| 51 | + val maxAmountLog = remember { log10(maxAmount.sat.toFloat()) } |
| 52 | + var amountLog by remember { mutableStateOf(log10(amount.sat.toFloat())) } |
| 53 | + |
| 54 | + var errorMessage by remember { mutableStateOf("") } |
| 55 | + |
| 56 | + Column(modifier = modifier.enableOrFade(enabled)) { |
| 57 | + Slider( |
| 58 | + value = amountLog, |
| 59 | + onValueChange = { |
| 60 | + errorMessage = "" |
| 61 | + try { |
| 62 | + amountLog = it |
| 63 | + val valueSat = 10f.pow(it).toLong().sat |
| 64 | + onAmountChange(valueSat) |
| 65 | + } catch (e: Exception) { |
| 66 | + errorMessage = context.getString(R.string.validation_invalid_number) |
| 67 | + } |
| 68 | + }, |
| 69 | + valueRange = minAmountLog..maxAmountLog, |
| 70 | + steps = steps, |
| 71 | + enabled = enabled, |
| 72 | + colors = SliderDefaults.colors( |
| 73 | + activeTrackColor = MaterialTheme.colors.primary, |
| 74 | + inactiveTrackColor = MaterialTheme.colors.primary.copy(alpha = 0.4f), |
| 75 | + activeTickColor = MaterialTheme.colors.primary, |
| 76 | + inactiveTickColor = Color.Transparent, |
| 77 | + ) |
| 78 | + ) |
| 79 | + |
| 80 | + errorMessage.takeUnless { it.isBlank() }?.let { |
| 81 | + Spacer(Modifier.height(4.dp)) |
| 82 | + ErrorMessage(header = it, padding = PaddingValues(0.dp)) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments