Skip to content

Commit

Permalink
✨ Add an extended day setting (#596)
Browse files Browse the repository at this point in the history
Co-authored-by: gBL17 <[email protected]>
  • Loading branch information
LeoColman and gBL17 authored Jun 4, 2024
1 parent 8db40be commit 40c85c9
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = datastore.data.map { it[ExtendedDayEnabled] ?: extendedDayList[1] }

fun setCurrencyIcon(value: String): Unit = runBlocking {
datastore.edit { it[CurrencyIcon] = value }
Expand All @@ -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<String?>
get() = datastore.data.map { it[Pin] }

Expand All @@ -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<String> = stringPreferencesKey("is_day_extended")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -53,6 +55,11 @@ fun SettingsView(settingsRepository: SettingsRepository) {
settingsRepository.decimalPrecisionList,
settingsRepository::setDecimalPrecision
)
ExtendDayListItem(
currentExtendDay,
settingsRepository.extendedDayList,
settingsRepository::setExtendedDay
)
ShareApp()
}
}
Original file line number Diff line number Diff line change
@@ -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<String> = 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<String> = listOf(),
setExtendedDay: (String) -> Unit = {},
onDismiss: () -> Unit = {},
) {
SelectFromListDialog(
initialValue = initialExtendDay,
possibleValues = extendDayList,
setValue = setExtendedDay,
onDismiss = onDismiss,
label = extend_day
)
}
25 changes: 24 additions & 1 deletion app/src/main/kotlin/br/com/colman/petals/use/UseBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Use>) {
val blockRepository = koinInject<BlockRepository>()
val settingsRepository = koinInject<SettingsRepository>()

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),
Expand Down Expand Up @@ -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<Use>,
): List<Use> {
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)) }
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
<item quantity="many">Últimos %s dias</item>
<item quantity="other">Últimos %s dias</item>
</plurals>
<string name="extend_day">Habilitar Dia Estendido</string>
<string name="extend_day_a_few_hours">Estender dia algumas horas após a meia-noite</string>
<string name="wait_until_3am_to_show_a_new_day">Aguarda até 03:00 da manhã para mostrar um novo dia. Útil para quem
vai dormir depois da meia-noite.
</string>
<string name="custom">Personalizado</string>
<string name="general_knowledge">Conhecimento Geral</string>
<string name="legislation_and_rights">Legislação e Direitos</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
<string name="enable_or_disable_milliseconds_on_hit_timer_page">Enable or disable milliseconds on Hit Timer page</string>
<string name="decimal_precision">Decimal Precision</string>
<string name="what_decimal_precision_should_be_used">What decimal precision should be displayed on Usage page</string>
<string name="extend_day">Toggle Extended Day</string>
<string name="extend_day_a_few_hours">Extend day a few hours past midnight</string>
<string name="wait_until_3am_to_show_a_new_day">Wait until 3:00 AM to show a new day. Useful if you typically go to sleep after midnight.</string>
<string name="custom">Custom</string>
<string name="general_knowledge">General Knowledge</string>
<string name="legislation_and_rights">Legislation and Rights</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
})

0 comments on commit 40c85c9

Please sign in to comment.