mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-21 09:40:37 +02:00
No more strange hardcoded symbols for TextFieldController
This commit is contained in:
parent
ce614aa172
commit
a1333f5746
@ -111,7 +111,7 @@ open class UnittoFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove formatting. Reverses [format]
|
* Reapply formatting. Reverses [format] and applies [format] again.
|
||||||
*/
|
*/
|
||||||
fun reFormat(input: String): String {
|
fun reFormat(input: String): String {
|
||||||
// We get 123.45,6789
|
// We get 123.45,6789
|
||||||
@ -121,11 +121,30 @@ open class UnittoFormatter {
|
|||||||
// Remove grouping
|
// Remove grouping
|
||||||
// 12345,6789
|
// 12345,6789
|
||||||
// Replace fractional with "." because formatter accepts only numbers where fractional is a dot
|
// Replace fractional with "." because formatter accepts only numbers where fractional is a dot
|
||||||
|
return format(removeFormat(input))
|
||||||
|
}
|
||||||
|
|
||||||
val cleanString = input
|
/**
|
||||||
|
* Helper method to change formatting from [input] with a specified [separator] to the one that
|
||||||
|
* is set for this [UnittoFormatter].
|
||||||
|
*/
|
||||||
|
fun fromSeparator(input: String, separator: Int): String {
|
||||||
|
val sGrouping = when (separator) {
|
||||||
|
Separator.PERIOD -> PERIOD
|
||||||
|
Separator.COMMA -> COMMA
|
||||||
|
else -> SPACE
|
||||||
|
}
|
||||||
|
val sFractional = if (separator == Separator.PERIOD) KEY_COMMA else KEY_DOT
|
||||||
|
|
||||||
|
return input
|
||||||
|
.replace(sGrouping, grouping)
|
||||||
|
.replace(sFractional, fractional)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeFormat(input: String): String {
|
||||||
|
return input
|
||||||
.replace(grouping, "")
|
.replace(grouping, "")
|
||||||
.replace(fractional, KEY_DOT)
|
.replace(fractional, KEY_DOT)
|
||||||
return format(cleanString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,6 +58,7 @@ import com.sadellie.unitto.core.base.KEY_RIGHT_BRACKET
|
|||||||
import com.sadellie.unitto.core.base.KEY_SIN
|
import com.sadellie.unitto.core.base.KEY_SIN
|
||||||
import com.sadellie.unitto.core.base.KEY_SQRT
|
import com.sadellie.unitto.core.base.KEY_SQRT
|
||||||
import com.sadellie.unitto.core.base.KEY_TAN
|
import com.sadellie.unitto.core.base.KEY_TAN
|
||||||
|
import com.sadellie.unitto.core.base.Separator
|
||||||
import com.sadellie.unitto.core.ui.UnittoFormatter
|
import com.sadellie.unitto.core.ui.UnittoFormatter
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
@ -73,8 +74,7 @@ class TextFieldController @Inject constructor() {
|
|||||||
// react to formatting preferences at this level.
|
// react to formatting preferences at this level.
|
||||||
private val localFormatter: UnittoFormatter by lazy {
|
private val localFormatter: UnittoFormatter by lazy {
|
||||||
UnittoFormatter().also {
|
UnittoFormatter().also {
|
||||||
it.grouping = "`"
|
it.setSeparator(Separator.COMMA)
|
||||||
it.fractional = "|"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ import androidx.compose.ui.platform.LocalView
|
|||||||
import androidx.compose.ui.text.AnnotatedString
|
import androidx.compose.ui.text.AnnotatedString
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
|
import com.sadellie.unitto.core.base.Separator
|
||||||
import com.sadellie.unitto.core.ui.Formatter
|
import com.sadellie.unitto.core.ui.Formatter
|
||||||
import com.sadellie.unitto.core.ui.theme.NumbersTextStyleDisplayLarge
|
import com.sadellie.unitto.core.ui.theme.NumbersTextStyleDisplayLarge
|
||||||
|
|
||||||
@ -49,19 +50,15 @@ internal fun InputTextField(
|
|||||||
derivedStateOf {
|
derivedStateOf {
|
||||||
value.copy(
|
value.copy(
|
||||||
// We replace this because internally input value is already formatted, but uses
|
// We replace this because internally input value is already formatted, but uses
|
||||||
// "|" as grouping and "-" as fractional.
|
// COMMA as separator.
|
||||||
value.text
|
Formatter.fromSeparator(value.text, Separator.COMMA)
|
||||||
.replace("`", Formatter.grouping)
|
|
||||||
.replace("|", Formatter.fractional)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun copyToClipboard() = clipboardManager.setText(
|
fun copyToClipboard() = clipboardManager.setText(
|
||||||
AnnotatedString(
|
AnnotatedString(
|
||||||
formattedInput.text
|
Formatter.removeFormat(formattedInput.text)
|
||||||
.replace(Formatter.grouping, "")
|
|
||||||
.replace(Formatter.fractional, ".")
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,8 +29,6 @@ internal class TextFieldControllerTest {
|
|||||||
|
|
||||||
private val TextFieldController.text: String
|
private val TextFieldController.text: String
|
||||||
get() = this.input.value.text
|
get() = this.input.value.text
|
||||||
.replace("`", ",")
|
|
||||||
.replace("|", ".")
|
|
||||||
|
|
||||||
private val TextFieldController.selection: IntRange
|
private val TextFieldController.selection: IntRange
|
||||||
get() = this.input.value.selection.start..this.input.value.selection.end
|
get() = this.input.value.selection.start..this.input.value.selection.end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user