|
1 | 1 | package com.smarttoolfactory.colorpicker.widget |
2 | 2 |
|
3 | | -import androidx.compose.foundation.layout.padding |
4 | 3 | import androidx.compose.foundation.layout.widthIn |
5 | | -import androidx.compose.foundation.layout.wrapContentHeight |
6 | 4 | import androidx.compose.foundation.shape.RoundedCornerShape |
7 | | -import androidx.compose.foundation.text.BasicTextField |
8 | 5 | import androidx.compose.material.* |
9 | 6 | import androidx.compose.runtime.Composable |
10 | 7 | import androidx.compose.ui.Modifier |
11 | 8 | import androidx.compose.ui.draw.drawBehind |
12 | 9 | import androidx.compose.ui.geometry.Offset |
13 | 10 | import androidx.compose.ui.graphics.Color |
14 | 11 | import androidx.compose.ui.graphics.Shape |
15 | | -import androidx.compose.ui.graphics.SolidColor |
16 | 12 | import androidx.compose.ui.text.AnnotatedString |
17 | 13 | import androidx.compose.ui.text.TextStyle |
18 | 14 | import androidx.compose.ui.text.input.OffsetMapping |
@@ -78,68 +74,29 @@ fun HexTextField( |
78 | 74 | onValueChange = { |
79 | 75 |
|
80 | 76 | if (it.length <= if (useAlpha) 8 else 6) { |
81 | | - if (it.isNotEmpty()) { |
82 | | - val lastChar = it.last() |
83 | | - val isHexChar = hexRegexSingleChar.matches(lastChar.toString()) |
84 | | - if (isHexChar) { |
85 | | - onTextChange(it) |
86 | | - } |
87 | | - } else { |
88 | | - onTextChange(it) |
89 | | - } |
90 | 77 |
|
91 | | - // Hex String with 6 or 8 chars matches a Color |
92 | | - if (hex.matches(it)) { |
93 | | - onColorChange(hexToColor(it)) |
| 78 | + println("😍 CurrentString: $it") |
| 79 | + var validHex = true |
| 80 | + |
| 81 | + for (index in it.indices) { |
| 82 | + validHex = hexRegexSingleChar.matches(it[index].toString()) |
| 83 | + println("🎃 CHART: ${it[index]}, validHex: $validHex") |
| 84 | + if (!validHex) break |
94 | 85 | } |
95 | | - } |
96 | | - } |
97 | | - ) |
98 | | -} |
99 | 86 |
|
100 | | -@Composable |
101 | | -private fun BasicHexTextField( |
102 | | - modifier: Modifier = Modifier, |
103 | | - value: String, |
104 | | - textStyle: TextStyle = TextStyle( |
105 | | - fontSize = 20.sp, |
106 | | - ), |
107 | | - textColor: Color = Color.Black, |
108 | | - backgroundColor: Color = Color.LightGray, |
109 | | - cursorColor: Color = MaterialTheme.colors.primary, |
110 | | - visualTransformation: VisualTransformation, |
111 | | - hint: String, |
112 | | - shape: Shape = RoundedCornerShape(25), |
113 | | - onValueChange: (String) -> Unit, |
114 | | -) { |
| 87 | + println("💬 TEXT: $it, validHex: $validHex") |
115 | 88 |
|
116 | | - Surface( |
117 | | - modifier = modifier |
118 | | - .wrapContentHeight() |
119 | | - .widthIn(min = 60.dp), |
120 | | - shape = shape, |
121 | | - color = backgroundColor, |
122 | | - contentColor = textColor |
123 | | - ) { |
124 | | - BasicTextField( |
125 | | - textStyle = textStyle, |
126 | | - value = value, |
127 | | - singleLine = true, |
128 | | - visualTransformation = visualTransformation, |
129 | | - cursorBrush = SolidColor(cursorColor), |
130 | | - onValueChange = onValueChange |
131 | | - ) { |
132 | | - if (value.isEmpty()) { |
133 | | - Text( |
134 | | - modifier = Modifier.padding(4.dp), |
135 | | - text = hint, |
136 | | - style = textStyle.copy(textStyle.color.copy(.7f)) |
137 | | - ) |
138 | | - } else { |
139 | | - it() |
| 89 | + if (validHex) { |
| 90 | + onTextChange(it) |
| 91 | + |
| 92 | + // Hex String with 6 or 8 chars matches a Color |
| 93 | + if (hex.matches(it)) { |
| 94 | + onColorChange(hexToColor(it)) |
| 95 | + } |
| 96 | + } |
140 | 97 | } |
141 | 98 | } |
142 | | - } |
| 99 | + ) |
143 | 100 | } |
144 | 101 |
|
145 | 102 | private class HexVisualTransformation(private val useAlpha: Boolean) : VisualTransformation { |
|
0 commit comments