From d93d586451dd4b79b023af0af8a3a680f9e7951c Mon Sep 17 00:00:00 2001 From: Empa <42304516+ItsEmpa@users.noreply.github.com> Date: Sun, 16 Jun 2024 19:15:16 +0200 Subject: Fix: Duration Format below 1s (#2110) --- .../java/at/hannibal2/skyhanni/utils/TimeUtils.kt | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt index a2c1633d7..3af450db4 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt @@ -34,16 +34,16 @@ object TimeUtils { var currentUnits = 0 val result = buildString { for ((unit, value) in parts) { - if (value != 0) { - val formatted = unit.format(value, longName) - append(formatted) - if (unit == TimeUnit.SECOND && showMilliSeconds) { - val formattedMillis = (millis / 100).toInt() - deleteCharAt(lastIndex) - append(".${formattedMillis}s") - } - - append(" ") + if (value != 0 || (unit == TimeUnit.SECOND && showMilliSeconds)) { + val formatted = value.addSeparators() + val formattedMillis = (millis / 100).toInt() + val name = unit.getName(value, longName) + + val text = if (unit == TimeUnit.SECOND && showMilliSeconds) { + "$formatted.$formattedMillis" + } else formatted + + append("$text$name ") if (maxUnits != -1 && ++currentUnits == maxUnits) break } } @@ -160,9 +160,9 @@ enum class TimeUnit(val factor: Long, val shortName: String, val longName: Strin SECOND(FACTOR_SECONDS, "s", "Second"), ; - fun format(value: Int, longFormat: Boolean = false) = if (longFormat) { - "${value.addSeparators()} $longName" + if (value > 1) "s" else "" - } else { - "${value.addSeparators()}$shortName" - } + fun getName(value: Int, longFormat: Boolean) = if (longFormat) { + " $longName" + if (value > 1) "s" else "" + } else shortName + + fun format(value: Int, longFormat: Boolean = false) = value.addSeparators() + getName(value, longFormat) } -- cgit