Skip to content

Commit

Permalink
fix: BigDecimal #156
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Feb 4, 2024
1 parent 6ce45bf commit 26372ae
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jing332.tts_server_android.compose.systts.list

import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material3.Text
Expand All @@ -9,7 +10,7 @@ import androidx.compose.ui.res.stringResource
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.widgets.AppDialog
import com.github.jing332.tts_server_android.compose.widgets.LabelSlider
import java.text.DecimalFormat
import com.github.jing332.tts_server_android.utils.toScale

@Composable
fun BasicAudioParamsDialog(
Expand All @@ -19,32 +20,48 @@ fun BasicAudioParamsDialog(
resetValue: Float = 0f,
onReset: () -> Unit,

defaultSpeed: Float = 0f,
speedRange: ClosedFloatingPointRange<Float> = 0f..3f,
speed: Float,
onSpeedChange: (Float) -> Unit,

defaultVolume : Float = 0f,
volumeRange: ClosedFloatingPointRange<Float> = 0f..3f,
volume: Float,
onVolumeChange: (Float) -> Unit,

defaultPitch: Float = 0f,
pitchRange: ClosedFloatingPointRange<Float> = 0f..3f,
pitch: Float,
onPitchChange: (Float) -> Unit,
) {

fun Float.to2Dic() = DecimalFormat("#.00").format(this).toFloat()
buttons: @Composable (BoxScope.() -> Unit) = {
Row {
TextButton(
enabled = speed != resetValue || volume != resetValue || pitch != resetValue,
onClick = {
onReset()
}) {
Text(stringResource(id = R.string.reset))
}

TextButton(onClick = onDismissRequest) {
Text(stringResource(id = R.string.close))
}
}
},
) {
AppDialog(
title = title,
content = {
Column {
val str = stringResource(
id = R.string.label_speech_rate,
if (speed == 0f) stringResource(R.string.follow) else speed.toString()
if (speed == defaultSpeed) stringResource(R.string.follow) else speed.toString()
)
LabelSlider(
value = speed,
onValueChange = { onSpeedChange(it.to2Dic()) },
onValueChange = { onSpeedChange(it.toScale(2)) },
valueRange = speedRange
) {
Text(str)
Expand All @@ -53,43 +70,29 @@ fun BasicAudioParamsDialog(
val volStr =
stringResource(
id = R.string.label_speech_volume,
if (volume == 0f) stringResource(R.string.follow) else volume.toString()
if (volume == defaultVolume) stringResource(R.string.follow) else volume.toString()
)
LabelSlider(
value = volume,
onValueChange = { onVolumeChange(it.to2Dic()) },
onValueChange = { onVolumeChange(it.toScale(2)) },
valueRange = volumeRange
) { Text(volStr) }

val pitchStr =
stringResource(
id = R.string.label_speech_pitch,
if (pitch == 0f) stringResource(R.string.follow) else pitch.toString()
if (pitch == defaultPitch) stringResource(R.string.follow) else pitch.toString()
)
LabelSlider(
value = pitch,
onValueChange = { onPitchChange(it.to2Dic()) },
onValueChange = { onPitchChange(it.toScale(2)) },
valueRange = pitchRange
) {
Text(pitchStr)
}

}
},
buttons = {
Row {
TextButton(
enabled = speed != resetValue || volume != resetValue || pitch != resetValue,
onClick = {
onReset()
}) {
Text(stringResource(id = R.string.reset))
}

TextButton(onClick = onDismissRequest) {
Text(stringResource(id = R.string.close))
}
}
}, onDismissRequest = onDismissRequest
buttons = buttons, onDismissRequest = onDismissRequest
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.jing332.tts_server_android.compose.systts.list

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
Expand All @@ -13,12 +11,8 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.widgets.AppDialog
import com.github.jing332.tts_server_android.compose.widgets.LabelSlider
import com.github.jing332.tts_server_android.data.entities.systts.AudioParams
import java.text.DecimalFormat

@Composable
fun GroupAudioParamsDialog(
Expand All @@ -30,79 +24,40 @@ fun GroupAudioParamsDialog(
var volume by remember { mutableFloatStateOf(params.volume) }
var pitch by remember { mutableFloatStateOf(params.pitch) }

AppDialog(
title = { Text(stringResource(id = R.string.audio_params)) },
content = {
Column {
val str = stringResource(
id = R.string.label_speech_rate,
if (speed == AudioParams.FOLLOW_GLOBAL_VALUE) stringResource(R.string.follow) else speed.toString()
)
LabelSlider(
value = speed,
onValueChange = {
speed = DecimalFormat("#.00").format(it).toFloat()
},
valueRange = 0.0f..3.0f
) {
Text(str)
}

val volStr =
stringResource(
id = R.string.label_speech_volume,
if (volume == AudioParams.FOLLOW_GLOBAL_VALUE) stringResource(R.string.follow) else volume.toString()
)
LabelSlider(
value = volume,
onValueChange = {
volume = DecimalFormat("#.00").format(it).toFloat()

},
valueRange = 0.0f..3.0f
) { Text(volStr) }

val pitchStr =
stringResource(
id = R.string.label_speech_pitch,
if (pitch == AudioParams.FOLLOW_GLOBAL_VALUE) stringResource(R.string.follow) else pitch.toString()
)
LabelSlider(
value = pitch,
onValueChange = {
pitch = DecimalFormat("#.00").format(it).toFloat()
},
valueRange = 0.0f..3.0f
) {
Text(pitchStr)
}

}
BasicAudioParamsDialog(
onDismissRequest = onDismissRequest,
onReset = {
speed = AudioParams.FOLLOW_GLOBAL_VALUE
volume = AudioParams.FOLLOW_GLOBAL_VALUE
pitch = AudioParams.FOLLOW_GLOBAL_VALUE
},
speed = speed,
onSpeedChange = { speed = it },
volume = volume,
onVolumeChange = { volume = it },
pitch = pitch,
onPitchChange = { pitch = it },
buttons = {
Row {
TextButton(
enabled = speed != AudioParams.FOLLOW_GLOBAL_VALUE ||
volume != AudioParams.FOLLOW_GLOBAL_VALUE ||
pitch != AudioParams.FOLLOW_GLOBAL_VALUE,
enabled = speed != AudioParams.FOLLOW_GLOBAL_VALUE || volume != AudioParams.FOLLOW_GLOBAL_VALUE || pitch != AudioParams.FOLLOW_GLOBAL_VALUE,
onClick = {
speed = AudioParams.FOLLOW_GLOBAL_VALUE
volume = AudioParams.FOLLOW_GLOBAL_VALUE
pitch = AudioParams.FOLLOW_GLOBAL_VALUE
}) {
Text(stringResource(id = R.string.reset))
}

Spacer(modifier = Modifier.width(24.dp))

TextButton(
onClick = onDismissRequest
) {
Text(stringResource(id = R.string.cancel))
}
TextButton(onClick = { onConfirm(AudioParams(speed, volume, pitch)) }) {
Text(stringResource(id = R.string.confirm))
Spacer(modifier = Modifier.weight(1f))
Row {
TextButton(onClick = onDismissRequest) {
Text(stringResource(id = R.string.cancel))
}
TextButton(onClick = { onConfirm(AudioParams(speed, volume, pitch)) }) {
Text(stringResource(id = R.string.confirm))
}
}
}
}, onDismissRequest = onDismissRequest)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.github.jing332.tts_server_android.R
import com.github.jing332.tts_server_android.compose.widgets.AppDialog
import com.github.jing332.tts_server_android.compose.widgets.LabelSlider
import com.github.jing332.tts_server_android.conf.SystemTtsConfig
import java.text.DecimalFormat
import com.github.jing332.tts_server_android.utils.toScale

@Composable
fun InternalPlayerDialog(onDismissRequest: () -> Unit) {
Expand All @@ -26,7 +26,7 @@ fun InternalPlayerDialog(onDismissRequest: () -> Unit) {
LabelSlider(
value = speed,
onValueChange = {
speed = DecimalFormat("#.00").format(it).toFloat()
speed = it.toScale(2)
},
valueRange = 0.1f..3.0f
) {
Expand All @@ -38,7 +38,7 @@ fun InternalPlayerDialog(onDismissRequest: () -> Unit) {
LabelSlider(
value = volume,
onValueChange = {
volume = DecimalFormat("#.00").format(it).toFloat()
volume = it.toScale(2)
},
valueRange = 0.1f..1.0f
) {
Expand All @@ -50,7 +50,7 @@ fun InternalPlayerDialog(onDismissRequest: () -> Unit) {
LabelSlider(
value = pitch,
onValueChange = {
pitch = DecimalFormat("#.00").format(it).toFloat()
pitch = it.toScale(2)
},
valueRange = 0.1f..3.0f
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.ComposeView
import com.github.jing332.tts_server_android.compose.widgets.LabelSlider
import com.github.jing332.tts_server_android.utils.ThrottleUtil
import com.github.jing332.tts_server_android.utils.toScale
import java.math.BigDecimal
import java.math.RoundingMode

Expand Down Expand Up @@ -80,7 +81,7 @@ class JSeekBar(context: Context, val hint: CharSequence) : FrameLayout(context)
},
) {
Text(
hint.toString() + BigDecimal(value.toDouble()).setScale(n, RoundingMode.HALF_UP)
hint.toString() + value.toScale(n)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.jing332.tts_server_android.utils

import java.math.BigDecimal
import java.math.RoundingMode

object DecimalUtils {
}

fun Float.toScale(scale: Int = 2) =
BigDecimal(this.toDouble()).setScale(scale, RoundingMode.HALF_UP).toFloat()

This file was deleted.

0 comments on commit 26372ae

Please sign in to comment.