Skip to content

Commit

Permalink
🔥 Remove some util enums and replaced with default DateTimeFormatter
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Dec 25, 2024
1 parent 20fe93b commit d6cc057
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,41 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import br.com.colman.petals.utils.datetime.ClockFormatEnum
import br.com.colman.petals.utils.datetime.DateTimeFormatEnum
import br.com.colman.petals.utils.datetime.TimeFormatEnum
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import java.time.format.DateTimeFormatter

/**
* @see DateTimeFormatter
*/
private val DateFormats = listOf(
"yyyy-MM-dd",
"yyyy/MM/dd",
"dd-MM-yyyy",
"dd.MM.yyyy",
"MM/dd/yyyy",
"MM-dd-yyyy"
)

/**
* @see DateTimeFormatter
*/
private val TimeFormats = listOf(
"HH:mm",
"KK:mm a",
"HH:mm:ss",
"KK:mm:ss a"
)

class SettingsRepository(
private val datastore: DataStore<Preferences>
) {

val currencyIcon = datastore.data.map { it[CurrencyIcon] ?: "$" }
val dateFormatList = DateTimeFormatEnum.entries.map { it.format }
val dateFormatList = DateFormats
val dateFormat = datastore.data.map { it[DateFormat] ?: dateFormatList.first() }
val timeFormatList = TimeFormatEnum.entries.map { it.format }
val timeFormatList = TimeFormats
val timeFormat = datastore.data.map { it[TimeFormat] ?: timeFormatList.first() }
val clockFormatList = ClockFormatEnum.entries.toList()
val is24HoursFormat = datastore.data.map { it[Is24HoursFormat] ?: false }
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/br/com/colman/petals/use/UseCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ import br.com.colman.petals.R.string.total_spent
import br.com.colman.petals.R.string.yes
import br.com.colman.petals.settings.SettingsRepository
import br.com.colman.petals.use.repository.Use
import br.com.colman.petals.utils.datetime.DateTimeFormatEnum
import br.com.colman.petals.utils.datetime.TimeFormatEnum
import br.com.colman.petals.utils.truncatedToMinute
import compose.icons.TablerIcons
import compose.icons.tablericons.Cash
import compose.icons.tablericons.ReportMoney
Expand All @@ -58,6 +57,8 @@ import org.koin.compose.koinInject
import java.math.BigDecimal
import java.math.RoundingMode.HALF_UP
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter.ISO_DATE
import java.time.format.DateTimeFormatter.ISO_TIME
import java.time.format.DateTimeFormatter.ofPattern

@Preview
Expand Down Expand Up @@ -152,8 +153,9 @@ private fun DeleteUseButton(
context: Context = LocalContext.current
) {
val showDialog = remember { mutableStateOf(false) }
val dateString = use.date.format(ofPattern(DateTimeFormatEnum.YYYY_MM_DD_LINE.format))
val timeString = use.date.format(ofPattern(TimeFormatEnum.HH_MM.format))
// FIXME Date and Time string should come from Settings
val dateString = use.date.format(ISO_DATE)
val timeString = use.date.truncatedToMinute().format(ISO_TIME)
Icon(TablerIcons.Trash, null, Modifier.clickable { showDialog.value = true })

if (showDialog.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import br.com.colman.petals.R
import br.com.colman.petals.use.pause.repository.Pause
import br.com.colman.petals.utils.datetime.TimeFormatEnum
import java.time.format.DateTimeFormatter
import br.com.colman.petals.utils.truncatedToMinute
import java.time.format.DateTimeFormatter.ISO_TIME

@Preview
@Composable
Expand All @@ -20,9 +19,6 @@ fun DeletePauseDialog(
onDelete: () -> Unit = { },
onDismiss: () -> Unit = { }
) {
val formatter = remember {
DateTimeFormatter.ofPattern(TimeFormatEnum.HH_MM.format)
}
AlertDialog(
onDismissRequest = onDismiss,
title = {
Expand All @@ -35,8 +31,8 @@ fun DeletePauseDialog(
Text(
stringResource(
R.string.are_you_sure_delete_pause,
pause.startTime.format(formatter),
pause.endTime.format(formatter)
pause.startTime.truncatedToMinute().format(ISO_TIME),
pause.endTime.truncatedToMinute().format(ISO_TIME)
),
)
},
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/kotlin/br/com/colman/petals/use/pause/PauseCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import br.com.colman.petals.R
import br.com.colman.petals.use.pause.repository.Pause
import br.com.colman.petals.utils.datetime.TimeFormatEnum
import java.time.format.DateTimeFormatter
import br.com.colman.petals.utils.truncatedToMinute
import java.time.format.DateTimeFormatter.ISO_TIME

@Composable
fun PauseCard(
Expand All @@ -33,7 +33,6 @@ fun PauseCard(
onPauseDisabledStatusChange: (Boolean) -> Unit,
) {
Card(modifier = modifier) {
val formatter = DateTimeFormatter.ofPattern(TimeFormatEnum.HH_MM.format)
Row(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -43,7 +42,11 @@ fun PauseCard(
) {
Column {
Text(stringResource(id = R.string.pause_time))
Text("${pause.startTime.format(formatter)} - ${pause.endTime.format(formatter)}")
Text(
"${pause.startTime.truncatedToMinute().format(ISO_TIME)} - ${pause.endTime.truncatedToMinute().format(
ISO_TIME
)}"
)
}
Row(horizontalArrangement = spacedBy(4.dp)) {
DisablePauseView(
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/kotlin/br/com/colman/petals/utils/DateTime.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package br.com.colman.petals.utils

import java.time.LocalDateTime
import java.time.LocalTime
import java.time.temporal.ChronoUnit
import java.time.temporal.ChronoUnit.MINUTES

fun LocalTime.truncatedToMinute(): LocalTime = truncatedTo(ChronoUnit.MINUTES)
fun LocalDateTime.truncatedToMinute(): LocalDateTime = truncatedTo(MINUTES)
fun LocalTime.truncatedToMinute(): LocalTime = truncatedTo(MINUTES)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package br.com.colman.petals.utils

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.temporal.ChronoUnit

class DateTimeExtensionsTest : FunSpec({

context("LocalDateTime truncation") {

test("should truncate to minute for current time") {
val dateTime = LocalDateTime.now()
val expectedDateTime = dateTime.truncatedTo(ChronoUnit.MINUTES)

dateTime.truncatedToMinute() shouldBe expectedDateTime
}

test("should truncate to minute for a specific time") {
val dateTime = LocalDateTime.of(1998, 2, 9, 20, 0, 45)
val expectedDateTime = LocalDateTime.of(1998, 2, 9, 20, 0)

dateTime.truncatedToMinute() shouldBe expectedDateTime
}

test("should handle truncation of an already truncated time") {
val dateTime = LocalDateTime.of(1998, 2, 9, 20, 0)
val expectedDateTime = LocalDateTime.of(1998, 2, 9, 20, 0)

dateTime.truncatedToMinute() shouldBe expectedDateTime
}
}

context("LocalTime truncation") {

test("should truncate to minute for current time") {
val time = LocalTime.now()
val expectedTime = time.truncatedTo(ChronoUnit.MINUTES)

time.truncatedToMinute() shouldBe expectedTime
}

test("should truncate to minute for a specific time") {
val time = LocalTime.of(20, 0, 45)
val expectedTime = LocalTime.of(20, 0)

time.truncatedToMinute() shouldBe expectedTime
}

test("should handle truncation of an already truncated time") {
val time = LocalTime.of(20, 30)
val expectedTime = LocalTime.of(20, 30)

time.truncatedToMinute() shouldBe expectedTime
}

test("should handle midnight correctly") {
val time = LocalTime.MIDNIGHT
val expectedTime = LocalTime.MIDNIGHT

time.truncatedToMinute() shouldBe expectedTime
}
}
})

0 comments on commit d6cc057

Please sign in to comment.