|
| 1 | +package ru.svolf.convertx.presentation.compose |
| 2 | + |
| 3 | +import android.content.ClipData |
| 4 | +import androidx.compose.animation.animateColorAsState |
| 5 | +import androidx.compose.animation.core.tween |
| 6 | +import androidx.compose.foundation.background |
| 7 | +import androidx.compose.foundation.clickable |
| 8 | +import androidx.compose.foundation.layout.Arrangement |
| 9 | +import androidx.compose.foundation.layout.Box |
| 10 | +import androidx.compose.foundation.layout.Column |
| 11 | +import androidx.compose.foundation.layout.Row |
| 12 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 13 | +import androidx.compose.foundation.layout.height |
| 14 | +import androidx.compose.foundation.layout.Spacer |
| 15 | +import androidx.compose.foundation.layout.size |
| 16 | +import androidx.compose.foundation.layout.width |
| 17 | +import androidx.compose.material.icons.Icons |
| 18 | +import androidx.compose.material.icons.filled.Check |
| 19 | +import androidx.compose.material.icons.filled.Clear |
| 20 | +import androidx.compose.material.icons.filled.ContentCopy |
| 21 | +import androidx.compose.material.icons.filled.KeyboardArrowDown |
| 22 | +import androidx.compose.material.icons.filled.KeyboardArrowUp |
| 23 | +import androidx.compose.material3.AssistChip |
| 24 | +import androidx.compose.material3.DropdownMenu |
| 25 | +import androidx.compose.material3.DropdownMenuItem |
| 26 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 27 | +import androidx.compose.material3.ExposedDropdownMenuBox |
| 28 | +import androidx.compose.material3.ExposedDropdownMenuDefaults |
| 29 | +import androidx.compose.material3.Icon |
| 30 | +import androidx.compose.material3.MaterialTheme |
| 31 | +import androidx.compose.material3.SnackbarHostState |
| 32 | +import androidx.compose.material3.Text |
| 33 | +import androidx.compose.material3.TextButton |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.runtime.getValue |
| 36 | +import androidx.compose.runtime.mutableStateOf |
| 37 | +import androidx.compose.runtime.remember |
| 38 | +import androidx.compose.runtime.rememberCoroutineScope |
| 39 | +import androidx.compose.runtime.setValue |
| 40 | +import androidx.compose.ui.Alignment |
| 41 | +import androidx.compose.ui.Modifier |
| 42 | +import androidx.compose.ui.platform.ClipEntry |
| 43 | +import androidx.compose.ui.platform.LocalClipboard |
| 44 | +import androidx.compose.ui.res.stringResource |
| 45 | +import androidx.compose.ui.unit.dp |
| 46 | +import kotlinx.coroutines.launch |
| 47 | +import ru.svolf.convertx.R |
| 48 | +import ru.svolf.convertx.presentation.navigation.AppRoute |
| 49 | +import ru.svolf.convertx.presentation.navigation.Base64Route |
| 50 | +import ru.svolf.convertx.presentation.navigation.HexRoute |
| 51 | +import ru.svolf.convertx.presentation.navigation.TextTool |
| 52 | +import ru.svolf.convertx.presentation.navigation.TextToolRoute |
| 53 | +import ru.svolf.convertx.presentation.navigation.UnicodeRoute |
| 54 | + |
| 55 | +internal enum class ConverterField { INPUT, OUTPUT } |
| 56 | + |
| 57 | +@Composable |
| 58 | +internal fun inputHint(route: AppRoute): String = when (route) { |
| 59 | + UnicodeRoute, is Base64Route -> stringResource(R.string.hint_utf) |
| 60 | + is HexRoute -> stringResource(R.string.hint_string) |
| 61 | + is TextToolRoute -> when (route.tool) { |
| 62 | + TextTool.ADLER32 -> stringResource(R.string.hint_adler32) |
| 63 | + TextTool.CRC32 -> stringResource(R.string.hint_crc) |
| 64 | + TextTool.XML -> stringResource(R.string.hint_encoded_xml) |
| 65 | + TextTool.TIMESTAMP -> stringResource(R.string.hint_timestamp) |
| 66 | + } |
| 67 | + |
| 68 | + else -> stringResource(R.string.hint_string) |
| 69 | +} |
| 70 | + |
| 71 | +@Composable |
| 72 | +internal fun outputHint(route: AppRoute): String = when (route) { |
| 73 | + UnicodeRoute -> stringResource(R.string.hint_unicode) |
| 74 | + is Base64Route -> stringResource(R.string.hint_base64) |
| 75 | + is HexRoute -> stringResource(R.string.hint_hex) |
| 76 | + else -> stringResource(R.string.copy2clipboard) |
| 77 | +} |
| 78 | + |
| 79 | +@Composable |
| 80 | +internal fun ConverterControls( |
| 81 | + route: AppRoute, |
| 82 | + selectedMode: Int, |
| 83 | + activeField: ConverterField, |
| 84 | + onFieldToggle: () -> Unit, |
| 85 | + onModeSelected: (Int) -> Unit |
| 86 | +) { |
| 87 | + Box(Modifier.fillMaxWidth().height(48.dp)) { |
| 88 | + DirectionSwitcher(activeField, onFieldToggle, Modifier.align(Alignment.Center)) |
| 89 | + if (route is Base64Route || route is ru.svolf.convertx.presentation.navigation.HexRoute) { |
| 90 | + ModeMenu(route, selectedMode, onModeSelected, Modifier.align(Alignment.CenterStart)) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@Composable |
| 96 | +private fun DirectionSwitcher( |
| 97 | + activeField: ConverterField, |
| 98 | + onToggle: () -> Unit, |
| 99 | + modifier: Modifier = Modifier |
| 100 | +) { |
| 101 | + val primary = MaterialTheme.colorScheme.primary |
| 102 | + val muted = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.35f) |
| 103 | + val downTint by animateColorAsState( |
| 104 | + targetValue = if (activeField == ConverterField.INPUT) primary else muted, |
| 105 | + animationSpec = tween(durationMillis = 220), |
| 106 | + label = "downArrowTint" |
| 107 | + ) |
| 108 | + val upTint by animateColorAsState( |
| 109 | + targetValue = if (activeField == ConverterField.OUTPUT) primary else muted, |
| 110 | + animationSpec = tween(durationMillis = 220), |
| 111 | + label = "upArrowTint" |
| 112 | + ) |
| 113 | + Column( |
| 114 | + modifier = modifier.height(46.dp).clickable(onClick = onToggle), |
| 115 | + horizontalAlignment = Alignment.CenterHorizontally |
| 116 | + ) { |
| 117 | + Box(Modifier.width(2.dp).weight(1f).background(primary.copy(alpha = 0.65f))) |
| 118 | + Row(horizontalArrangement = Arrangement.Center) { |
| 119 | + Icon( |
| 120 | + imageVector = Icons.Default.KeyboardArrowDown, |
| 121 | + contentDescription = null, |
| 122 | + tint = downTint, |
| 123 | + modifier = Modifier.size(20.dp) |
| 124 | + ) |
| 125 | + Icon( |
| 126 | + imageVector = Icons.Default.KeyboardArrowUp, |
| 127 | + contentDescription = null, |
| 128 | + tint = upTint, |
| 129 | + modifier = Modifier.size(20.dp) |
| 130 | + ) |
| 131 | + } |
| 132 | + Box(Modifier.width(2.dp).weight(1f).background(primary.copy(alpha = 0.65f))) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +@OptIn(ExperimentalMaterial3Api::class) |
| 137 | +@Composable |
| 138 | +private fun ModeMenu( |
| 139 | + route: AppRoute, |
| 140 | + selected: Int, |
| 141 | + onSelected: (Int) -> Unit, |
| 142 | + modifier: Modifier = Modifier |
| 143 | +) { |
| 144 | + val options = if (route is Base64Route) { |
| 145 | + listOf("No wrap", "No close", "No padding", "URL safe", "CRLF", "Default") |
| 146 | + } else { |
| 147 | + listOf("String", "Integer") |
| 148 | + } |
| 149 | + var expanded by remember { mutableStateOf(false) } |
| 150 | + ExposedDropdownMenuBox(expanded, { expanded = !expanded }, modifier) { |
| 151 | + AssistChip( |
| 152 | + onClick = { expanded = true }, |
| 153 | + label = { Text(options.getOrElse(selected) { options.first() }) }, |
| 154 | + trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded) }, |
| 155 | + modifier = Modifier.menuAnchor() |
| 156 | + ) |
| 157 | + DropdownMenu(expanded, { expanded = false }) { |
| 158 | + options.forEachIndexed { index, option -> |
| 159 | + DropdownMenuItem( |
| 160 | + text = { Text(option) }, |
| 161 | + onClick = { expanded = false; onSelected(index) }, |
| 162 | + leadingIcon = if (index == selected) { |
| 163 | + { Icon(Icons.Default.Check, null) } |
| 164 | + } else { |
| 165 | + null |
| 166 | + } |
| 167 | + ) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +@Composable |
| 174 | +internal fun ClearButton(onClick: () -> Unit) { |
| 175 | + TextButton(modifier = Modifier.height(48.dp), onClick = onClick) { |
| 176 | + Icon(Icons.Default.Clear, stringResource(R.string.clear)) |
| 177 | + Spacer(Modifier.width(8.dp)) |
| 178 | + Text(stringResource(R.string.clear)) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +@Composable |
| 183 | +internal fun CopyButton(value: String, message: String, snackbar: SnackbarHostState) { |
| 184 | + val clipboard = LocalClipboard.current |
| 185 | + val scope = rememberCoroutineScope() |
| 186 | + TextButton( |
| 187 | + modifier = Modifier.height(48.dp), |
| 188 | + onClick = { |
| 189 | + scope.launch { |
| 190 | + clipboard.setClipEntry(ClipEntry(ClipData.newPlainText(null, value))) |
| 191 | + snackbar.showSnackbar(message) |
| 192 | + } |
| 193 | + } |
| 194 | + ) { |
| 195 | + Icon(Icons.Default.ContentCopy, stringResource(R.string.copy2clipboard)) |
| 196 | + Spacer(Modifier.width(8.dp)) |
| 197 | + Text(stringResource(R.string.copy2clipboard)) |
| 198 | + } |
| 199 | +} |
0 commit comments