Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Set proper initial date for date picker dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliyan Germanov committed Nov 5, 2021
1 parent 4273ec6 commit 5831d2c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
17 changes: 15 additions & 2 deletions app/src/main/java/com/ivy/wallet/ui/IvyActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ class IvyActivity : AppCompatActivity() {
// Make the app drawing area fullscreen (draw behind status and nav bars)
WindowCompat.setDecorFitsSystemWindows(window, false)

ivyContext.onShowDatePicker = { minDate, maxDate, onDatePicked ->
ivyContext.onShowDatePicker = { minDate,
maxDate,
initialDate,
onDatePicked ->
val picker = android.app.DatePickerDialog(this)

if (minDate != null) {
picker.datePicker.minDate = minDate.atTime(12, 0).toEpochMilli()
}
Expand All @@ -135,6 +139,15 @@ class IvyActivity : AppCompatActivity() {
onDatePicked(LocalDate.of(year, month + 1, dayOfMonth))
}
picker.show()

if (initialDate != null) {
picker.updateDate(
initialDate.year,
//month-1 because LocalDate start from 1 and date picker starts from 0
initialDate.monthValue - 1,
initialDate.dayOfMonth
)
}
}

ivyContext.onShowTimePicker = { onTimePicked ->
Expand Down Expand Up @@ -429,7 +442,7 @@ class IvyActivity : AppCompatActivity() {
putExtra(Intent.EXTRA_EMAIL, arrayOf(SUPPORT_EMAIL))
putExtra(
Intent.EXTRA_SUBJECT, "Ivy Wallet Support Request #" + caseNumber +
"0" + BuildConfig.VERSION_CODE
"0" + BuildConfig.VERSION_CODE
)
putExtra(Intent.EXTRA_TEXT, "")
}
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/ivy/wallet/ui/IvyContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,22 @@ class IvyContext {
//------------------------------------------- BackStack ----------------------------------------

//Activity help -------------------------------------------------------------------------------
lateinit var onShowDatePicker: (minDate: LocalDate?, maxDate: LocalDate?, onDatePicked: (LocalDate) -> Unit) -> Unit
lateinit var onShowDatePicker: (
minDate: LocalDate?,
maxDate: LocalDate?,
initialDate: LocalDate?,
onDatePicked: (LocalDate) -> Unit
) -> Unit
lateinit var onShowTimePicker: (onDatePicked: (LocalTime) -> Unit) -> Unit
lateinit var onContactSupport: () -> Unit

fun datePicker(
minDate: LocalDate? = null,
maxDate: LocalDate? = null,
initialDate: LocalDate?,
onDatePicked: (LocalDate) -> Unit
) {
onShowDatePicker(minDate, maxDate, onDatePicked)
onShowDatePicker(minDate, maxDate, initialDate, onDatePicked)
}

fun timePicker(onTimePicked: (LocalTime) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ private fun UI(
iconStart = R.drawable.ic_calendar,
text = startDate?.toLocalDate()?.formatDateOnly() ?: "Not set",
) {
ivyContext.datePicker {
ivyContext.datePicker(
initialDate = null
) {
onSetStartDate(it.atStartOfDay())
}
}
Expand All @@ -130,7 +132,9 @@ private fun UI(
iconStart = R.drawable.ic_calendar,
text = endDate?.toLocalDate()?.formatDateOnly() ?: "Not set",
) {
ivyContext.datePicker {
ivyContext.datePicker(
initialDate = null
) {
onSetEndDate(it.atStartOfDay())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ private fun BoxWithConstraintsScope.UI(

if (dueDate != null) {
DueDate(dueDate = dueDate) {
ivyContext.datePicker {
ivyContext.datePicker(
initialDate = dueDate.toLocalDate()
) {
onDueDateChanged(it.atTime(12, 0))
}
}
Expand All @@ -232,7 +234,9 @@ private fun BoxWithConstraintsScope.UI(
dateTime = dateTime,
dueDateTime = dueDate,
) {
ivyContext.datePicker { date ->
ivyContext.datePicker(
initialDate = dateTime?.toLocalDate(),
) { date ->
ivyContext.timePicker { time ->
onSetDateTime(date.atTime(time.hour, time.minute, time.second))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ private fun IntervalFromToDate(
maxDate = if (border == IntervalBorder.FROM)
otherEndDateTime
?.toLocalDate()
?.minusDays(1) else null
?.minusDays(1) else null,
initialDate = null
) {
onSelected(it.atStartOfDay())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.ivy.wallet.ui.theme.components.IvyCircleButton
import com.ivy.wallet.ui.theme.components.IvyDividerLine
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*

Expand Down Expand Up @@ -298,7 +299,7 @@ private fun DateRow(

Column(
modifier = Modifier.clickableNoIndication {
ivyContext.pickDate(onDatePicked)
ivyContext.pickDate(dateTime.toLocalDate(), onDatePicked)
}
) {
val date = dateTime.toLocalDate()
Expand Down Expand Up @@ -337,15 +338,22 @@ private fun DateRow(
backgroundGradient = Gradient.solid(IvyTheme.colors.pureInverse),
tint = IvyTheme.colors.pure
) {
ivyContext.pickDate(onDatePicked)
ivyContext.pickDate(dateTime.toLocalDate(), onDatePicked)
}

Spacer(Modifier.width(32.dp))
}
}

private fun IvyContext.pickDate(onDatePicked: (LocalDateTime) -> Unit) {
datePicker {
private fun IvyContext.pickDate(
initialDate: LocalDate,
onDatePicked: (
LocalDateTime
) -> Unit
) {
datePicker(
initialDate = initialDate
) {
onDatePicked(it.atTime(12, 0))
}
}
Expand Down

0 comments on commit 5831d2c

Please sign in to comment.