Skip to content

Commit

Permalink
fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedma872 committed Sep 26, 2024
1 parent fa88ead commit 96f86b9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fun HijriDatePickerButton(
preselectedMonth.value = month
preselectedDay.value = day
},
onConfirm = {
onConfirm = { year, month, day ->
showDialog = false // Close the dialog when Confirm is clicked
},
onDismissRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fun HijriDatePickerDialogWithThreeSections(
initialYear: Int,
initialMonth: Int,
initialDay: Int,
onDateSelected: (Int, Int, Int) -> Unit,
onConfirm: () -> Unit,
onDateSelected: (Int, Int, Int) -> Unit, // Call this when date is selected
onConfirm: (Int, Int, Int) -> Unit, // Pass the selected year, month, day
onDismissRequest: () -> Unit,
initialShowYearSelection: Boolean = true, // Always show year selection when opening the dialog
calendarType: String
Expand All @@ -39,25 +39,30 @@ fun HijriDatePickerDialogWithThreeSections(
var showYearSelection by remember { mutableStateOf(initialShowYearSelection) }

// Ensure selected day is valid for the selected month
val daysInMonth = getHijriDaysInMonth(selectedYear, selectedMonth,calendarType)
val daysInMonth = getHijriDaysInMonth(selectedYear, selectedMonth, calendarType)
if (selectedDay > daysInMonth) {
selectedDay = daysInMonth
}

Dialog(onDismissRequest = onDismissRequest) {
Surface(
modifier = Modifier.fillMaxWidth().heightIn(min = 400.dp, max = 500.dp).padding(16.dp),
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 400.dp, max = 500.dp)
.padding(16.dp),
shape = MaterialTheme.shapes.medium
) {
Column(
modifier = Modifier.fillMaxWidth().background(Color.White)
modifier = Modifier
.fillMaxWidth()
.background(Color.White)
) {
// Header section with the selected date
val calendar = getIslamicCalendar(calendarType)
// Set the specific year, month, and day on the calendar instance
calendar.set(Calendar.YEAR, selectedYear)
calendar.set(Calendar.MONTH, selectedMonth)
calendar.set(Calendar.DAY_OF_MONTH, selectedDay)

// Pass the callback to trigger year selection
HeaderSection(calendar = calendar) {
showYearSelection = true // Toggle to show year selection when the year is clicked
Expand All @@ -78,15 +83,17 @@ fun HijriDatePickerDialogWithThreeSections(
} else {
// Month Grid with Days Section
Box(
modifier = Modifier.weight(1f).fillMaxWidth()
modifier = Modifier
.weight(1f)
.fillMaxWidth()
) {
MonthGridWithDays(
selectedYear = selectedYear,
onDaySelected = { year, month, day ->
selectedYear = year
selectedMonth = month
selectedDay = day
onDateSelected(year, month, day)
onDateSelected(year, month, day) // Update selected date
},
preselectedMonth = selectedMonth, // Pass the preselected month
preselectedDay = selectedDay, // Pass the preselected day
Expand All @@ -100,14 +107,17 @@ fun HijriDatePickerDialogWithThreeSections(
// Footer with Confirm and Cancel buttons
FooterSection(
nextMonthName = getHijriMonthName(selectedMonth),
onConfirm = onConfirm,
onCancel = onDismissRequest
onConfirm = {
onConfirm(selectedYear, selectedMonth, selectedDay) // Pass the selected date when confirmed
},
onCancel = onDismissRequest // Handle dismissal when cancel is clicked
)
}
}
}
}


@Preview(showBackground = true)
@Composable
fun PreviewHijriDatePickerDialogWithThreeSections() {
Expand All @@ -116,14 +126,17 @@ fun PreviewHijriDatePickerDialogWithThreeSections() {
initialMonth = 1, // Safar (month index starts at 0)
initialDay = 5, // 5th day of Safar
onDateSelected = { year, month, day ->
// Handle date selection (preview action)
// Simulate the date selection in the preview (just log or print)
println("Date Selected in Preview: $day-${getHijriMonthName(month)}-$year")
},
onConfirm = {
// Handle confirm action (preview action)
onConfirm = { year, month, day ->
// Simulate confirmation action in the preview
println("Date Confirmed in Preview: $day-${getHijriMonthName(month)}-$year")
},
onDismissRequest = {
// Handle dismiss action (preview action)
// Simulate dismiss action in the preview
println("Dialog Dismissed in Preview")
},
calendarType = "umalqura" // Simulate the "umalqura" calendar type for preview
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,34 @@ fun showHijriDatePicker(
onDismissRequest: () -> Unit,
calendarType: String
) {
// State for preselected date
val preselectedYear = remember { mutableStateOf(initialYear) }
val preselectedMonth = remember { mutableStateOf(initialMonth) }
val preselectedDay = remember { mutableStateOf(initialDay) }

// Dialog visibility is controlled outside (no button, it will be triggered by external events)
// The showDialog state is controlled outside, as this Composable doesn't have buttons.
var showDialog by remember { mutableStateOf(true) }

if (showDialog) {
HijriDatePickerDialogWithThreeSections(
initialYear = preselectedYear.value,
initialMonth = preselectedMonth.value,
initialDay = preselectedDay.value,
initialYear = initialYear, // Preselected year from the lifted state
initialMonth = initialMonth, // Preselected month from the lifted state
initialDay = initialDay, // Preselected day from the lifted state
onDateSelected = { year, month, day ->
// Update the preselected date for the next opening
preselectedYear.value = year
preselectedMonth.value = month
preselectedDay.value = day
onDateSelected(year, month, day) // Call the provided callback
// Update the selected date in the parent
onDateSelected(year, month, day)
},
onConfirm = {
showDialog = false // Close the dialog when Confirm is clicked
onConfirm = { year, month, day ->
showDialog = false // Close the dialog when Confirm is clicked
// Pass the selected date to the confirm callback
onConfirm(preselectedYear.value, preselectedMonth.value, preselectedDay.value)
onConfirm(year, month, day)
},
onDismissRequest = {
showDialog = false // Close the dialog when dismissed
onDismissRequest() // Call the dismiss callback
showDialog = false // Close the dialog when dismissed
onDismissRequest() // Call the dismiss callback
},
initialShowYearSelection = true, // Always show year selection first
calendarType = calendarType // "umalqura", "civil", or "islamic"
calendarType = calendarType // Calendar type ("umalqura", "civil", or "islamic")
)
}
}


@Preview(showBackground = true)
@Composable
fun PreviewHijriDatePicker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,27 @@ class MainActivity : ComponentActivity() {
setContent {
// Use "umalqura", "civil", or "islamic"
// HijriDatePickerButton(calendarType = "umalqura")
var lastSelectedYear = 1445
var lastSelectedMonth = 1
var lastSelectedDay = 1

// Example of how you can trigger the date picker from anywhere
showHijriDatePicker(
initialYear = 1445,
initialMonth = 1,
initialDay = 1,
initialYear = lastSelectedYear,
initialMonth = lastSelectedMonth,
initialDay = lastSelectedDay,
onDateSelected = { year, month, day ->
// Handle date selection changes
lastSelectedYear = year
lastSelectedMonth = month
lastSelectedDay = day
println("Selected date: $year-$month-$day")
},
onConfirm = { year, month, day ->
// Handle the final confirmed date here
lastSelectedYear = year
lastSelectedMonth = month
lastSelectedDay = day
println("Confirmed date: $year-$month-$day")
},
onDismissRequest = {
Expand Down

0 comments on commit 96f86b9

Please sign in to comment.