Skip to content

Commit

Permalink
feat: default to current time when creating new alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 28, 2024
1 parent 4dc1aad commit 9cfe556
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AlarmPickerModel(application: Application, savedStateHandle: SavedStateHan
init {
val alarmId = id?.toLong() ?: 0L
alarm = if (alarmId == 0L) {
Alarm(time = 0)
Alarm(time = TimeHelper.currentDayMillis)
} else {
runBlocking(Dispatchers.IO) {
alarmRepository.getAlarmById(alarmId)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/bnyro/clock/util/AlarmHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object AlarmHelper {
*/
fun getAlarmTime(alarm: Alarm): Long {
val calendar = GregorianCalendar()
calendar.time = TimeHelper.currentTime
calendar.time = TimeHelper.currentDateTime

// reset the calendar time to the start of the day
calendar.set(Calendar.HOUR_OF_DAY, 0)
Expand All @@ -86,7 +86,7 @@ object AlarmHelper {
}

fun fixDaylightTime(calendar: GregorianCalendar) {
val now = TimeHelper.currentTime
val now = TimeHelper.currentDateTime

if (calendar.timeZone.useDaylightTime()) {
if (calendar.timeZone.inDaylightTime(now) && !calendar.timeZone.inDaylightTime(calendar.time)) {
Expand All @@ -100,7 +100,7 @@ object AlarmHelper {
private fun getPostponeDays(alarm: Alarm, calendar: GregorianCalendar): Int {
if (alarm.days.isEmpty() && alarm.repeat) return 0

val hasEventPassed = calendar.time.time < TimeHelper.currentTime.time
val hasEventPassed = calendar.time.time < TimeHelper.currentDateTime.time

if (alarm.repeat) {
val today = calendar.get(Calendar.DAY_OF_WEEK) - 1
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/bnyro/clock/util/TimeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ import kotlin.math.abs
import kotlin.time.Duration

object TimeHelper {
val currentTime: Date get() = Calendar.getInstance().time
private const val MILLIS_PER_MINUTE: Int = 60_000
private const val MINUTES_PER_HOUR: Int = 60

private val calendar get() = Calendar.getInstance()
val currentDateTime: Date get() = calendar.time
val currentDayMillis: Long get() = run {
val calendar = Calendar.getInstance()
val minutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
return minutes * 60L * 1000L
}

fun getCurrentWeekDay(): Int {
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
return calendar.get(Calendar.DAY_OF_WEEK)
}

fun formatDateTime(time: ZonedDateTime): Pair<String, String> {
Expand Down

0 comments on commit 9cfe556

Please sign in to comment.