diff --git a/app/src/main/java/com/bnyro/clock/domain/model/Alarm.kt b/app/src/main/java/com/bnyro/clock/domain/model/Alarm.kt index 22de5092..20d25667 100644 --- a/app/src/main/java/com/bnyro/clock/domain/model/Alarm.kt +++ b/app/src/main/java/com/bnyro/clock/domain/model/Alarm.kt @@ -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( diff --git a/app/src/main/java/com/bnyro/clock/presentation/features/AlarmReceiverDialog.kt b/app/src/main/java/com/bnyro/clock/presentation/features/AlarmReceiverDialog.kt index c10c1303..5293ab02 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/features/AlarmReceiverDialog.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/features/AlarmReceiverDialog.kt @@ -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 @@ -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) } @@ -22,7 +23,7 @@ fun AlarmReceiverDialog(alarm: Alarm) { onDismissRequest = { showSheet = false }, currentAlarm = alarm, onSave = { - alarmModel.createAlarm(alarm) + alarmModel.createAlarm(context, alarm) } ) } diff --git a/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmScreen.kt b/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmScreen.kt index a072ce96..72bff429 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmScreen.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/AlarmScreen.kt @@ -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 @@ -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 @@ -71,7 +70,7 @@ fun AlarmScreen( showCreationDialog = true } ) { - Icon(Icons.Default.Create, null) + Icon(Icons.Rounded.Add, null) } } }, actions = { @@ -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 } } diff --git a/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/model/AlarmModel.kt b/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/model/AlarmModel.kt index 9d687d2b..43542702 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/model/AlarmModel.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/screens/alarm/model/AlarmModel.kt @@ -44,7 +44,10 @@ 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) } @@ -52,15 +55,7 @@ class AlarmModel : ViewModel() { 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) { @@ -68,6 +63,18 @@ class AlarmModel : ViewModel() { } } + 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) { diff --git a/app/src/main/java/com/bnyro/clock/presentation/screens/clock/ClockScreen.kt b/app/src/main/java/com/bnyro/clock/presentation/screens/clock/ClockScreen.kt index 5117c285..244ab2f3 100644 --- a/app/src/main/java/com/bnyro/clock/presentation/screens/clock/ClockScreen.kt +++ b/app/src/main/java/com/bnyro/clock/presentation/screens/clock/ClockScreen.kt @@ -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 @@ -45,7 +45,7 @@ fun ClockScreen( FloatingActionButton(onClick = { showTimeZoneDialog = true }) { - Icon(Icons.Default.Create, null) + Icon(Icons.Rounded.Add, null) } }) { pv -> diff --git a/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt b/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt index 4cb1718c..e1d5d4f2 100644 --- a/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt +++ b/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt @@ -128,7 +128,7 @@ class MainActivity : ComponentActivity() { color = MaterialTheme.colorScheme.background ) { getInitialAlarm()?.let { - AlarmReceiverDialog(it) + AlarmReceiverDialog(this, it) } getInitialTimer()?.let { TimerReceiverDialog(it)