Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to create one-time alarms #236

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions app/schemas/com.bnyro.clock.db.AppDatabase/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "fbfe4cceb885036244d87a982461448d",
"entities": [
{
"tableName": "timeZones",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`name` TEXT NOT NULL, `displayName` TEXT NOT NULL, `offset` INTEGER NOT NULL, PRIMARY KEY(`name`))",
"fields": [
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "displayName",
"columnName": "displayName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "offset",
"columnName": "offset",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"name"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "alarms",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `time` INTEGER NOT NULL, `label` TEXT, `enabled` INTEGER NOT NULL, `days` TEXT NOT NULL, `vibrate` INTEGER NOT NULL, `soundName` TEXT, `soundUri` TEXT, `repeat` INTEGER NOT NULL DEFAULT 1)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "time",
"columnName": "time",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "label",
"columnName": "label",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "enabled",
"columnName": "enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "days",
"columnName": "days",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "vibrate",
"columnName": "vibrate",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "soundName",
"columnName": "soundName",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "soundUri",
"columnName": "soundUri",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "repeat",
"columnName": "repeat",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "1"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'fbfe4cceb885036244d87a982461448d')"
]
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/com/bnyro/clock/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import com.bnyro.clock.obj.TimeZone

@Database(
entities = [TimeZone::class, Alarm::class],
version = 4,
version = 5,
autoMigrations = [
AutoMigration(
from = 2,
to = 3,
spec = AppDatabase.RemoveSoundColumnAutoMigration::class
)
),
AutoMigration(from = 4, to = 5)
]
)
@TypeConverters(Converters::class)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/bnyro/clock/obj/Alarm.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bnyro.clock.obj

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

Expand All @@ -12,5 +13,6 @@ data class Alarm(
var days: List<Int> = listOf(0, 1, 2, 3, 4, 5, 6),
var vibrate: Boolean = false,
var soundName: String? = null,
var soundUri: String? = null
var soundUri: String? = null,
@ColumnInfo(defaultValue = "1") var repeat: Boolean = false
)
11 changes: 9 additions & 2 deletions app/src/main/java/com/bnyro/clock/receivers/AlarmReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ class AlarmReceiver : BroadcastReceiver() {

val currentDay = TimeHelper.getCurrentWeekDay()

if (currentDay - 1 in alarm.days) {
if (currentDay - 1 in alarm.days || !alarm.repeat) {
val playAlarm = Intent(context, AlarmService::class.java)
playAlarm.putExtra(AlarmHelper.EXTRA_ID, id)
ContextCompat.startForegroundService(context, playAlarm)
}

// re-enqueue the alarm for the next day
AlarmHelper.enqueue(context, alarm)
if (alarm.repeat) {
AlarmHelper.enqueue(context, alarm)
} else {
alarm.enabled = false
runBlocking {
DatabaseHolder.instance.alarmsDao().update(alarm)
}
}
}
}
103 changes: 61 additions & 42 deletions app/src/main/java/com/bnyro/clock/ui/components/AlarmRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ fun AlarmRow(alarm: Alarm, alarmModel: AlarmModel) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 15.dp, vertical = 10.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.fillMaxWidth().padding(horizontal = 15.dp, vertical = 10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Expand Down Expand Up @@ -160,52 +159,72 @@ fun AlarmRow(alarm: Alarm, alarmModel: AlarmModel) {
}
}
AnimatedVisibility(visible = expanded) {
Column {
val chosenDays = remember {
alarm.days.toMutableStateList()
Column(
Modifier.background(MaterialTheme.colorScheme.primaryContainer)
.padding(horizontal = 15.dp, vertical = 10.dp)
) {
var repeat by remember {
mutableStateOf(alarm.repeat)
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 15.dp),
horizontalArrangement = Arrangement.SpaceBetween
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
val daysOfWeek = remember {
AlarmHelper.getDaysOfWeekByLocale()
Text(text = stringResource(R.string.repeat))
Checkbox(checked = repeat, onCheckedChange = {
repeat = it
alarm.repeat = it
alarmModel.updateAlarm(context, alarm)
})
}
AnimatedVisibility(visible = repeat) {
val chosenDays = remember {
alarm.days.toMutableStateList()
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 15.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
val daysOfWeek = remember {
AlarmHelper.getDaysOfWeekByLocale()
}

daysOfWeek.forEach { (day, index) ->
val enabled = chosenDays.contains(index)
Box(
modifier = Modifier
.size(30.dp)
.background(
if (enabled) MaterialTheme.colorScheme.secondary else Color.Transparent,
CircleShape
)
.clip(CircleShape)
.border(
if (enabled) 0.dp else 1.dp,
MaterialTheme.colorScheme.outline,
CircleShape
daysOfWeek.forEach { (day, index) ->
val enabled = chosenDays.contains(index)
Box(
modifier = Modifier
.size(30.dp)
.background(
if (enabled) MaterialTheme.colorScheme.primary else Color.Transparent,
CircleShape
)
.clip(CircleShape)
.border(
if (enabled) 0.dp else 1.dp,
MaterialTheme.colorScheme.primary,
CircleShape
)
.clickable {
if (enabled) {
chosenDays.remove(index)
} else {
chosenDays.add(
index
)
}
alarm.days = chosenDays
alarmModel.updateAlarm(context, alarm)
},
contentAlignment = Alignment.Center
) {
Text(
text = day,
color = if (enabled) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onPrimaryContainer
)
.clickable {
if (enabled) {
chosenDays.remove(index)
} else {
chosenDays.add(
index
)
}
alarm.days = chosenDays
alarmModel.updateAlarm(context, alarm)
},
contentAlignment = Alignment.Center
) {
Text(
text = day,
color = if (enabled) MaterialTheme.colorScheme.onSecondary else MaterialTheme.colorScheme.onBackground
)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<string name="snooze">Snooze</string>
<string name="dismiss">Dismiss</string>
<string name="same_time">Same time</string>
<string name="repeat">Repeat</string>
<plurals name="hour_offset_positive">
<item quantity="one">%d hour ahead</item>
<item quantity="other">%d hours ahead</item>
Expand Down
Loading