From 40ab6749be6be554733b69450a3e02c4ee254fde Mon Sep 17 00:00:00 2001 From: Wesley William Supangat <44500655+supwess@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:10:50 -0600 Subject: [PATCH] did same commit as previous --- .../lambda_bbb_events/LambdaEventsApp.kt | 7 + .../controller/ReservationController.kt | 12 +- .../model/repository/ReservationRepository.kt | 19 +- .../CampsiteWinnerAvailabilityScreen.kt | 206 ++++++++++++++++++ .../lottery/RegistrationStatusScreen.kt | 74 +------ 5 files changed, 236 insertions(+), 82 deletions(-) create mode 100644 code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/CampsiteWinnerAvailabilityScreen.kt diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/LambdaEventsApp.kt b/code/app/src/main/java/com/example/lambda_bbb_events/LambdaEventsApp.kt index 25f08abe..4a4f9b7a 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/LambdaEventsApp.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/LambdaEventsApp.kt @@ -76,6 +76,7 @@ import com.example.lambda_bbb_events.view.ui.user_screens.notification.ChatRoute import kotlinx.coroutines.CancellationException import androidx.compose.runtime.rememberCoroutineScope import com.example.lambda_bbb_events.view.ui.owner_screens.LotteryRequestsScreen +import com.example.lambda_bbb_events.view.ui.user_screens.bookings.CampsiteWinnerAvailabilityScreen import kotlinx.coroutines.launch import com.example.lambda_bbb_events.view.ui.user_screens.explore.CampsiteSection import com.example.lambda_bbb_events.view.ui.user_screens.explore.SeeAllCampsitesScreen @@ -285,6 +286,12 @@ private fun MainApp( ) } + composable("winner_availability/{entryId}/{campsiteId}") { backStackEntry -> + val entryId = backStackEntry.arguments?.getString("entryId") ?: "" + val campsiteId = backStackEntry.arguments?.getString("campsiteId") ?: "" + CampsiteWinnerAvailabilityScreen(navController, controllers, entryId, campsiteId) + } + composable("booking_receipt/{reservationId}") { backStackEntry -> val reservationId = diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/controller/ReservationController.kt b/code/app/src/main/java/com/example/lambda_bbb_events/controller/ReservationController.kt index 3ad801bd..9f67f738 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/controller/ReservationController.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/controller/ReservationController.kt @@ -141,11 +141,18 @@ class ReservationController( * * @param reservations The reservations of the campsite. * @param maxOccupancy The max occupancy of the campsite. + * @param newGuests How many more guests need room. A day is unavailable if its booked + * guests plus these would go over the max occupancy. The default of 1 gives the days + * that are completely full. * @return The set of fully booked days. * * Reference: Claude created this function. */ - fun getFullyBookedDates(reservations: List, maxOccupancy: Int): Set { + fun getFullyBookedDates( + reservations: List, + maxOccupancy: Int, + newGuests: Int = 1 + ): Set { val guestsPerDay = mutableMapOf() for (reservation in reservations) { @@ -162,7 +169,8 @@ class ReservationController( } } - return guestsPerDay.filter { it.value >= maxOccupancy }.keys + // guests can't reserve date if their num of guests + current booked guests in that day is over max capacity + return guestsPerDay.filter { it.value + newGuests > maxOccupancy }.keys } diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/model/repository/ReservationRepository.kt b/code/app/src/main/java/com/example/lambda_bbb_events/model/repository/ReservationRepository.kt index e38826fc..d8920313 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/model/repository/ReservationRepository.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/model/repository/ReservationRepository.kt @@ -164,21 +164,12 @@ class ReservationRepository( "Invalid campsite or campsite selection required." } - // Convert the lottery booking dates to LocalDate. - val bookingStart = lottery.bookingStart - .toInstant() - .atZone(ZoneId.systemDefault()) - .toLocalDate() - - val bookingEnd = lottery.bookingEnd - .toInstant() - .atZone(ZoneId.systemDefault()) - .toLocalDate() - - // Validate the requested reservation dates. + val bookingStart = LocalDate.parse(fromDate) + val bookingEnd = LocalDate.parse(endDate) + require( - LocalDate.parse(fromDate) == bookingStart && - LocalDate.parse(endDate) == bookingEnd + !bookingEnd.isBefore(bookingStart) && + !bookingStart.isBefore(LocalDate.now()) ) { "Invalid reservation dates." } diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/CampsiteWinnerAvailabilityScreen.kt b/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/CampsiteWinnerAvailabilityScreen.kt new file mode 100644 index 00000000..49a3cb1e --- /dev/null +++ b/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/CampsiteWinnerAvailabilityScreen.kt @@ -0,0 +1,206 @@ + +package com.example.lambda_bbb_events.view.ui.user_screens.bookings + +import com.example.lambda_bbb_events.model.formatting.parseDateOrNull +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.LocationOn +import androidx.compose.material.icons.outlined.Person +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.navigation.NavController +import com.example.lambda_bbb_events.controller.AppControllers +import com.example.lambda_bbb_events.view.ui.components.calendar.CampsiteAvailabilityCalendar +import com.example.lambda_bbb_events.view.ui.components.calendar.DatePickerField +import com.example.lambda_bbb_events.view.ui.components.rating.RatingSummary +import com.example.lambda_bbb_events.view.ui.components.common.ScreenTitle +import com.example.lambda_bbb_events.view.ui.components.common.FormTextFieldWithTitle +import java.time.LocalDate +import java.time.YearMonth +import com.example.lambda_bbb_events.view.ui.components.common.CampsiteThumbnail +import androidx.compose.ui.draw.clip +import com.example.lambda_bbb_events.controller.ReservationFilter +import com.example.lambda_bbb_events.view.ui.components.campsite.VerticalCampsiteCard +import kotlinx.coroutines.launch + +/** + * Displays campsite information and a form for selecting their + * reservation dates. + * + * Navigates to the booking receipt status screen when the + * entered dates and guest count pass validation. + * + * @param navController Handles screen navigation. + * @param controllers Retrieves campsite information. + * @param entryId The ID of the waitlist entry. + * @param campsiteId The ID of the selected campsite. + */ +@Composable +fun CampsiteWinnerAvailabilityScreen( + navController: NavController, + controllers: AppControllers, + entryId: String, + campsiteId: String +) { + val entry = controllers.waitlistEntries.getEntryById(entryId) + val campsite = controllers.campsites.getCampsite(campsiteId) + var isLoading by remember { mutableStateOf(true) } + val scope = rememberCoroutineScope() + + var startDate by remember { mutableStateOf(null) } + var endDate by remember { mutableStateOf(null) } + var unavailableDates by remember { mutableStateOf>(emptySet()) } + var errorMessage by remember { mutableStateOf(null) } + var isConfirming by remember { mutableStateOf(false) } + + // Find the days this campsite is already full on. + LaunchedEffect(campsiteId, campsite != null, entry?.numberOfGuests) { + val maxOccupancy = campsite?.maxOccupancy ?: return@LaunchedEffect + + unavailableDates = try { + controllers.reservations.getFullyBookedDates( + controllers.reservations.getReservationsForCampsite( + campsiteId, + ReservationFilter.ALL + ), + maxOccupancy, + entry?.numberOfGuests ?: 1 + ) + } catch (e: Exception) { // if fails then no unavailable + emptySet() + } + isLoading = false + } + + Column( + modifier = Modifier.fillMaxSize() + ) { + ScreenTitle( + title = "Choose Reservation Dates", + navController = navController, + showBack = true + ) + + if (entry == null || campsite == null || isLoading) { + Box( + modifier = Modifier + .fillMaxWidth() + .weight(1f), + contentAlignment = Alignment.Center + ) { + CircularProgressIndicator( + color = MaterialTheme.colorScheme.primary + ) + } + } else { + val fitsGuests = entry.numberOfGuests <= campsite.maxOccupancy + val start = startDate + val end = endDate + + Column( + modifier = Modifier + .fillMaxSize() + .background(MaterialTheme.colorScheme.background) + .verticalScroll(rememberScrollState()), + verticalArrangement = Arrangement.spacedBy(16.dp) + ) { + VerticalCampsiteCard( + campsite = campsite, + onClick = {} + ) + + Text( + text = "Number of Guests: ${entry.numberOfGuests}", + style = MaterialTheme.typography.bodyMedium + ) + + if (!fitsGuests) { + Text( + text = "You have more people than the max occupancy (${campsite.maxOccupancy}.", + color = MaterialTheme.colorScheme.error + ) + } + + Text( + text = "Select Dates", + style = MaterialTheme.typography.titleMedium + ) + + CampsiteAvailabilityCalendar( + initialMonth = YearMonth.now(), + unavailableDates = unavailableDates, + startDate = startDate, + endDate = endDate, + onDateRangeChange = { s, e -> + startDate = s + endDate = e + } + ) + + Text( + text = if (start != null && end != null) { + "Selected: $start to $end" + } else { + "Pick a start and end date." + }, + style = MaterialTheme.typography.bodyMedium + ) + + errorMessage?.let { + Text(text = it, color = MaterialTheme.colorScheme.error) + } + + Button( + onClick = { + if (start != null && end != null && !isConfirming) { + isConfirming = true + errorMessage = null + + scope.launch { + try { + controllers.reservations.confirmLotteryReservation( + entryId = entryId, + entrantId = controllers.users.user.deviceId, + campsiteId = campsiteId, + fromDate = start, + endDate = end, + numberOfGuests = entry.numberOfGuests + ) + + navController.navigate("booking_receipt/lottery_$entryId") { + popUpTo("registrationStatus/{entryId}") { + inclusive = true + } + launchSingleTop = true + } + } catch (e: Exception) { + errorMessage = e.message ?: "Failed to confirm reservation." + } finally { + isConfirming = false + } + } + } + }, + enabled = start != null && end != null && fitsGuests && !isConfirming, + modifier = Modifier.fillMaxWidth() + ) { + Text( + text = if (isConfirming) "Confirming..." else "Confirm Reservation", + style = MaterialTheme.typography.labelLarge + ) + } + + Spacer(modifier = Modifier.height(16.dp)) + } + } + } +} diff --git a/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/lottery/RegistrationStatusScreen.kt b/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/lottery/RegistrationStatusScreen.kt index 863db59b..698a91e8 100644 --- a/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/lottery/RegistrationStatusScreen.kt +++ b/code/app/src/main/java/com/example/lambda_bbb_events/view/ui/user_screens/bookings/lottery/RegistrationStatusScreen.kt @@ -300,77 +300,19 @@ fun RegistrationStatusScreen( if (entry.status == WaitlistStatus.WON) { Button( - onClick = confirmClick@{ - - val selectedCampsiteId = - campsiteId ?: return@confirmClick - - val selectedFromDate = - fromDate ?: return@confirmClick - - val selectedEndDate = - endDate ?: return@confirmClick - - val confirmReservation = - onConfirmReservation - - if (confirmReservation == null) { - errorMessage = - "Reservation confirmation is not connected yet." - return@confirmClick - } - - if (isConfirming) return@confirmClick - - isConfirming = true - errorMessage = null - - scope.launch { - try { - - // Save the confirmed reservation. - confirmReservation( - entryId, - selectedCampsiteId, - selectedFromDate, - selectedEndDate, - entry.numberOfGuests - ) - - // Navigate directly to Booking Receipt. - navController.navigate("booking_receipt/lottery_$entryId") { - popUpTo("registrationStatus/{entryId}") { - inclusive = true - } - - launchSingleTop = true - } - - } catch (e: CancellationException) { - throw e - - } catch (e: Exception) { - errorMessage = e.message - ?: "Failed to confirm reservation." - - } finally { - isConfirming = false - } + onClick = { + val selectedCampsiteId = campsiteId + if (selectedCampsiteId != null) { + navController.navigate( + "winner_availability/$entryId/$selectedCampsiteId" + ) } }, - enabled = !isConfirming && - !isDeclining && - campsiteId != null && - fromDate != null && - endDate != null, + enabled = !isDeclining && campsiteId != null, modifier = Modifier.fillMaxWidth() ) { Text( - text = if (isConfirming) { - "Confirming..." - } else { - "Confirm Reservation" - }, + text = "Choose Reservation Dates", style = MaterialTheme.typography.labelLarge ) }