Skip to content

Commit

Permalink
refactor: Move alarm settings page into its own nav destination
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 20, 2024
1 parent 87a5a52 commit 19bc6fd
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 368 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bnyro.clock.domain.usecase

import android.content.Context
import com.bnyro.clock.data.database.DatabaseHolder
import com.bnyro.clock.domain.model.Alarm
import com.bnyro.clock.util.AlarmHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class CreateUpdateDeleteAlarmUseCase(private val context: Context) {
suspend fun createAlarm(alarm: Alarm) {
alarm.enabled = true
AlarmHelper.enqueue(context, alarm)
withContext(Dispatchers.IO) {
DatabaseHolder.instance.alarmsDao().insert(alarm)
}
}

suspend fun updateAlarm(alarm: Alarm) {
AlarmHelper.enqueue(context, alarm)
withContext(Dispatchers.IO) {
DatabaseHolder.instance.alarmsDao().update(alarm)
}
}

suspend fun deleteAlarm(alarm: Alarm) {
AlarmHelper.cancel(context, alarm)
withContext(Dispatchers.IO) {
DatabaseHolder.instance.alarmsDao().delete(alarm)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.bnyro.clock.presentation.screens.timer.model.TimerModel

@Composable
fun HomeNavContainer(
onNavigate: (NavRoutes) -> Unit,
onNavigate: (route: String) -> Unit,
initialTab: HomeRoutes,
clockModel: ClockModel,
timerModel: TimerModel,
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/bnyro/clock/navigation/HomeNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.bnyro.clock.presentation.screens.timer.model.TimerModel
@Composable
fun HomeNavHost(
navController: NavHostController,
onNavigate: (NavRoutes) -> Unit,
onNavigate: (route: String) -> Unit,
startDestination: HomeRoutes,
clockModel: ClockModel,
alarmModel: AlarmModel,
Expand All @@ -26,22 +26,24 @@ fun HomeNavHost(
NavHost(navController, startDestination = startDestination.route) {
composable(HomeRoutes.Alarm.route) {
AlarmScreen(onClickSettings = {
onNavigate(NavRoutes.Settings)
onNavigate(NavRoutes.Settings.route)
}, onAlarm = {
onNavigate("${NavRoutes.AlarmPicker.route}/$it")
}, alarmModel)
}
composable(HomeRoutes.Clock.route) {
ClockScreen(onClickSettings = {
onNavigate(NavRoutes.Settings)
onNavigate(NavRoutes.Settings.route)
}, clockModel)
}
composable(HomeRoutes.Timer.route) {
TimerScreen(onClickSettings = {
onNavigate(NavRoutes.Settings)
onNavigate(NavRoutes.Settings.route)
}, timerModel)
}
composable(HomeRoutes.Stopwatch.route) {
StopwatchScreen(onClickSettings = {
onNavigate(NavRoutes.Settings)
onNavigate(NavRoutes.Settings.route)
}, stopwatchModel)
}
}
Expand Down
46 changes: 43 additions & 3 deletions app/src/main/java/com/bnyro/clock/navigation/NavHost.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.clock.navigation

import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.bnyro.clock.presentation.screens.alarm.model.AlarmModel
import com.bnyro.clock.presentation.screens.alarmpicker.AlarmPickerScreen
import com.bnyro.clock.presentation.screens.clock.model.ClockModel
import com.bnyro.clock.presentation.screens.settings.SettingsScreen
import com.bnyro.clock.presentation.screens.settings.model.SettingsModel
Expand All @@ -26,10 +30,20 @@ fun AppNavHost(
val clockModel: ClockModel = viewModel(factory = ClockModel.Factory)

NavHost(navController, startDestination = NavRoutes.Home.route, modifier = modifier) {
composable(NavRoutes.Home.route) {
composable(NavRoutes.Home.route,
enterTransition = {
slideIntoContainer(
AnimatedContentTransitionScope.SlideDirection.Down,
initialOffset = { it / 4 }
) + fadeIn()
},
exitTransition = {
slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Up,
targetOffset = { it / 4 }) + fadeOut()
}) {
HomeNavContainer(
onNavigate = {
navController.navigate(it.route)
navController.navigate(it)
},
alarmModel = alarmModel,
clockModel = clockModel,
Expand All @@ -38,10 +52,36 @@ fun AppNavHost(
initialTab = initialTab
)
}
composable(NavRoutes.Settings.route) {
composable(NavRoutes.Settings.route,
enterTransition = {
slideIntoContainer(
AnimatedContentTransitionScope.SlideDirection.Up,
initialOffset = { it / 4 }) + fadeIn()
},
exitTransition = {
slideOutOfContainer(
AnimatedContentTransitionScope.SlideDirection.Down,
targetOffset = { it / 4 }) + fadeOut()
}) {
SettingsScreen(onClickBack = {
navController.popBackStack()
}, settingsModel, timerModel)
}

composable(NavRoutes.AlarmPicker.routeWithArgs, arguments = NavRoutes.AlarmPicker.args,
enterTransition = {
slideIntoContainer(
AnimatedContentTransitionScope.SlideDirection.Up,
initialOffset = { it / 4 }) + fadeIn()
},
exitTransition = {
slideOutOfContainer(
AnimatedContentTransitionScope.SlideDirection.Down,
targetOffset = { it / 4 }) + fadeOut()
}) {
AlarmPickerScreen {
navController.popBackStack()
}
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/bnyro/clock/navigation/NavRoutes.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.bnyro.clock.navigation

import androidx.navigation.NavType
import androidx.navigation.navArgument

sealed class NavRoutes(
val route: String
) {
object Home : NavRoutes("home")
object Settings : NavRoutes("settings")
object AlarmPicker : NavRoutes("alarmPicker") {
const val alarmId = "alarmId"
val routeWithArgs = "$route/{$alarmId}"
val args = listOf(navArgument(alarmId) { NavType.LongType })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.clock.domain.model.Alarm
import com.bnyro.clock.presentation.screens.alarm.components.AlarmSettingsSheet
import com.bnyro.clock.presentation.screens.alarm.model.AlarmModel
import com.bnyro.clock.presentation.screens.alarmpicker.components.AlarmPicker
import com.bnyro.clock.presentation.screens.alarmpicker.model.AlarmPickerModel

@Composable
fun AlarmReceiverDialog(context: Context, alarm: Alarm) {
var showSheet by remember {
var showDialog by remember {
mutableStateOf(true)
}
val alarmModel: AlarmModel = viewModel()

if (showSheet) {
AlarmSettingsSheet(
onDismissRequest = { showSheet = false },
currentAlarm = alarm,
onSave = {
alarmModel.createAlarm(context, alarm)
}
)
if (showDialog) {
val alarmModel: AlarmPickerModel = viewModel()
Dialog(
onDismissRequest = { showDialog = false },
properties = DialogProperties(usePlatformDefaultWidth = false)
) {
AlarmPicker(
onCancel = { showDialog = false },
currentAlarm = alarm,
onSave = {
alarmModel.createAlarm(alarm)
alarmModel.createToast(alarm, context)
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,34 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
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
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import com.bnyro.clock.BuildConfig
import com.bnyro.clock.R
import com.bnyro.clock.domain.model.Alarm
import com.bnyro.clock.navigation.TopBarScaffold
import com.bnyro.clock.presentation.components.BlobIconBox
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.model.AlarmModel
import com.bnyro.clock.util.AlarmHelper

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AlarmScreen(
onClickSettings: () -> Unit,
onAlarm: (alarmId: Long) -> Unit,
alarmModel: AlarmModel
) {
val context = LocalContext.current
var showCreationDialog by remember {
mutableStateOf(false)
}
val alarms by alarmModel.alarms.collectAsState()
val filters by alarmModel.filters.collectAsState()

Expand All @@ -67,7 +58,7 @@ fun AlarmScreen(
if (!alarmModel.showFilter) {
FloatingActionButton(
onClick = {
showCreationDialog = true
onAlarm.invoke(0L)
}
) {
Icon(Icons.Rounded.Add, null)
Expand Down Expand Up @@ -105,29 +96,21 @@ fun AlarmScreen(
}

items(items = alarms, key = { it.id }) {
AlarmItem(it, alarmModel, context)
AlarmItem(it, onClick = { alarm ->
onAlarm.invoke(alarm.id)
}, onDeleteAlarm = { alarm ->
alarmModel.deleteAlarm(alarm)
}, onUpdateAlarm = { alarm ->
alarmModel.updateAlarm(alarm)
if (alarm.enabled) {
alarmModel.createToast(alarm, context)
}
})
}

item {
Spacer(modifier = Modifier.height(80.dp))
}
}

if (showCreationDialog) {
AlarmSettingsSheet(onDismissRequest = { showCreationDialog = false },
currentAlarm = remember { Alarm(time = 0) }) {
alarmModel.createAlarm(context, it)
showCreationDialog = false
}
}
}
alarmModel.selectedAlarm?.let {
AlarmSettingsSheet(
onDismissRequest = { alarmModel.selectedAlarm = null },
currentAlarm = it,
onSave = { newAlarm ->
alarmModel.updateAlarm(context, newAlarm)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -32,16 +29,18 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bnyro.clock.R
import com.bnyro.clock.domain.model.Alarm
import com.bnyro.clock.presentation.screens.alarm.model.AlarmModel
import com.bnyro.clock.util.AlarmHelper

@Composable
fun AlarmCard(alarm: Alarm, alarmModel: AlarmModel) {
fun AlarmCard(
alarm: Alarm,
onClick: () -> Unit,
isAlarmEnabled: Boolean,
onEnable: (Boolean) -> Unit
) {
val context = LocalContext.current
ElevatedCard(
onClick = {
alarmModel.selectedAlarm = alarm
},
onClick = onClick,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp),
shape = RoundedCornerShape(20.dp)
) {
Expand Down Expand Up @@ -127,17 +126,9 @@ fun AlarmCard(alarm: Alarm, alarmModel: AlarmModel) {
}
}

var isEnabled by remember {
mutableStateOf(alarm.enabled)
}

Switch(
checked = isEnabled,
onCheckedChange = { newValue ->
alarm.enabled = newValue
isEnabled = newValue
alarmModel.updateAlarm(context, alarm)
}
checked = isAlarmEnabled,
onCheckedChange = onEnable
)
}
}
Expand Down
Loading

0 comments on commit 19bc6fd

Please sign in to comment.