From 96f86b9cd8afc14be21f97be3d516825bb327d53 Mon Sep 17 00:00:00 2001 From: mohammed Elsdody <31186483+mohamedma872@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:34:16 +0300 Subject: [PATCH] fix some issues --- .../HijriDatePickerButton.kt | 2 +- .../HijriDatePickerDialogWithThreeSections.kt | 43 ++++++++++++------- .../hijridatepickerlib/showHijriDatePicker.kt | 33 ++++++-------- .../MainActivity.kt | 15 +++++-- 4 files changed, 54 insertions(+), 39 deletions(-) diff --git a/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerButton.kt b/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerButton.kt index 5ef1d09..5f1fa83 100644 --- a/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerButton.kt +++ b/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerButton.kt @@ -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 = { diff --git a/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerDialogWithThreeSections.kt b/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerDialogWithThreeSections.kt index 14de701..0c051b7 100644 --- a/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerDialogWithThreeSections.kt +++ b/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/HijriDatePickerDialogWithThreeSections.kt @@ -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 @@ -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 @@ -78,7 +83,9 @@ fun HijriDatePickerDialogWithThreeSections( } else { // Month Grid with Days Section Box( - modifier = Modifier.weight(1f).fillMaxWidth() + modifier = Modifier + .weight(1f) + .fillMaxWidth() ) { MonthGridWithDays( selectedYear = selectedYear, @@ -86,7 +93,7 @@ fun HijriDatePickerDialogWithThreeSections( 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 @@ -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() { @@ -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 ) -} \ No newline at end of file +} diff --git a/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/showHijriDatePicker.kt b/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/showHijriDatePicker.kt index 87af81a..24ccf66 100644 --- a/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/showHijriDatePicker.kt +++ b/HijriDatePickerLib/src/main/java/com/sdody/hijridatepickerlib/showHijriDatePicker.kt @@ -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() { diff --git a/app/src/main/java/com/sdody/HijriDatePickerPlusApplication/MainActivity.kt b/app/src/main/java/com/sdody/HijriDatePickerPlusApplication/MainActivity.kt index 5c698a2..20d9323 100644 --- a/app/src/main/java/com/sdody/HijriDatePickerPlusApplication/MainActivity.kt +++ b/app/src/main/java/com/sdody/HijriDatePickerPlusApplication/MainActivity.kt @@ -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 = {