Skip to content

Commit

Permalink
Fix #176
Browse files Browse the repository at this point in the history
  • Loading branch information
sadellie committed Jan 31, 2024
1 parent 087ba19 commit a88aa90
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ package com.sadellie.unitto.core.ui.common.textfield
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.input.TextFieldValue
import com.sadellie.unitto.core.base.Token

/**
* Copy value to clipboard without grouping symbols.
* Copy value to clipboard with fractional symbols.
*
* Example:
* "123.456,789" will be copied as "123456,789"
* "123456.789" will be copied as "123456,789"
*
* @param value Formatted value that has grouping symbols.
* @param value Internal [TextFieldValue] without formatting with [Token.Digit.dot] as fractional.
*/
internal fun ClipboardManager.copyWithoutGrouping(
internal fun ClipboardManager.copyWithFractional(
value: TextFieldValue,
formatterSymbols: FormatterSymbols
) = this.setText(
AnnotatedString(
value.annotatedString
.subSequence(value.selection)
.text
.replace(formatterSymbols.grouping, "")
.replace(Token.Digit.dot, formatterSymbols.fractional)
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun FixedInputTextField(
val expressionInteractionSource = remember(expression) { MutableInteractionSource() }
LaunchedEffect(expressionInteractionSource) {
expressionInteractionSource.interactions.collect {
if (it is PressInteraction.Release) onClick(expression.clearAndFilterExpression(formatterSymbols))
if (it is PressInteraction.Release) onClick(value)
}
}

Expand All @@ -71,7 +71,7 @@ fun FixedInputTextField(
LocalTextToolbar provides UnittoTextToolbar(
view = LocalView.current,
copyCallback = {
clipboardManager.copyWithoutGrouping(expressionValue, formatterSymbols)
clipboardManager.copyWithFractional(expressionValue, formatterSymbols)
expressionValue = expressionValue.copy(selection = TextRange(expressionValue.selection.end))
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun ExpressionTextField(
val expressionTransformer = remember(formatterSymbols) { ExpressionTransformer(formatterSymbols) }

fun copyCallback() {
clipboardManager.copyWithoutGrouping(value, formatterSymbols)
clipboardManager.copyWithFractional(value, formatterSymbols)
onCursorChange(TextRange(value.selection.end))
}

Expand All @@ -90,7 +90,7 @@ fun ExpressionTextField(
pasteCallback(clipboardManager.getText()?.text?.clearAndFilterExpression(formatterSymbols) ?: "")
},
cutCallback = {
clipboardManager.copyWithoutGrouping(value, formatterSymbols)
clipboardManager.copyWithFractional(value, formatterSymbols)
cutCallback()
}
)
Expand Down

0 comments on commit a88aa90

Please sign in to comment.