Use more sensible values for sleep timer

This also changes all of these items to use `getQuantityString` instead of
manually appending values.
This commit is contained in:
Ensar Sarajčić 2023-09-06 10:41:25 +02:00
parent c55e784081
commit 174b2ac803
1 changed files with 21 additions and 23 deletions

View File

@ -304,32 +304,13 @@ class MainActivity : SimpleActivity() {
) )
return return
} }
val minutes = getString(R.string.minutes_raw)
val hour = resources.getQuantityString(R.plurals.hours, 1, 1)
val minute = resources.getQuantityString(R.plurals.minutes, 1, 1)
val items = arrayListOf( val items = ArrayList(listOf(10, 30, 60, 5 * 60, 10 * 60, 30 * 60).map {
RadioItem(1 * 60, "1 $minute"), RadioItem(it, secondsToString(it))
RadioItem(2 * 60, "2 $minutes"), })
RadioItem(5 * 60, "5 $minutes"),
RadioItem(10 * 60, "10 $minutes"),
RadioItem(20 * 60, "20 $minutes"),
RadioItem(30 * 60, "30 $minutes"),
RadioItem(60 * 60, hour)
)
if (items.none { it.id == config.lastSleepTimerSeconds }) { if (items.none { it.id == config.lastSleepTimerSeconds }) {
val lastSleepTimerMinutes = config.lastSleepTimerSeconds / 60 items.add(RadioItem(config.lastSleepTimerSeconds, secondsToString(config.lastSleepTimerSeconds)))
val lastSleepTimerSeconds = config.lastSleepTimerSeconds % 60
val parts = mutableListOf<String>()
if (lastSleepTimerMinutes != 0) {
parts.add(resources.getQuantityString(R.plurals.minutes, lastSleepTimerMinutes, lastSleepTimerMinutes))
}
if (lastSleepTimerSeconds != 0) {
parts.add(resources.getQuantityString(R.plurals.seconds, lastSleepTimerSeconds, lastSleepTimerSeconds))
}
val text = parts.joinToString(separator = " ")
items.add(RadioItem(config.lastSleepTimerSeconds, text))
} }
items.sortBy { it.id } items.sortBy { it.id }
@ -348,6 +329,23 @@ class MainActivity : SimpleActivity() {
} }
} }
private fun secondsToString(seconds: Int): String {
val finalHours = seconds / 3600
val finalMinutes = (seconds / 60) % 60
val finalSeconds = seconds % 60
val parts = mutableListOf<String>()
if (finalHours != 0) {
parts.add(resources.getQuantityString(R.plurals.hours, finalHours, finalHours))
}
if (finalMinutes != 0) {
parts.add(resources.getQuantityString(R.plurals.minutes, finalMinutes, finalMinutes))
}
if (finalSeconds != 0) {
parts.add(resources.getQuantityString(R.plurals.seconds, finalSeconds, finalSeconds))
}
return parts.joinToString(separator = " ")
}
private fun pickedSleepTimer(seconds: Int) { private fun pickedSleepTimer(seconds: Int) {
config.lastSleepTimerSeconds = seconds config.lastSleepTimerSeconds = seconds
config.sleepInTS = System.currentTimeMillis() + seconds * 1000 config.sleepInTS = System.currentTimeMillis() + seconds * 1000