This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
499 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
...kitsuda/rebound/ui/components/workouteditor/durationeditor/WorkoutDurationEditorDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Copyright (c) 2022 Ankit Suda. | ||
* | ||
* Licensed under the GNU General Public License v3 | ||
* | ||
* This is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
*/ | ||
|
||
package com.ankitsuda.rebound.ui.components.workouteditor.durationeditor | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextButton | ||
import androidx.compose.runtime.* | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Dialog | ||
import com.ankitsuda.base.utils.toString | ||
import com.ankitsuda.rebound.ui.components.RButtonStyle2 | ||
import com.ankitsuda.rebound.ui.components.datetimepicker.DateTimePicker | ||
import com.ankitsuda.rebound.ui.components.workouteditor.R | ||
import com.ankitsuda.rebound.ui.theme.ReboundTheme | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
import java.time.format.FormatStyle | ||
|
||
@Composable | ||
fun WorkoutDurationEditorDialog( | ||
initialStartDateTime: LocalDateTime, | ||
initialEndDateTime: LocalDateTime, | ||
onDismissRequest: () -> Unit, | ||
onSave: (startDateTime: LocalDateTime, endDateTime: LocalDateTime) -> Unit, | ||
) { | ||
var currentPickerActiveFor by remember { | ||
mutableStateOf(0) | ||
} | ||
|
||
var startDateTime by rememberSaveable { | ||
mutableStateOf(initialStartDateTime) | ||
} | ||
|
||
var endDateTime by rememberSaveable { | ||
mutableStateOf(initialEndDateTime) | ||
} | ||
|
||
fun getFormattedDateString(dateTime: LocalDateTime): String = | ||
dateTime.format( | ||
DateTimeFormatter.ofLocalizedDateTime( | ||
FormatStyle.MEDIUM, | ||
FormatStyle.SHORT | ||
) | ||
) | ||
|
||
fun handleSaveClick() { | ||
onSave( | ||
startDateTime, | ||
endDateTime | ||
) | ||
onDismissRequest() | ||
} | ||
|
||
val formattedStartDateTime = remember(startDateTime) { | ||
getFormattedDateString(startDateTime) | ||
} | ||
|
||
val formattedEndDateTime = remember(endDateTime) { | ||
getFormattedDateString(endDateTime) | ||
} | ||
|
||
Dialog( | ||
onDismissRequest = onDismissRequest, | ||
) { | ||
Surface( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(horizontal = 32.dp), | ||
color = ReboundTheme.colors.background, | ||
shape = ReboundTheme.shapes.large | ||
) { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
) { | ||
Column( | ||
modifier = | ||
Modifier.padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 8.dp), | ||
verticalArrangement = Arrangement.spacedBy(12.dp) | ||
) { | ||
Text( | ||
text = stringResource(R.string.adjust_duration), | ||
style = ReboundTheme.typography.h6, | ||
color = ReboundTheme.colors.onBackground | ||
) | ||
|
||
Text( | ||
text = stringResource(R.string.start_time), | ||
style = ReboundTheme.typography.caption, | ||
color = ReboundTheme.colors.onBackground.copy(0.75f) | ||
) | ||
|
||
RButtonStyle2( | ||
modifier = Modifier.fillMaxWidth(), | ||
text = formattedStartDateTime, | ||
onClick = { | ||
currentPickerActiveFor = 1 | ||
} | ||
) | ||
|
||
Text( | ||
text = stringResource(R.string.end_time), | ||
style = ReboundTheme.typography.caption, | ||
color = ReboundTheme.colors.onBackground.copy(0.75f) | ||
) | ||
|
||
RButtonStyle2( | ||
modifier = Modifier.fillMaxWidth(), | ||
text = formattedEndDateTime, | ||
onClick = { | ||
currentPickerActiveFor = 2 | ||
} | ||
) | ||
|
||
DialogButtonsRow( | ||
onClickSave = ::handleSaveClick, | ||
onClickCancel = onDismissRequest | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (currentPickerActiveFor == 1 || currentPickerActiveFor == 2) { | ||
DateTimePicker( | ||
value = when (currentPickerActiveFor) { | ||
2 -> endDateTime | ||
else -> startDateTime | ||
}, | ||
onCancel = { | ||
currentPickerActiveFor = 0 | ||
}, | ||
onValueChange = { | ||
when (currentPickerActiveFor) { | ||
1 -> startDateTime = it | ||
2 -> endDateTime = it | ||
} | ||
currentPickerActiveFor = 0 | ||
} | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun ColumnScope.DialogButtonsRow( | ||
onClickSave: () -> Unit, | ||
onClickCancel: () -> Unit | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.align(Alignment.End), | ||
horizontalArrangement = Arrangement.spacedBy(8.dp) | ||
) { | ||
TextButton(onClick = onClickCancel) { | ||
Text(stringResource(id = R.string.cancel)) | ||
} | ||
TextButton(onClick = onClickSave) { | ||
Text(stringResource(id = R.string.save)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...components/src/main/java/com/ankitsuda/rebound/ui/components/datetimepicker/DatePicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2022 Ankit Suda. | ||
* | ||
* Licensed under the GNU General Public License v3 | ||
* | ||
* This is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
*/ | ||
|
||
package com.ankitsuda.rebound.ui.components.datetimepicker | ||
|
||
import android.app.DatePickerDialog | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.platform.LocalContext | ||
import java.time.LocalDate | ||
|
||
@Composable | ||
fun DatePicker( | ||
value: LocalDate, | ||
onCancel: () -> Unit = {}, | ||
onValueChange: (LocalDate) -> Unit = {}, | ||
) { | ||
val context = LocalContext.current | ||
val dialog by remember { | ||
mutableStateOf( | ||
DatePickerDialog( | ||
context, | ||
{ _, year, month, dayOfMonth -> | ||
onValueChange(LocalDate.of(year, month + 1, dayOfMonth)) | ||
}, | ||
value.year, | ||
value.monthValue - 1, | ||
value.dayOfMonth, | ||
) | ||
) | ||
} | ||
|
||
fun setupDialog() { | ||
dialog.apply { | ||
setOnCancelListener { | ||
onCancel() | ||
} | ||
} | ||
} | ||
|
||
LaunchedEffect(key1 = Unit) { | ||
setupDialog() | ||
dialog.show() | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...onents/src/main/java/com/ankitsuda/rebound/ui/components/datetimepicker/DateTimePicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2022 Ankit Suda. | ||
* | ||
* Licensed under the GNU General Public License v3 | ||
* | ||
* This is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
*/ | ||
|
||
package com.ankitsuda.rebound.ui.components.datetimepicker | ||
|
||
import androidx.compose.runtime.* | ||
import java.time.LocalDateTime | ||
|
||
@Composable | ||
fun DateTimePicker( | ||
value: LocalDateTime, | ||
onCancel: () -> Unit = {}, | ||
onValueChange: (LocalDateTime) -> Unit = {}, | ||
) { | ||
var currentView by remember { | ||
mutableStateOf(1) | ||
} | ||
|
||
var pickedDate by remember(value) { | ||
mutableStateOf(value.toLocalDate()) | ||
} | ||
|
||
var pickedTime by remember(value) { | ||
mutableStateOf(value.toLocalTime()) | ||
} | ||
|
||
when (currentView) { | ||
1 -> DatePicker( | ||
value = pickedDate, | ||
onCancel = onCancel, | ||
onValueChange = { | ||
pickedDate = it | ||
currentView = 2 | ||
} | ||
) | ||
2 -> TimePicker( | ||
value = pickedTime, | ||
onCancel = onCancel, | ||
onValueChange = { | ||
pickedTime = it | ||
currentView = 0 | ||
onValueChange(LocalDateTime.of(pickedDate, pickedTime)) | ||
} | ||
) | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...components/src/main/java/com/ankitsuda/rebound/ui/components/datetimepicker/TimePicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2022 Ankit Suda. | ||
* | ||
* Licensed under the GNU General Public License v3 | ||
* | ||
* This is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
*/ | ||
|
||
package com.ankitsuda.rebound.ui.components.datetimepicker | ||
|
||
import android.app.TimePickerDialog | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.platform.LocalContext | ||
import java.time.LocalTime | ||
|
||
@Composable | ||
fun TimePicker( | ||
value: LocalTime, | ||
is24HourView: Boolean = false, | ||
onCancel: () -> Unit = {}, | ||
onValueChange: (LocalTime) -> Unit = {}, | ||
) { | ||
val context = LocalContext.current | ||
val dialog by remember { | ||
mutableStateOf( | ||
TimePickerDialog( | ||
context, | ||
{ _, hour, minute -> | ||
onValueChange(LocalTime.of(hour, minute)) | ||
}, | ||
value.hour, | ||
value.minute, | ||
is24HourView, | ||
) | ||
) | ||
} | ||
|
||
fun setupDialog() { | ||
dialog.apply { | ||
setOnCancelListener { | ||
onCancel() | ||
} | ||
} | ||
} | ||
|
||
LaunchedEffect(key1 = Unit) { | ||
setupDialog() | ||
dialog.show() | ||
} | ||
} |
Oops, something went wrong.