-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from Wottrich/issues-84
Selecting a date less than today crashes the application - issue #84
- Loading branch information
Showing
4 changed files
with
111 additions
and
31 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
46 changes: 46 additions & 0 deletions
46
...src/main/java/com/waseefakhtar/doseapp/feature/addmedication/DatePickerDialogComponent.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,46 @@ | ||
package com.waseefakhtar.doseapp.feature.addmedication | ||
|
||
import android.app.DatePickerDialog | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalContext | ||
import com.waseefakhtar.doseapp.feature.addmedication.model.CalendarInformation | ||
import java.util.Calendar | ||
|
||
@Composable | ||
fun DatePickerDialogComponent( | ||
showDialog: Boolean, | ||
selectedDate: CalendarInformation, | ||
onSelectedDate: (selectedDate: CalendarInformation) -> Unit | ||
) { | ||
val listener = setUpOnDateSetListener(onSelectedDate) | ||
val datePickerDialog = getDatePickerDialog(selectedDate, listener) | ||
if (showDialog) { | ||
datePickerDialog.datePicker.minDate = Calendar.getInstance().timeInMillis | ||
datePickerDialog.show() | ||
} | ||
} | ||
|
||
private fun setUpOnDateSetListener( | ||
onSelectedDate: (selectedDate: CalendarInformation) -> Unit | ||
): DatePickerDialog.OnDateSetListener { | ||
return DatePickerDialog.OnDateSetListener { _, selectedYear, selectedMonth, selectedDay -> | ||
val newDate = Calendar.getInstance().apply { set(selectedYear, selectedMonth, selectedDay) } | ||
onSelectedDate(CalendarInformation(newDate)) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun getDatePickerDialog( | ||
selectedDate: CalendarInformation, | ||
listener: DatePickerDialog.OnDateSetListener | ||
): DatePickerDialog { | ||
val context = LocalContext.current | ||
val (year, month, day) = selectedDate.dateInformation | ||
return DatePickerDialog( | ||
context, | ||
listener, | ||
year, | ||
month, | ||
day | ||
) | ||
} |
47 changes: 47 additions & 0 deletions
47
...src/main/java/com/waseefakhtar/doseapp/feature/addmedication/model/CalendarInformation.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,47 @@ | ||
package com.waseefakhtar.doseapp.feature.addmedication.model | ||
|
||
import androidx.compose.runtime.saveable.Saver | ||
import java.text.SimpleDateFormat | ||
import java.util.Calendar | ||
import java.util.Locale | ||
import java.util.MissingFormatArgumentException | ||
|
||
class CalendarInformation(private val calendar: Calendar) { | ||
|
||
val dateInformation = DateInformation( | ||
year = calendar.get(Calendar.YEAR), | ||
month = calendar.get(Calendar.MONTH), | ||
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) | ||
) | ||
|
||
fun getTimeInMillis() = calendar.timeInMillis | ||
|
||
fun getDateFormatted(pattern: String): String { | ||
return try { | ||
SimpleDateFormat(pattern, Locale.getDefault()).format(calendar.time) | ||
} catch (ex: MissingFormatArgumentException) { | ||
throw ex | ||
} | ||
} | ||
|
||
inner class DateInformation( | ||
val year: Int, | ||
val month: Int, | ||
val dayOfMonth: Int | ||
) { | ||
operator fun component1(): Int = year | ||
operator fun component2(): Int = month | ||
operator fun component3(): Int = dayOfMonth | ||
} | ||
|
||
companion object { | ||
fun getStateSaver() = Saver<CalendarInformation, Calendar>( | ||
save = { state -> | ||
state.calendar | ||
}, | ||
restore = { | ||
CalendarInformation(it) | ||
} | ||
) | ||
} | ||
} |
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