Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reservation>, maxOccupancy: Int): Set<LocalDate> {
fun getFullyBookedDates(
reservations: List<Reservation>,
maxOccupancy: Int,
newGuests: Int = 1
): Set<LocalDate> {
val guestsPerDay = mutableMapOf<LocalDate, Int>()

for (reservation in reservations) {
Expand All @@ -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
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LocalDate?>(null) }
var endDate by remember { mutableStateOf<LocalDate?>(null) }
var unavailableDates by remember { mutableStateOf<Set<LocalDate>>(emptySet()) }
var errorMessage by remember { mutableStateOf<String?>(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))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Loading