mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Continuous calculations in widget
This commit is contained in:
parent
f413e98d27
commit
d9454d8d01
@ -25,6 +25,7 @@ import androidx.compose.ui.unit.TextUnit
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.datastore.preferences.core.Preferences
|
import androidx.datastore.preferences.core.Preferences
|
||||||
|
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.glance.GlanceId
|
import androidx.glance.GlanceId
|
||||||
import androidx.glance.GlanceModifier
|
import androidx.glance.GlanceModifier
|
||||||
@ -75,11 +76,13 @@ class UnittoCalculatorWidget : GlanceAppWidget() {
|
|||||||
companion object {
|
companion object {
|
||||||
val inputKey = ActionParameters.Key<String>("inputKey")
|
val inputKey = ActionParameters.Key<String>("inputKey")
|
||||||
val outputKey = ActionParameters.Key<String>("outputKey")
|
val outputKey = ActionParameters.Key<String>("outputKey")
|
||||||
|
val equalClickedKey = ActionParameters.Key<Boolean>("equalClickedKey")
|
||||||
val precisionKey = ActionParameters.Key<Int>("precisionKey")
|
val precisionKey = ActionParameters.Key<Int>("precisionKey")
|
||||||
val outputFormatKey = ActionParameters.Key<Int>("outputFormatKey")
|
val outputFormatKey = ActionParameters.Key<Int>("outputFormatKey")
|
||||||
|
|
||||||
val inputPrefKey = stringPreferencesKey("GLANCE_INPUT")
|
val inputPrefKey = stringPreferencesKey("GLANCE_INPUT")
|
||||||
val outputPrefKey = stringPreferencesKey("GLANCE_OUTPUT")
|
val outputPrefKey = stringPreferencesKey("GLANCE_OUTPUT")
|
||||||
|
val equalClickedPrefKey = booleanPreferencesKey("GLANCE_EQUAL_CLICKED")
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
||||||
@ -122,10 +125,15 @@ private fun ReadyUI(
|
|||||||
val glancePrefs = currentState<Preferences>()
|
val glancePrefs = currentState<Preferences>()
|
||||||
val input = glancePrefs[UnittoCalculatorWidget.inputPrefKey] ?: ""
|
val input = glancePrefs[UnittoCalculatorWidget.inputPrefKey] ?: ""
|
||||||
val output = glancePrefs[UnittoCalculatorWidget.outputPrefKey] ?: ""
|
val output = glancePrefs[UnittoCalculatorWidget.outputPrefKey] ?: ""
|
||||||
|
val equalClicked = glancePrefs[UnittoCalculatorWidget.equalClickedPrefKey] ?: false
|
||||||
val formatterSymbols = AllFormatterSymbols.getById(appPrefs.separator)
|
val formatterSymbols = AllFormatterSymbols.getById(appPrefs.separator)
|
||||||
|
|
||||||
fun runCalculateAction(input: String): Action = updateInputAction(
|
fun runCalculateAction(
|
||||||
|
input: String,
|
||||||
|
equalClicked: Boolean = false
|
||||||
|
): Action = updateInputAction(
|
||||||
input = input,
|
input = input,
|
||||||
|
equalClicked = equalClicked,
|
||||||
precision = appPrefs.precision,
|
precision = appPrefs.precision,
|
||||||
outputFormat = appPrefs.outputFormat
|
outputFormat = appPrefs.outputFormat
|
||||||
)
|
)
|
||||||
@ -168,10 +176,14 @@ private fun ReadyUI(
|
|||||||
modifier = GlanceModifier
|
modifier = GlanceModifier
|
||||||
.padding(8.dp),
|
.padding(8.dp),
|
||||||
addTokenAction = {
|
addTokenAction = {
|
||||||
runCalculateAction(input.addToken(it))
|
runCalculateAction(
|
||||||
|
// Clear input if equal is clicked and new token is a Digit
|
||||||
|
(if (equalClicked and Token.Digit.allWithDot.contains(it)) "" else input)
|
||||||
|
.addToken(it)
|
||||||
|
)
|
||||||
},
|
},
|
||||||
replaceInputAction = {
|
clearInputAction = {
|
||||||
runCalculateAction(it)
|
runCalculateAction("")
|
||||||
},
|
},
|
||||||
addBracketAction = {
|
addBracketAction = {
|
||||||
runCalculateAction(input.addBracket())
|
runCalculateAction(input.addBracket())
|
||||||
@ -182,7 +194,7 @@ private fun ReadyUI(
|
|||||||
equalAction = equal@{
|
equalAction = equal@{
|
||||||
if (output.isEmpty()) return@equal actionRunCallback<UpdateInputAction>()
|
if (output.isEmpty()) return@equal actionRunCallback<UpdateInputAction>()
|
||||||
|
|
||||||
runCalculateAction(output)
|
runCalculateAction(output, true)
|
||||||
},
|
},
|
||||||
useDot = formatterSymbols.fractional == Token.Digit.dot,
|
useDot = formatterSymbols.fractional == Token.Digit.dot,
|
||||||
middleZero = appPrefs.middleZero
|
middleZero = appPrefs.middleZero
|
||||||
@ -240,7 +252,7 @@ private fun TextField(
|
|||||||
private fun GlanceKeyboard(
|
private fun GlanceKeyboard(
|
||||||
modifier: GlanceModifier,
|
modifier: GlanceModifier,
|
||||||
addTokenAction: (String) -> Action,
|
addTokenAction: (String) -> Action,
|
||||||
replaceInputAction: (String) -> Action,
|
clearInputAction: () -> Action,
|
||||||
addBracketAction: () -> Action,
|
addBracketAction: () -> Action,
|
||||||
deleteTokenAction: () -> Action,
|
deleteTokenAction: () -> Action,
|
||||||
equalAction: () -> Action,
|
equalAction: () -> Action,
|
||||||
@ -258,7 +270,7 @@ private fun GlanceKeyboard(
|
|||||||
glanceModifier = buttonModifier,
|
glanceModifier = buttonModifier,
|
||||||
containerColor = GlanceTheme.colors.tertiaryContainer,
|
containerColor = GlanceTheme.colors.tertiaryContainer,
|
||||||
iconRes = R.drawable.clear,
|
iconRes = R.drawable.clear,
|
||||||
onClick = replaceInputAction("")
|
onClick = clearInputAction()
|
||||||
)
|
)
|
||||||
IconButton(
|
IconButton(
|
||||||
glanceModifier = buttonModifier,
|
glanceModifier = buttonModifier,
|
||||||
|
@ -45,6 +45,7 @@ internal class UpdateInputAction : ActionCallback {
|
|||||||
) = withContext(Dispatchers.Default) {
|
) = withContext(Dispatchers.Default) {
|
||||||
// Get new input
|
// Get new input
|
||||||
val input = parameters[UnittoCalculatorWidget.inputKey] ?: return@withContext
|
val input = parameters[UnittoCalculatorWidget.inputKey] ?: return@withContext
|
||||||
|
val equalClicked = parameters[UnittoCalculatorWidget.equalClickedKey] ?: return@withContext
|
||||||
val precision = parameters[UnittoCalculatorWidget.precisionKey] ?: return@withContext
|
val precision = parameters[UnittoCalculatorWidget.precisionKey] ?: return@withContext
|
||||||
val outputFormat = parameters[UnittoCalculatorWidget.outputFormatKey] ?: return@withContext
|
val outputFormat = parameters[UnittoCalculatorWidget.outputFormatKey] ?: return@withContext
|
||||||
|
|
||||||
@ -62,6 +63,7 @@ internal class UpdateInputAction : ActionCallback {
|
|||||||
preferences.apply {
|
preferences.apply {
|
||||||
this[UnittoCalculatorWidget.inputPrefKey] = input
|
this[UnittoCalculatorWidget.inputPrefKey] = input
|
||||||
this[UnittoCalculatorWidget.outputPrefKey] = output
|
this[UnittoCalculatorWidget.outputPrefKey] = output
|
||||||
|
this[UnittoCalculatorWidget.equalClickedPrefKey] = equalClicked
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,11 +97,13 @@ internal class RestartWidget : ActionCallback {
|
|||||||
|
|
||||||
internal fun updateInputAction(
|
internal fun updateInputAction(
|
||||||
input: String,
|
input: String,
|
||||||
|
equalClicked: Boolean,
|
||||||
precision: Int,
|
precision: Int,
|
||||||
outputFormat: Int
|
outputFormat: Int
|
||||||
): Action = actionRunCallback<UpdateInputAction>(
|
): Action = actionRunCallback<UpdateInputAction>(
|
||||||
actionParametersOf(
|
actionParametersOf(
|
||||||
UnittoCalculatorWidget.inputKey to input,
|
UnittoCalculatorWidget.inputKey to input,
|
||||||
|
UnittoCalculatorWidget.equalClickedKey to equalClicked,
|
||||||
UnittoCalculatorWidget.precisionKey to precision,
|
UnittoCalculatorWidget.precisionKey to precision,
|
||||||
UnittoCalculatorWidget.outputFormatKey to outputFormat
|
UnittoCalculatorWidget.outputFormatKey to outputFormat
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user