From fbae61eb16f242541c4a192411bc5b00ea073be6 Mon Sep 17 00:00:00 2001 From: Fabian Norlin Date: Thu, 18 Sep 2025 14:48:57 +0200 Subject: [PATCH] Move unit formatting to :shared: add expect/actual formatRelativeOrTime, port formatCoords/formatDistance to commonMain (JVM-free), remove app UnitFormat + TimeFormat files, update SettingsContent to use shared formatter --- .../main/java/app/klottr/util/UnitFormat.kt | 68 ------------------- .../app/klottr/util/UnitFormat.android.kt | 30 ++++++++ .../kotlin/app/klottr/ui/SettingsContent.kt | 6 +- .../kotlin/app/klottr/util/UnitFormat.kt | 51 ++++++++++++++ 4 files changed, 83 insertions(+), 72 deletions(-) delete mode 100644 app/src/main/java/app/klottr/util/UnitFormat.kt create mode 100644 shared/src/androidMain/kotlin/app/klottr/util/UnitFormat.android.kt create mode 100644 shared/src/commonMain/kotlin/app/klottr/util/UnitFormat.kt diff --git a/app/src/main/java/app/klottr/util/UnitFormat.kt b/app/src/main/java/app/klottr/util/UnitFormat.kt deleted file mode 100644 index 5f9feb0..0000000 --- a/app/src/main/java/app/klottr/util/UnitFormat.kt +++ /dev/null @@ -1,68 +0,0 @@ -package app.klottr.util - -import java.util.* -import java.util.Locale -import kotlin.math.abs - -fun formatRelativeOrTime(ms: Long): String { - val now = System.currentTimeMillis() - - val calNow = Calendar.getInstance() - val calTime = Calendar.getInstance().apply { timeInMillis = ms } - - val sameYear = calNow.get(Calendar.YEAR) == calTime.get(Calendar.YEAR) - val sameDay = sameYear && - calNow.get(Calendar.DAY_OF_YEAR) == calTime.get(Calendar.DAY_OF_YEAR) - - val yesterday = sameYear && - calNow.get(Calendar.DAY_OF_YEAR) - calTime.get(Calendar.DAY_OF_YEAR) == 1 - - return when { - sameDay -> { - "idag " + android.text.format.DateFormat.format("HH:mm", ms).toString() - } - yesterday -> { - "igår " + android.text.format.DateFormat.format("HH:mm", ms).toString() - - } - else -> { - val pattern = if (sameYear) "d MMM HH:mm" else "yyyy-MM-dd HH:mm" - android.text.format.DateFormat.format(pattern, ms).toString() - } - } -} - -fun formatCoords(lat: Double, lon: Double): String { - val latDir = if (lat >= 0) "N" else "S" - val lonDir = if (lon >= 0) "E" else "W" - - // format med punkt först (Locale.US → garanterar punkt) - val latStr = String.format(Locale.US, "%.5f", abs(lat)) - val lonStr = String.format(Locale.US, "%.5f", abs(lon)) - - // byt punkt till komma för svenskt skrivsätt - val latComma = latStr.replace('.', ',') - val lonComma = lonStr.replace('.', ',') - - return "$latComma° $latDir, $lonComma° $lonDir" -} - - -fun formatDistance(meters: Int): String = when { - meters < 1000 -> "$meters\u00A0m" - meters < 10_000 -> { - val km = meters / 1000.0 - if (km % 1.0 == 0.0) // heltal? - String.format(Locale("sv"), "%.0f\u00A0km", km) - else - String.format(Locale("sv"), "%.1f\u00A0km", km) - } - else -> { - val mil = meters / 10_000.0 - if (mil % 1.0 == 0.0) - String.format(Locale("sv"), "%.0f\u00A0mil", mil) - else - String.format(Locale("sv"), "%.1f\u00A0mil", mil) - } -} - diff --git a/shared/src/androidMain/kotlin/app/klottr/util/UnitFormat.android.kt b/shared/src/androidMain/kotlin/app/klottr/util/UnitFormat.android.kt new file mode 100644 index 0000000..a8e5a7f --- /dev/null +++ b/shared/src/androidMain/kotlin/app/klottr/util/UnitFormat.android.kt @@ -0,0 +1,30 @@ +package app.klottr.util + +import android.text.format.DateFormat +import java.util.Calendar + +// Android-actual: samma beteende som tidigare. +actual fun formatRelativeOrTime(ms: Long): String { + val calNow = Calendar.getInstance() + val calTime = Calendar.getInstance().apply { timeInMillis = ms } + + val sameYear = calNow.get(Calendar.YEAR) == calTime.get(Calendar.YEAR) + val sameDay = sameYear && + calNow.get(Calendar.DAY_OF_YEAR) == calTime.get(Calendar.DAY_OF_YEAR) + + val yesterday = sameYear && + calNow.get(Calendar.DAY_OF_YEAR) - calTime.get(Calendar.DAY_OF_YEAR) == 1 + + return when { + sameDay -> { + "idag " + DateFormat.format("HH:mm", ms).toString() + } + yesterday -> { + "igår " + DateFormat.format("HH:mm", ms).toString() + } + else -> { + val pattern = if (sameYear) "d MMM HH:mm" else "yyyy-MM-dd HH:mm" + DateFormat.format(pattern, ms).toString() + } + } +} diff --git a/shared/src/commonMain/kotlin/app/klottr/ui/SettingsContent.kt b/shared/src/commonMain/kotlin/app/klottr/ui/SettingsContent.kt index 080fff5..cbea128 100644 --- a/shared/src/commonMain/kotlin/app/klottr/ui/SettingsContent.kt +++ b/shared/src/commonMain/kotlin/app/klottr/ui/SettingsContent.kt @@ -31,6 +31,8 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.zIndex +import app.klottr.util.formatDistance // Använd delade funktionen + /** * Material-fritt innehåll för Inställningar (CMP-redo) med egengjord switch och dropdown. @@ -389,7 +391,3 @@ private fun ValuePicker( } } } - -/** Enkel formattering för meter/”km”. */ -private fun formatDistance(m: Int): String = - if (m >= 1000 && m % 1000 == 0) "${m / 1000} km" else "$m meter" diff --git a/shared/src/commonMain/kotlin/app/klottr/util/UnitFormat.kt b/shared/src/commonMain/kotlin/app/klottr/util/UnitFormat.kt new file mode 100644 index 0000000..7d18f85 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/util/UnitFormat.kt @@ -0,0 +1,51 @@ +package app.klottr.util + +import kotlin.math.abs +import kotlin.math.pow +import kotlin.math.round +import kotlin.math.roundToLong + +// Plattformsgemensamt API (actual i plattformsmappar, Android redan på plats i UnitFormat.android.kt) +expect fun formatRelativeOrTime(ms: Long): String + +// Hjälp: format med fast antal decimaler, utan JVM/Locale/printf. +private fun formatFixed(value: Double, decimals: Int): String { + val factor = 10.0.pow(decimals).roundToLong() // 10^decimals som heltal + val scaled = round(value * factor).toLong() // Avrunda till heltal i "skalad" domän + val intPart = scaled / factor // Heltalsdelen + if (decimals == 0) return intPart.toString() // Ingen decimaldel + val fracPart = (scaled % factor).toString().padStart(decimals, '0') // Fyll ut med nollor + return "$intPart.$fracPart" +} + +// Koordinater: 5 decimaler, kommatecken som svensk decimalavskiljare, NBSP för mellanrum. +fun formatCoords(lat: Double, lon: Double): String { + val latDir = if (lat >= 0) "N" else "S" + val lonDir = if (lon >= 0) "E" else "W" + + val latStr = formatFixed(abs(lat), 5).replace('.', ',') // 5 decimaler + val lonStr = formatFixed(abs(lon), 5).replace('.', ',') // 5 decimaler + + return "$latStr° $latDir, $lonStr° $lonDir" +} + +// Avstånd: m < 1000 → "m"; < 10 km → "km" med ev. 1 decimal; annars "mil" med ev. 1 decimal. +// NBSP (\u00A0) mellan tal och enhet, svensk decimal (komma). +fun formatDistance(meters: Int): String { + val nbsp = "\u00A0" + return when { + meters < 1000 -> "$meters${nbsp}m" + + meters < 10_000 -> { + val km = meters / 1000.0 + val s = if (km % 1.0 == 0.0) formatFixed(km, 0) else formatFixed(km, 1) + s.replace('.', ',') + "${nbsp}km" + } + + else -> { + val mil = meters / 10_000.0 + val s = if (mil % 1.0 == 0.0) formatFixed(mil, 0) else formatFixed(mil, 1) + s.replace('.', ',') + "${nbsp}mil" + } + } +}