Skip to content

Commit

Permalink
complete time format
Browse files Browse the repository at this point in the history
  • Loading branch information
brandyodhiambo committed Jun 24, 2024
1 parent ce3b5b6 commit 6a1e50a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ interface TimeFormateRepository {
suspend fun insertTimeFormate(timeFormate: TimeFormate)
fun getTimeFormate(): LiveData<TimeFormate?>
suspend fun deleteTimeFormate(timeFormate: TimeFormate)
suspend fun deleteAllTimeFormate()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ interface TimeFormateDao {

@Delete
suspend fun deleteTimeFormate(timeFormateEntity: TimeFormateEntity)

@Query("DELETE FROM time_formate_table")
suspend fun deleteAllTimeFormate()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ import com.brandyodhiambo.entity.MonthlyStatisticsEntity
import com.brandyodhiambo.entity.ReminderTimeEntity
import com.brandyodhiambo.entity.SelectedDrinkEntity
import com.brandyodhiambo.entity.SleepTimeEntity
import com.brandyodhiambo.entity.TimeFormateEntity
import com.brandyodhiambo.entity.WakeTimeEntity
import com.brandyodhiambo.entity.WeeklyStatisticsEntity

@TypeConverters(Converter::class)
@Database(
entities = [SelectedDrinkEntity::class, WakeTimeEntity::class, IdealWaterIntakeEntity::class, GoalWaterIntakeEntity::class, SleepTimeEntity::class, LevelEntity::class, ReminderTimeEntity::class, DailyStatisticsEntity::class, WeeklyStatisticsEntity::class, MonthlyStatisticsEntity::class, AchievementEntity::class],
entities = [SelectedDrinkEntity::class, WakeTimeEntity::class, IdealWaterIntakeEntity::class, GoalWaterIntakeEntity::class, SleepTimeEntity::class, LevelEntity::class, ReminderTimeEntity::class, DailyStatisticsEntity::class, WeeklyStatisticsEntity::class, MonthlyStatisticsEntity::class, AchievementEntity::class,TimeFormateEntity::class],
version = 5,
exportSchema = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ class TimeFormateRepositoryImpl(
override suspend fun deleteTimeFormate(timeFormate: TimeFormate) {
timeFormateDao.deleteTimeFormate(timeFormate.toTimeFormateEntity())
}

override suspend fun deleteAllTimeFormate() {
timeFormateDao.deleteAllTimeFormate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ import androidx.hilt.navigation.compose.hiltViewModel
import com.brandyodhiambo.common.R
import com.brandyodhiambo.common.domain.model.GoalWaterIntake
import com.brandyodhiambo.common.domain.model.IdealWaterIntake
import com.brandyodhiambo.common.domain.model.TimeFormate
import com.brandyodhiambo.common.presentation.component.WaterIntakeDialog
import com.brandyodhiambo.home.presentation.component.IdealIntakeGoalDialog
import com.brandyodhiambo.home.presentation.homeScreen.HomeViewModel
import com.brandyodhiambo.settings.presentation.component.CustomReminderDialog
import com.brandyodhiambo.settings.presentation.component.TimeFormateDialog
import com.ramcosta.composedestinations.annotation.Destination

interface SettingsNavigator {
Expand All @@ -75,9 +77,11 @@ fun SettingScreen(
val openTimeDialog = settingViewModel.openTimeDialog.value
val openWaterUnitDialog = settingViewModel.openWaterUnitDialog.value
val openWeightUnitDialog = settingViewModel.openWeightUnitDialog.value
val timeformate = settingViewModel.timeFormateFromDb.value


SettingScreenContent(
timeFormate = timeformate ?: TimeFormate(12),
openIntakeDialog = openIntakeDialog,
openTimeDialog = openTimeDialog,
openWaterUnitDialog = openWaterUnitDialog,
Expand All @@ -86,6 +90,16 @@ fun SettingScreen(
currentWaterIntakeFormText = homeViewModel.goalWaterForm.value,
currentIntake = homeViewModel.goalWaterIntakeValue.value,
currentForm = homeViewModel.goalWaterForm.value,
selectedValue = settingViewModel.selectedTimeFormate.value,
onRadioButtonClicked = {
val insertTimeFormate = TimeFormate(
formate = it.toInt()
)
settingViewModel.insertTimeFormate(insertTimeFormate)
} ,
onChangeState = {
settingViewModel.onTimeFormatSelected(it)
},
onCurrentWaterIntakeTextChange = {
homeViewModel.setGoalWaterIntakeValue(it)
},
Expand Down Expand Up @@ -139,6 +153,7 @@ fun SettingScreen(
@Composable
fun SettingScreenContent(
modifier: Modifier = Modifier,
timeFormate: TimeFormate,
openIntakeDialog: Boolean,
openTimeDialog: Boolean,
openWaterUnitDialog: Boolean,
Expand All @@ -160,6 +175,9 @@ fun SettingScreenContent(
onDismissTimeDialog:()->Unit,
onDismissWaterUnitDialog:()->Unit,
onDismissWeightUnitDialog:()->Unit,
selectedValue: String,
onChangeState: (String) -> Unit,
onRadioButtonClicked: (String) -> Unit,

) {
Scaffold(
Expand All @@ -173,6 +191,7 @@ fun SettingScreenContent(
LazyColumn {
item {
UnitsWaterIntake(
timeFormate = selectedValue,
onOpenWeightUnitDialog = onOpenWeightUnitDialog,
onOpenTimeFormatDialog = onOpenTimeFormatDialog,
onOpenWaterUnitDialog = onOpenWaterUnitDialog,
Expand Down Expand Up @@ -206,10 +225,13 @@ fun SettingScreenContent(
}
if (openTimeDialog) {
Dialog(onDismissRequest = { onDismissTimeDialog() }) {
val time = listOf("12 Hour", "24 Hour")
CustomReminderDialog(
val time = listOf("12", "24")
TimeFormateDialog(
items = time,
title = "Time Format",
selectedValue = selectedValue,
onChangeState = onChangeState,
onRadioButtonClicked = onRadioButtonClicked,
onCustomReminderDialog = onDismissTimeDialog
)
}
Expand Down Expand Up @@ -242,6 +264,7 @@ fun SettingScreenContent(

@Composable
fun UnitsWaterIntake(
timeFormate: String,
onOpenWeightUnitDialog: (Boolean) -> Unit,
onOpenTimeFormatDialog: (Boolean) -> Unit,
onOpenWaterUnitDialog: (Boolean) -> Unit,
Expand Down Expand Up @@ -326,7 +349,7 @@ fun UnitsWaterIntake(
)
TextButton(onClick = { onOpenTimeFormatDialog(true) }) {
Text(
text = "24 hours",
text = "$timeFormate hours",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.primary
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.brandyodhiambo.settings.presentation
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.brandyodhiambo.common.domain.model.TimeFormate
import com.brandyodhiambo.common.domain.repository.TimeFormateRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -35,4 +38,23 @@ class SettingsViewModel @Inject constructor(
_repeatModeDialog.value = value
}

private val _selectedTimeFormate = mutableStateOf("12")
val selectedTimeFormate: State<String> = _selectedTimeFormate
fun onTimeFormatSelected(value:String){
_selectedTimeFormate.value = value
}

val timeFormateFromDb = timeFormateRepository.getTimeFormate()

fun insertTimeFormate(timeFormate: TimeFormate){
viewModelScope.launch {
if(timeFormateFromDb.value != null){
timeFormateRepository.deleteAllTimeFormate()
timeFormateRepository.insertTimeFormate(timeFormate)
} else{
timeFormateRepository.insertTimeFormate(timeFormate)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.brandyodhiambo.settings.presentation.component

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp

@Composable
fun TimeFormateDialog(
title: String,
items: List<String>,
selectedValue: String,
onChangeState: (String) -> Unit,
onRadioButtonClicked: (String) -> Unit,
onCustomReminderDialog: () -> Unit,
) {
val isSelectedItem: (String) -> Boolean = { selectedValue == it }

Card(
shape = RoundedCornerShape(12.dp),
modifier = Modifier.padding(10.dp, 5.dp, 10.dp, 10.dp)
) {
Column(
Modifier
.padding(8.dp)
.background(MaterialTheme.colorScheme.background)
) {
Text(
text = "Select $title",
modifier = Modifier.padding(8.dp)
)
items.forEach { item ->
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.selectable(
selected = isSelectedItem(item),
onClick = { onChangeState(item) },
role = Role.RadioButton
)
.padding(8.dp)
) {
RadioButton(
selected = isSelectedItem(item),
onClick = null
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = item,
modifier = Modifier.fillMaxWidth()
)
}
}

Row(
Modifier
.fillMaxWidth()
.padding(top = 10.dp)
.background(MaterialTheme.colorScheme.background),
horizontalArrangement = Arrangement.SpaceAround
) {
Card(
shape = RoundedCornerShape(20.dp),
modifier = Modifier
.fillMaxWidth(0.5f)
.padding(10.dp)
.clickable {
onRadioButtonClicked(selectedValue)
onCustomReminderDialog()
},
colors = CardDefaults.cardColors(
MaterialTheme.colorScheme.onBackground.copy(
alpha = 0.12f
)
)
) {
Text(
"Cancel",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onBackground,
modifier = Modifier.padding(10.dp),
textAlign = TextAlign.Center
)
}
Card(
shape = RoundedCornerShape(20.dp),
modifier = Modifier
.padding(10.dp)
.clickable {
onCustomReminderDialog()
},
colors = CardDefaults.cardColors(MaterialTheme.colorScheme.primary)
) {
Text(
"Okay",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.padding(10.dp),
textAlign = TextAlign.Center
)
}
}
}
}
}

0 comments on commit 6a1e50a

Please sign in to comment.