Skip to content

Commit

Permalink
feat: show current time in alarm alert screen (closes #311)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Mar 29, 2024
1 parent 018cdc2 commit 89bcfb7
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 78 deletions.
216 changes: 138 additions & 78 deletions app/src/main/java/com/bnyro/clock/ui/screens/AlarmAlertScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bnyro.clock.ui.screens

import android.content.res.Configuration
import android.content.res.Configuration.ORIENTATION_PORTRAIT
import androidx.compose.animation.core.Ease
import androidx.compose.animation.core.EaseInOutBack
import androidx.compose.animation.core.RepeatMode
Expand All @@ -14,6 +15,7 @@ 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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
Expand All @@ -31,10 +33,12 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -44,6 +48,9 @@ import com.bnyro.clock.R
import com.bnyro.clock.ui.model.SettingsModel
import com.bnyro.clock.ui.theme.ClockYouTheme
import com.bnyro.clock.util.ThemeUtil
import com.bnyro.clock.util.TimeHelper
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

@Composable
fun AlarmAlertScreen(
Expand All @@ -60,99 +67,152 @@ fun AlarmAlertScreen(
true
)
) {
val orientation = LocalConfiguration.current.orientation
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.SpaceEvenly,
horizontalAlignment = Alignment.CenterHorizontally
) {
val infiniteTransition = rememberInfiniteTransition(label = "")
val rotation by infiniteTransition.animateFloat(
initialValue = -10F,
targetValue = 10F,
animationSpec = infiniteRepeatable(
animation = tween(400, easing = EaseInOutBack),
repeatMode = RepeatMode.Reverse
),
label = ""
)
val offset by infiniteTransition.animateFloat(
initialValue = 10F,
targetValue = -10F,
animationSpec = infiniteRepeatable(
animation = tween(200, easing = Ease),
repeatMode = RepeatMode.Reverse
),
label = ""
)
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.offset(y = offset.dp)
.rotate(rotation)
if (orientation == ORIENTATION_PORTRAIT) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.SpaceEvenly,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
modifier = Modifier.size(250.dp),
painter = painterResource(id = R.drawable.ic_alarm),
contentDescription = null,
colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.primary)
)
}
label?.let {
Text(text = it, style = MaterialTheme.typography.headlineMedium)
AlarmAnimation()
AlarmControls(label, snoozeEnabled, onSnooze, onDismiss)
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically
) {
if (snoozeEnabled) {
OutlinedButton(
onClick = {
onSnooze.invoke()
}
) {
Row(Modifier.padding(8.dp)) {
Icon(
modifier = Modifier.align(Alignment.CenterVertically),
imageVector = Icons.Rounded.Snooze,
contentDescription = null
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = stringResource(R.string.snooze),
style = MaterialTheme.typography.titleLarge
)
}
}
} else {
Row {
Column(
modifier = Modifier
.fillMaxHeight()
.weight(2f),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
AlarmAnimation()
}
Button(
onClick = {
onDismiss.invoke()
}

Column(
modifier = Modifier
.fillMaxHeight()
.weight(3f),
verticalArrangement = Arrangement.SpaceEvenly,
horizontalAlignment = Alignment.CenterHorizontally
) {
Row(Modifier.padding(8.dp)) {
Icon(
modifier = Modifier.align(alignment = Alignment.CenterVertically),
imageVector = Icons.Rounded.AlarmOff,
contentDescription = null
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = stringResource(R.string.dismiss),
style = MaterialTheme.typography.titleLarge
)
}
AlarmControls(label, snoozeEnabled, onSnooze, onDismiss)
}
}
}
}
}
}

@Composable
private fun AlarmControls(
label: String?,
snoozeEnabled: Boolean,
onSnooze: () -> Unit,
onDismiss: () -> Unit
) {
val time by produceState(
initialValue = TimeHelper.formatTime(TimeHelper.getTimeByZone()),
producer = {
while (isActive) {
value = TimeHelper.formatTime(TimeHelper.getTimeByZone())
delay(1000)
}
}
)
Text(
text = time,
style = MaterialTheme.typography.displayMedium
)
label?.let {
Text(text = it, style = MaterialTheme.typography.headlineMedium)
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically
) {
if (snoozeEnabled) {
OutlinedButton(
onClick = {
onSnooze.invoke()
}
) {
Row(Modifier.padding(8.dp)) {
Icon(
modifier = Modifier.align(Alignment.CenterVertically),
imageVector = Icons.Rounded.Snooze,
contentDescription = null
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = stringResource(R.string.snooze),
style = MaterialTheme.typography.titleLarge
)
}
}
}
Button(
onClick = {
onDismiss.invoke()
}
) {
Row(Modifier.padding(8.dp)) {
Icon(
modifier = Modifier.align(alignment = Alignment.CenterVertically),
imageVector = Icons.Rounded.AlarmOff,
contentDescription = null
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = stringResource(R.string.dismiss),
style = MaterialTheme.typography.titleLarge
)
}
}
}
}

@Composable
private fun AlarmAnimation() {
val infiniteTransition = rememberInfiniteTransition(label = "")
val rotation by infiniteTransition.animateFloat(
initialValue = -10F,
targetValue = 10F,
animationSpec = infiniteRepeatable(
animation = tween(400, easing = EaseInOutBack),
repeatMode = RepeatMode.Reverse
),
label = ""
)
val offset by infiniteTransition.animateFloat(
initialValue = 10F,
targetValue = -10F,
animationSpec = infiniteRepeatable(
animation = tween(200, easing = Ease),
repeatMode = RepeatMode.Reverse
),
label = ""
)
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.offset(y = offset.dp)
.rotate(rotation)
) {
Image(
modifier = Modifier.size(250.dp),
painter = painterResource(id = R.drawable.ic_alarm),
contentDescription = null,
colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.primary)
)
}
}

@Preview(
showBackground = true,
uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL,
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/bnyro/clock/util/TimeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ object TimeHelper {
return dateFormatter.format(time) to timeFormatter.format(time)
}

fun formatTime(time: ZonedDateTime): String {
val timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
return timeFormatter.format(time)
}

fun millisToFormatted(millis: Long): String {
val timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
val localTime = LocalTime.of(
Expand Down

0 comments on commit 89bcfb7

Please sign in to comment.