Skip to content

Commit

Permalink
fix: alarm time not respecting locale
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Nov 22, 2023
1 parent 9b15f2b commit 7a28d03
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
43 changes: 33 additions & 10 deletions app/src/main/java/com/bnyro/clock/ui/components/AlarmRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@ package com.bnyro.clock.ui.components

import android.text.format.DateUtils
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.*
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Alarm
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material.icons.filled.Label
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Checkbox
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
Expand All @@ -25,7 +49,6 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bnyro.clock.R
import com.bnyro.clock.extensions.addZero
import com.bnyro.clock.obj.Alarm
import com.bnyro.clock.ui.dialog.RingtonePickerDialog
import com.bnyro.clock.ui.dialog.TimePickerDialog
Expand Down Expand Up @@ -103,16 +126,16 @@ fun AlarmRow(alarm: Alarm, alarmModel: AlarmModel) {
) {
showPickerDialog = true
},
text = TimeHelper.millisToTime(alarm.time).let { time ->
"${time.hours.addZero()}:${time.minutes.addZero()}"
},
text = TimeHelper.millisToFormatted(alarm.time),
style = MaterialTheme.typography.headlineLarge,
fontSize = 36.sp
)
val relativeTimeString = DateUtils.getRelativeTimeSpanString(AlarmHelper.getAlarmTime(alarm))
val relativeTimeString = DateUtils.getRelativeTimeSpanString(
AlarmHelper.getAlarmTime(alarm)
)
Text(
modifier = Modifier.padding(start = 6.dp),
text = "${relativeTimeString}."
text = "$relativeTimeString."
)
}

Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/com/bnyro/clock/util/TimeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package com.bnyro.clock.util

import com.bnyro.clock.obj.TimeObject
import java.time.Instant
import java.time.LocalTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.*
import java.util.Calendar
import java.util.Date

This comment has been minimized.

Copy link
@soshial

soshial Nov 23, 2023

Please, don't use legacy java.util classes for working with time. Most of them are either deprecated or frown upon in the Java dev community. We have so many classes available from java.time! Thank you, @SuhasDissa

import java.util.TimeZone

object TimeHelper {
val currentTime: Date get() = Calendar.getInstance().time
Expand Down Expand Up @@ -45,14 +48,24 @@ object TimeHelper {
formattedTime = formattedTime.let {
it.removeRange(
Regex("\\d+:\\d+(:\\d+)").find(
it,
)!!.groups[1]!!.range,
it
)!!.groups[1]!!.range
)
}
}
return dateFormatter.format(time) to formattedTime
}

fun millisToFormatted(millis: Long): String {
val timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
val localTime = LocalTime.of(
millis.div(1000 * 60 * 60).toInt(),
millis.div(1000 * 60).mod(60),
millis.mod(1000)
)
return timeFormatter.format(localTime)
}

fun millisToTime(millis: Long): TimeObject {
val hours = millis.div(1000 * 60 * 60).toInt()
val minutes = millis.div(1000 * 60).mod(60)
Expand Down

0 comments on commit 7a28d03

Please sign in to comment.