diff --git a/app/src/main/kotlin/br/com/colman/petals/settings/SettingsRepository.kt b/app/src/main/kotlin/br/com/colman/petals/settings/SettingsRepository.kt index ac805ab4..fa7f5c58 100644 --- a/app/src/main/kotlin/br/com/colman/petals/settings/SettingsRepository.kt +++ b/app/src/main/kotlin/br/com/colman/petals/settings/SettingsRepository.kt @@ -33,6 +33,8 @@ class SettingsRepository( } val decimalPrecisionList = listOf(0, 1, 2, 3) val decimalPrecision = datastore.data.map { it[DecimalPrecision] ?: decimalPrecisionList[2] } + val extendedDayList = listOf("enabled", "disabled") + val extendedDay: Flow = datastore.data.map { it[ExtendedDayEnabled] ?: extendedDayList[1] } fun setCurrencyIcon(value: String): Unit = runBlocking { datastore.edit { it[CurrencyIcon] = value } @@ -58,6 +60,10 @@ class SettingsRepository( datastore.edit { it[DecimalPrecision] = value } } + fun setExtendedDay(value: String): Unit = runBlocking { + datastore.edit { it[ExtendedDayEnabled] = value } + } + val pin: Flow get() = datastore.data.map { it[Pin] } @@ -75,5 +81,6 @@ class SettingsRepository( val MillisecondsEnabled = stringPreferencesKey("milliseconds_enabled") val HitTimerMillisecondsEnabled = stringPreferencesKey("hit_timer_milliseconds_enabled") val DecimalPrecision = intPreferencesKey("decimal_precision") + val ExtendedDayEnabled: Preferences.Key = stringPreferencesKey("is_day_extended") } } diff --git a/app/src/main/kotlin/br/com/colman/petals/settings/SettingsView.kt b/app/src/main/kotlin/br/com/colman/petals/settings/SettingsView.kt index efd2654a..4ee8ad2f 100644 --- a/app/src/main/kotlin/br/com/colman/petals/settings/SettingsView.kt +++ b/app/src/main/kotlin/br/com/colman/petals/settings/SettingsView.kt @@ -9,6 +9,7 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import br.com.colman.petals.settings.view.listitem.CurrencyListItem import br.com.colman.petals.settings.view.listitem.DateListItem +import br.com.colman.petals.settings.view.listitem.ExtendDayListItem import br.com.colman.petals.settings.view.listitem.HitTimerMillisecondsEnabledListItem import br.com.colman.petals.settings.view.listitem.MillisecondsBarEnabledListItem import br.com.colman.petals.settings.view.listitem.PinListItem @@ -31,6 +32,7 @@ fun SettingsView(settingsRepository: SettingsRepository) { val currentDecimalPrecision by settingsRepository.decimalPrecision.collectAsState( settingsRepository.decimalPrecisionList[2] ) + val currentExtendDay: String by settingsRepository.extendedDay.collectAsState(settingsRepository.extendedDayList[1]) Column(Modifier.verticalScroll(rememberScrollState())) { CurrencyListItem(currentCurrency, settingsRepository::setCurrencyIcon) @@ -53,6 +55,11 @@ fun SettingsView(settingsRepository: SettingsRepository) { settingsRepository.decimalPrecisionList, settingsRepository::setDecimalPrecision ) + ExtendDayListItem( + currentExtendDay, + settingsRepository.extendedDayList, + settingsRepository::setExtendedDay + ) ShareApp() } } diff --git a/app/src/main/kotlin/br/com/colman/petals/settings/view/listitem/ExtendDayListItem.kt b/app/src/main/kotlin/br/com/colman/petals/settings/view/listitem/ExtendDayListItem.kt new file mode 100644 index 00000000..21740cb6 --- /dev/null +++ b/app/src/main/kotlin/br/com/colman/petals/settings/view/listitem/ExtendDayListItem.kt @@ -0,0 +1,49 @@ +package br.com.colman.petals.settings.view.listitem + +import androidx.compose.runtime.Composable +import androidx.compose.ui.tooling.preview.Preview +import br.com.colman.petals.R.string.extend_day +import br.com.colman.petals.R.string.extend_day_a_few_hours +import br.com.colman.petals.R.string.wait_until_3am_to_show_a_new_day +import br.com.colman.petals.settings.view.dialog.SelectFromListDialog +import compose.icons.TablerIcons +import compose.icons.tablericons.CalendarTime + +@Preview +@Composable +fun ExtendDayListItem( + extendedDay: String = "", + extendedDayOptions: List = listOf(), + setExtendDayOption: (String) -> Unit = {} +) { + DialogListItem( + icon = TablerIcons.CalendarTime, + textId = extend_day_a_few_hours, + descriptionId = wait_until_3am_to_show_a_new_day, + dialog = { hideDialog: () -> Unit -> + ExtendDayDialog( + extendedDay, + extendedDayOptions, + setExtendDayOption, + hideDialog + ) + } + ) +} + +@Preview +@Composable +private fun ExtendDayDialog( + initialExtendDay: String = "", + extendDayList: List = listOf(), + setExtendedDay: (String) -> Unit = {}, + onDismiss: () -> Unit = {}, +) { + SelectFromListDialog( + initialValue = initialExtendDay, + possibleValues = extendDayList, + setValue = setExtendedDay, + onDismiss = onDismiss, + label = extend_day + ) +} diff --git a/app/src/main/kotlin/br/com/colman/petals/use/UseBlock.kt b/app/src/main/kotlin/br/com/colman/petals/use/UseBlock.kt index 5e94617c..6ebfd7b0 100644 --- a/app/src/main/kotlin/br/com/colman/petals/use/UseBlock.kt +++ b/app/src/main/kotlin/br/com/colman/petals/use/UseBlock.kt @@ -66,20 +66,28 @@ import compose.icons.tablericons.ZoomMoney import org.koin.compose.koinInject import java.math.RoundingMode.HALF_UP import java.time.DayOfWeek.MONDAY +import java.time.LocalDate import java.time.LocalDate.now +import java.time.LocalTime @Composable fun StatsBlocks(uses: List) { val blockRepository = koinInject() + val settingsRepository = koinInject() val isTodayCensored by blockRepository.isTodayCensored.collectAsState(true) val isThisWeekCensored by blockRepository.isThisWeekCensored.collectAsState(true) val isThisMonthCensored by blockRepository.isThisMonthCensored.collectAsState(true) val isThisYearCensored by blockRepository.isThisYearCensored.collectAsState(true) val isAllTimeCensored by blockRepository.isAllTimeCensored.collectAsState(true) + val isDayExtended: String by settingsRepository.extendedDay.collectAsState("disabled") Row(Modifier.horizontalScroll(rememberScrollState()).width(Max)) { - UseBlock(Modifier.weight(1f), Today, uses.filter { it.date.toLocalDate() == now() }, isTodayCensored) + if (isDayExtended == "enabled") { + UseBlock(Modifier.weight(1f), Today, adjustTodayFilter(uses), isTodayCensored) + } else { + UseBlock(Modifier.weight(1f), Today, uses.filter { it.date.toLocalDate() == now() }, isTodayCensored) + } UseBlock( Modifier.weight(1f), @@ -160,3 +168,18 @@ private fun CensureIcon(isCensored: Boolean) { private fun BlockText(blockText: String, isCensored: Boolean) { if (isCensored) Text(stringResource(R.string.censored)) else Text(blockText) } + +@Composable +private fun adjustTodayFilter( + uses: List, +): List { + val limitTime = LocalTime.of(3, 0) + val currentTime = LocalTime.now() + val currentDate = LocalDate.now() + + return if (currentTime <= limitTime) { + uses.filter { it.date.toLocalDate() >= currentDate.minusDays(1) } + } else { + uses.filter { it.date.isAfter(currentDate.atTime(limitTime)) } + } +} diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 165b1d58..ca836191 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -130,6 +130,11 @@ Últimos %s dias Últimos %s dias + Habilitar Dia Estendido + Estender dia algumas horas após a meia-noite + Aguarda até 03:00 da manhã para mostrar um novo dia. Útil para quem + vai dormir depois da meia-noite. + Personalizado Conhecimento Geral Legislação e Direitos diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0138f494..07fa6db4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -122,6 +122,9 @@ Enable or disable milliseconds on Hit Timer page Decimal Precision What decimal precision should be displayed on Usage page + Toggle Extended Day + Extend day a few hours past midnight + Wait until 3:00 AM to show a new day. Useful if you typically go to sleep after midnight. Custom General Knowledge Legislation and Rights diff --git a/app/src/test/kotlin/br/com/colman/petals/settings/SettingsRepositoryTest.kt b/app/src/test/kotlin/br/com/colman/petals/settings/SettingsRepositoryTest.kt index 4939c686..d2fbe66c 100644 --- a/app/src/test/kotlin/br/com/colman/petals/settings/SettingsRepositoryTest.kt +++ b/app/src/test/kotlin/br/com/colman/petals/settings/SettingsRepositoryTest.kt @@ -3,6 +3,7 @@ package br.com.colman.petals.settings import androidx.datastore.preferences.core.PreferenceDataStoreFactory import br.com.colman.petals.settings.SettingsRepository.Companion.CurrencyIcon import br.com.colman.petals.settings.SettingsRepository.Companion.DateFormat +import br.com.colman.petals.settings.SettingsRepository.Companion.ExtendedDayEnabled import br.com.colman.petals.settings.SettingsRepository.Companion.TimeFormat import io.kotest.assertions.throwables.shouldNotThrowAny import io.kotest.core.spec.style.FunSpec @@ -70,4 +71,18 @@ class SettingsRepositoryTest : FunSpec({ target.setTimeFormat("HH:mm") datastore.data.first()[TimeFormat] shouldBe "HH:mm" } + + test("Default extended day to disabled") { + target.extendedDay.first() shouldBe "disabled" + } + + test("Changes extend day to enable") { + target.setExtendedDay("enabled") + target.extendedDay.first() shouldBe "enabled" + } + + test("Persists extended day to enabled") { + target.setExtendedDay("enabled") + datastore.data.first()[ExtendedDayEnabled] shouldBe "enabled" + } })