Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
braiso-22 committed Apr 26, 2024
2 parents d751969 + 51699aa commit ca1ab4a
Show file tree
Hide file tree
Showing 67 changed files with 2,462 additions and 955 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)

// LiveData
implementation (libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.lifecycle.livedata.ktx)

// Coroutine Lifecycle Scopes
implementation(libs.androidx.lifecycle.viewmodel.ktx)
Expand All @@ -99,7 +99,7 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.material3.window.size)

implementation(libs.androidx.material.icons.extended)
// Testing
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/braiso_22/cozycave/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.ui.Modifier
import com.braiso_22.cozycave.feature_task.ui.tasks.TasksScreen
import com.braiso_22.cozycave.router.AppNavigation
import com.braiso_22.cozycave.ui.theme.CozyCaveTheme
import dagger.hilt.android.AndroidEntryPoint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.braiso_22.cozycave.date_time_selector.presentation.date_selector

import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.braiso_22.cozycave.date_time_selector.presentation.date_selector.comps.DatePickerDialogWrapper
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@Composable
fun DateSelector(
state: String,
setState: (String) -> Unit,
modifier: Modifier = Modifier,
) {
val date = try {
LocalDate.parse(state, DateTimeFormatter.ofPattern("dd/MM/yyyy"))
} catch (e: Exception) {
LocalDate.now()
}

DateSelectorComponent(
date = date,
setDate = {
setState(
it.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))
)
},
modifier = modifier
)
}

@Composable
private fun DateSelectorComponent(
date: LocalDate,
setDate: (LocalDate) -> Unit,
modifier: Modifier = Modifier,
) {
var isDateOpen by remember {
mutableStateOf(false)
}
DatePickerDialogWrapper(
isVisible = isDateOpen,
setVisible = { isDateOpen = it },
state = date.toEpochDay().times(86_400_000),
setState = {
setDate(
try {
LocalDate.ofEpochDay(it.div(86_400_000))
} catch (e: Exception) {
LocalDate.now()
}
)
}
)
Text(
text = date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")),
modifier = modifier.clickable {
isDateOpen = true
}
)
}

@Preview
@Composable
private fun DateSelectorPreview() {
DateSelector(
state = "",
setState = {}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.braiso_22.cozycave.date_time_selector.presentation.date_selector.comps

import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.braiso_22.cozycave.R
import kotlin.math.absoluteValue

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatePickerDialogWrapper(
isVisible: Boolean,
setVisible: (Boolean) -> Unit,
state: Long,
setState: (Long) -> Unit,
modifier: Modifier = Modifier,
) {
val datePickerState = rememberDatePickerState(
initialSelectedDateMillis = state
)

if (isVisible) {
DatePickerDialog(
onDismissRequest = { setVisible(false) },
confirmButton = {
TextButton(
onClick = {
setVisible(false)
setState(datePickerState.selectedDateMillis?.absoluteValue ?: 0)
}
) {
Text(stringResource(R.string.accept))
}

},
dismissButton = {
TextButton(onClick = { setVisible(false) }) {
Text(stringResource(R.string.cancel))
}
},
modifier = modifier
) {
DatePicker(state = datePickerState)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.braiso_22.cozycave.date_time_selector.presentation.time_selector

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.braiso_22.cozycave.date_time_selector.presentation.time_selector.comps.TimePickerDialogWrapper
import java.time.LocalTime
import java.time.format.DateTimeFormatter

@Composable
fun TimeSelector(
state: String,
setState: (String) -> Unit,
modifier: Modifier = Modifier,
) {
val time = try {
LocalTime.parse(state)
} catch (e: Exception) {
LocalTime.now()
}

DateTimeSelectorContent(
time = time,
setTime = {
setState(
it.format(DateTimeFormatter.ofPattern("HH:mm"))
)
},
modifier = modifier
)
}

@Composable
private fun DateTimeSelectorContent(
time: LocalTime,
setTime: (LocalTime) -> Unit,
modifier: Modifier = Modifier,
) {
var isTimeOpen by remember {
mutableStateOf(false)
}

TimePickerDialogWrapper(
isVisible = isTimeOpen,
setVisible = { isTimeOpen = it },
state = TimeState(time.hour, time.minute),
setState = {
setTime(LocalTime.of(it.hour, it.minute))
}
)

Text(
text = time.format(DateTimeFormatter.ofPattern("HH:mm")),
modifier = modifier.clickable {
isTimeOpen = true
}
)
}

@Preview
@Composable
private fun DateTimeSelectorPreview() {
Surface {
TimeSelector(
state = "",
setState = {

},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.braiso_22.cozycave.date_time_selector.presentation.time_selector

data class TimeState(
val hour: Int,
val minute: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.braiso_22.cozycave.date_time_selector.presentation.time_selector.comps


import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Keyboard
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TimePickerDialog(
state: TimePickerState,
onDismissRequest: () -> Unit,
confirmButton: @Composable () -> Unit,
modifier: Modifier = Modifier,
dismissButton: @Composable (() -> Unit)? = null,
) {
var isTimePicker by remember {
mutableStateOf(true)
}

Dialog(
onDismissRequest = onDismissRequest,
properties = DialogProperties(
usePlatformDefaultWidth = false,
)
) {
Surface(
shape = MaterialTheme.shapes.extraLarge,
modifier = Modifier
.width(IntrinsicSize.Min)
.height(IntrinsicSize.Min)
.background(
shape = MaterialTheme.shapes.extraLarge,
color = MaterialTheme.colorScheme.surface
)
) {
Column(modifier.padding(8.dp)) {
Text("Select a time", Modifier.padding(8.dp))
if (isTimePicker) {
TimePicker(state = state)
} else {
TimeInput(state = state)
}

Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.Bottom,
modifier = Modifier.fillMaxWidth()
) {
IconButton(onClick = { isTimePicker = !isTimePicker }) {
Icon(
imageVector = Icons.Outlined.Keyboard,
"Change to write mode"
)
}
if (dismissButton != null) {
dismissButton()
}
confirmButton()
}
}
}
}

}

@OptIn(ExperimentalMaterial3Api::class)
@PreviewScreenSizes
@Composable
private fun TimePickerPreview() {
Box(Modifier.fillMaxSize()) {
TimePickerDialog(
state = rememberTimePickerState(),
onDismissRequest = {},
confirmButton = {
TextButton(onClick = { /*TODO*/ }) {
Text("Accept")
}
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.braiso_22.cozycave.date_time_selector.presentation.time_selector.comps

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.braiso_22.cozycave.R
import com.braiso_22.cozycave.date_time_selector.presentation.time_selector.TimeState

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TimePickerDialogWrapper(
isVisible: Boolean,
setVisible: (Boolean) -> Unit,
state: TimeState,
setState: (TimeState) -> Unit,
modifier: Modifier = Modifier,
) {
val timePickerState = rememberTimePickerState(
is24Hour = true,
initialMinute = state.minute,
initialHour = state.hour,
)
if (isVisible) {
TimePickerDialog(
state = timePickerState,
onDismissRequest = { setVisible(false) },
confirmButton = {
TextButton(
onClick = {
setVisible(false)
setState(
TimeState(timePickerState.hour, timePickerState.minute)
)
}
) {
Text(stringResource(R.string.accept))
}
},
dismissButton = {
TextButton(onClick = { setVisible(false) }) {
Text(stringResource(R.string.cancel))
}
},
modifier = modifier
)
}
}
Loading

0 comments on commit ca1ab4a

Please sign in to comment.