Skip to content

Commit

Permalink
fix: use new alarm picker when creating alarms (fix #325)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 17, 2024
1 parent 6bf479b commit f432fb5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/com/bnyro/clock/domain/model/Alarm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import androidx.room.PrimaryKey
import com.bnyro.clock.util.TimeHelper

/**
* @param days The days of the week to ring the alarm. Sunday-0, Monday-1 ,... ,Saturday-6
* @param snoozeMinutes How long the snooze should last in minutes (default 10).
* @property time The time of the day in milliseconds.
* @property days The days of the week to ring the alarm. Sunday-0, Monday-1 ,... ,Saturday-6
* @property snoozeMinutes How long the snooze should last in minutes (default 10).
*/
@Entity(tableName = "alarms")
data class Alarm(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bnyro.clock.presentation.features

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -11,7 +12,7 @@ import com.bnyro.clock.presentation.screens.alarm.components.AlarmSettingsSheet
import com.bnyro.clock.presentation.screens.alarm.model.AlarmModel

@Composable
fun AlarmReceiverDialog(alarm: Alarm) {
fun AlarmReceiverDialog(context: Context, alarm: Alarm) {
var showSheet by remember {
mutableStateOf(true)
}
Expand All @@ -22,7 +23,7 @@ fun AlarmReceiverDialog(alarm: Alarm) {
onDismissRequest = { showSheet = false },
currentAlarm = alarm,
onSave = {
alarmModel.createAlarm(alarm)
alarmModel.createAlarm(context, alarm)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.FilterAlt
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
Expand All @@ -36,7 +36,6 @@ import com.bnyro.clock.presentation.components.ClickableIcon
import com.bnyro.clock.presentation.screens.alarm.components.AlarmFilterSection
import com.bnyro.clock.presentation.screens.alarm.components.AlarmItem
import com.bnyro.clock.presentation.screens.alarm.components.AlarmSettingsSheet
import com.bnyro.clock.presentation.screens.alarm.components.TimePickerDialog
import com.bnyro.clock.presentation.screens.alarm.model.AlarmModel
import com.bnyro.clock.util.AlarmHelper

Expand Down Expand Up @@ -71,7 +70,7 @@ fun AlarmScreen(
showCreationDialog = true
}
) {
Icon(Icons.Default.Create, null)
Icon(Icons.Rounded.Add, null)
}
}
}, actions = {
Expand Down Expand Up @@ -115,12 +114,9 @@ fun AlarmScreen(
}

if (showCreationDialog) {
TimePickerDialog(
label = stringResource(R.string.new_alarm),
onDismissRequest = { showCreationDialog = false }
) {
val alarm = Alarm(time = it.toLong())
alarmModel.createAlarm(alarm)
AlarmSettingsSheet(onDismissRequest = { showCreationDialog = false },
currentAlarm = remember { Alarm(time = 0) }) {
alarmModel.createAlarm(context, it)
showCreationDialog = false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,37 @@ class AlarmModel : ViewModel() {
initialValue = listOf()
)

fun createAlarm(alarm: Alarm) {
fun createAlarm(context: Context, alarm: Alarm) {
alarm.enabled = true
AlarmHelper.enqueue(context, alarm)
createToast(alarm, context)
viewModelScope.launch(Dispatchers.IO) {
DatabaseHolder.instance.alarmsDao().insert(alarm)
}
}

fun updateAlarm(context: Context, alarm: Alarm) {
if (alarm.enabled) {
val millisRemainingForAlarm =
(AlarmHelper.getAlarmTime(alarm) - System.currentTimeMillis())
val formattedDuration =
TimeHelper.durationToFormatted(context, millisRemainingForAlarm.milliseconds)
Toast.makeText(
context,
context.resources.getString(R.string.alarm_will_play, formattedDuration),
Toast.LENGTH_SHORT
).show()
createToast(alarm, context)
}
AlarmHelper.enqueue(context, alarm)
viewModelScope.launch(Dispatchers.IO) {
DatabaseHolder.instance.alarmsDao().update(alarm)
}
}

private fun createToast(alarm: Alarm, context: Context) {
val millisRemainingForAlarm =
(AlarmHelper.getAlarmTime(alarm) - System.currentTimeMillis())
val formattedDuration =
TimeHelper.durationToFormatted(context, millisRemainingForAlarm.milliseconds)
Toast.makeText(
context,
context.resources.getString(R.string.alarm_will_play, formattedDuration),
Toast.LENGTH_SHORT
).show()
}

fun deleteAlarm(context: Context, alarm: Alarm) {
AlarmHelper.cancel(context, alarm)
viewModelScope.launch(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.Sort
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.FloatingActionButton
Expand Down Expand Up @@ -45,7 +45,7 @@ fun ClockScreen(
FloatingActionButton(onClick = {
showTimeZoneDialog = true
}) {
Icon(Icons.Default.Create, null)
Icon(Icons.Rounded.Add, null)
}
}) { pv ->

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/clock/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MainActivity : ComponentActivity() {
color = MaterialTheme.colorScheme.background
) {
getInitialAlarm()?.let {
AlarmReceiverDialog(it)
AlarmReceiverDialog(this, it)
}
getInitialTimer()?.let {
TimerReceiverDialog(it)
Expand Down

0 comments on commit f432fb5

Please sign in to comment.