Skip to content

Commit

Permalink
[ADDED] Text number formatter for 2 decimal formatting.
Browse files Browse the repository at this point in the history
Added tests as well.

The unit mess is getting out of hands - #60
  • Loading branch information
hossain-khan committed Jan 8, 2025
1 parent 52215b1 commit 440e424
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.drawable.Icon
import androidx.core.app.NotificationCompat
import dev.hossain.weatheralert.R
import dev.hossain.weatheralert.data.WeatherAlertCategory
import dev.hossain.weatheralert.util.formatUnit
import dev.hossain.weatheralert.util.stripMarkdownSyntax
import timber.log.Timber

Expand Down Expand Up @@ -46,10 +47,10 @@ internal fun triggerNotification(
append("About ")
when (alertCategory) {
WeatherAlertCategory.SNOW_FALL -> {
append("$currentValue cm snowfall expected.")
append("${currentValue.formatUnit(WeatherAlertCategory.SNOW_FALL.unit)} snowfall expected.")
}
WeatherAlertCategory.RAIN_FALL -> {
append("$currentValue mm rainfall expected.")
append("${currentValue.formatUnit(WeatherAlertCategory.RAIN_FALL.unit)} rainfall expected.")
}
}
}
Expand All @@ -60,13 +61,19 @@ internal fun triggerNotification(
when (alertCategory) {
WeatherAlertCategory.SNOW_FALL -> {
append(
"$cityName is forecasted to receive $currentValue cm of snowfall within the next 24 hours, surpassing your configured threshold of $thresholdValue cm.",
"$cityName is forecasted to receive ${currentValue.formatUnit(
WeatherAlertCategory.SNOW_FALL.unit,
)} of snowfall within the next 24 hours, ",
)
append("surpassing your configured threshold of ${thresholdValue.formatUnit(WeatherAlertCategory.SNOW_FALL.unit)}.")
}
WeatherAlertCategory.RAIN_FALL -> {
append(
"$cityName is forecasted to receive $currentValue mm of rainfall within the next 24 hours, surpassing your configured threshold of $thresholdValue mm.",
"$cityName is forecasted to receive ${currentValue.formatUnit(
WeatherAlertCategory.RAIN_FALL.unit,
)} of rainfall within the next 24 hours, ",
)
append("surpassing your configured threshold of ${thresholdValue.formatUnit(WeatherAlertCategory.RAIN_FALL.unit)}.")
}
}
if (reminderNotes.isNotBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ import dev.hossain.weatheralert.di.AppScope
import dev.hossain.weatheralert.network.NetworkMonitor
import dev.hossain.weatheralert.ui.addalert.AlertSettingsScreen
import dev.hossain.weatheralert.ui.theme.WeatherAlertAppTheme
import dev.hossain.weatheralert.util.formatUnit
import dev.hossain.weatheralert.util.parseMarkdown
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import timber.log.Timber
import java.util.Locale

@Parcelize
data class CurrentWeatherAlertScreen(
Expand Down Expand Up @@ -160,22 +160,13 @@ class CurrentWeatherAlertPresenter
lat = alert.city.lat,
lon = alert.city.lng,
category = alert.alert.alertCategory,
threshold =
"%.2f %s".format(
Locale.getDefault(),
alert.alert.threshold,
alert.alert.alertCategory.unit,
),
threshold = alert.alert.threshold.formatUnit(alert.alert.alertCategory.unit),
currentStatus =
"%.2f %s".format(
Locale.getDefault(),
if (alert.alert.alertCategory == WeatherAlertCategory.SNOW_FALL) {
snowStatus
} else {
rainStatus
},
alert.alert.alertCategory.unit,
),
if (alert.alert.alertCategory == WeatherAlertCategory.SNOW_FALL) {
snowStatus.formatUnit(alert.alert.alertCategory.unit)
} else {
rainStatus.formatUnit(alert.alert.alertCategory.unit)
},
isAlertActive =
when (alert.alert.alertCategory) {
WeatherAlertCategory.SNOW_FALL -> snowStatus > alert.alert.threshold
Expand Down Expand Up @@ -673,7 +664,7 @@ fun CurrentWeatherAlertsPreview() {
threshold = "10 mm",
currentStatus = "Tomorrow: 12 mm",
isAlertActive = true,
alertNote = "test note",
alertNote = "Note when alert is reached.\n* Charge batteries\n* Get car in **garage**",
),
)
CurrentWeatherAlerts(CurrentWeatherAlertScreen.State(sampleTiles) {})
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/dev/hossain/weatheralert/util/TextFormatter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.hossain.weatheralert.util

import java.util.Locale

/**
* Formats 2.440923834343 to 2.44 cm
*/
internal fun Float.formatUnit(unit: String): String = "%.2f %s".format(Locale.getDefault(), this, unit)

/**
* Formats 8.98237320 to 8.98 mm
*/
internal fun Double.formatUnit(unit: String): String = "%.2f %s".format(Locale.getDefault(), this, unit)
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.hossain.weatheralert.util

import org.junit.Assert.assertEquals
import org.junit.Test

class TextFormatterTest {
@Test
fun formatUnit_handlesFloatValue() {
val value = 2.440923834343f
val unit = "cm"
val expected = "2.44 cm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesDoubleValue() {
val value = 8.98237320
val unit = "mm"
val expected = "8.98 mm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesZeroFloatValue() {
val value = 0.0f
val unit = "cm"
val expected = "0.00 cm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesZeroDoubleValue() {
val value = 0.0
val unit = "mm"
val expected = "0.00 mm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesNegativeFloatValue() {
val value = -2.440923834343f
val unit = "cm"
val expected = "-2.44 cm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesNegativeDoubleValue() {
val value = -8.98237320
val unit = "mm"
val expected = "-8.98 mm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesLargeFloatValue() {
val value = 123456.78923f
val unit = "cm"
val expected = "123456.79 cm"
assertEquals(expected, value.formatUnit(unit))
}

@Test
fun formatUnit_handlesLargeDoubleValue() {
val value = 987654.321
val unit = "mm"
val expected = "987654.32 mm"
assertEquals(expected, value.formatUnit(unit))
}
}

0 comments on commit 440e424

Please sign in to comment.