Created
November 21, 2017 14:23
-
-
Save Bashta/97821afde279268c4070f68ae5df34c1 to your computer and use it in GitHub Desktop.
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
private func updateState() { | |
// Make sure all the information is available | |
guard let rentalState = rentalState else { | |
self.state.value = .Unknown | |
return | |
} | |
let isPaymentInProgress = (paymentInProgress ?? false) | |
// Save whether address details should be shown before updating state | |
let showAddressDetailsBeforeUpdatingState = showAddressDetails() | |
// Translate RentalState together with variables into BookingState | |
switch rentalState { | |
case .NotSend: | |
if isRenter { | |
self.state.value = .RequestNotSent | |
} else { | |
// Owner should never see bookings that have not been sent (should never happen) | |
self.state.value = .Unknown | |
} | |
case .Requested: | |
self.state.value = .Requested | |
case .Denied: | |
self.state.value = .Declined | |
case .Cancelled: | |
self.state.value = .Cancelled | |
case .PaymentCancelled: | |
self.state.value = .Unknown | |
case .Accepted: | |
if isRenter && isPaymentInProgress { | |
// Only the renter sees whether the payment is processing | |
self.state.value = .PaymentProcessing | |
} else { | |
self.state.value = .Accepted | |
} | |
case .PaymentAuthorized: | |
if let canCheckIn = checkInCanStart, let isCheckInStarted = checkInStarted, let isCheckInCompleted = checkInCompleted, let isCheckOutStarted = checkOutStarted, let isCheckOutCompleted = checkOutCompleted { | |
if isCheckOutCompleted { | |
// Mark the booking as finished if it was checked out already | |
self.state.value = .Finished | |
} else if isCheckOutStarted { | |
// Mark the booking as check-out started if the user started but did not finish the check-out | |
self.state.value = .CheckOutStarted | |
} else if isCheckInCompleted { | |
// Mark the booking as started if it was checked in but not checked out yet | |
self.state.value = .Started | |
} else if isCheckInStarted { | |
// Mark the booking as check-in started if the user started but did not finish the check-in | |
self.state.value = .CheckInStarted | |
} else if canCheckIn { | |
// Mark the booking as needing check-in if it can be checked in | |
self.state.value = .CanCheckIn | |
} else { | |
// Mark the booking as paid if it cannot be checked in yet | |
self.state.value = .Paid | |
} | |
} else { | |
// Check-in/out status not available, mark as paid | |
self.state.value = .Paid | |
} | |
case .RentalStarted: | |
guard let isCheckInStarted = checkInStarted, let isCheckInCompleted = checkInCompleted, let isCheckOutStarted = checkOutStarted, let isCheckOutCompleted = checkOutCompleted else { | |
// Check-in/out status is mandatory when booking has started to prevent showing the wrong state | |
self.state.value = .Unknown | |
return | |
} | |
if isCheckOutCompleted { | |
// Mark the booking as finished if it was checked out already | |
self.state.value = .Finished | |
} else if isCheckOutStarted { | |
// Mark the booking as check-out started if the user started but did not finish the check-out | |
self.state.value = .CheckOutStarted | |
} else if isCheckInCompleted { | |
// Mark the booking as started if it was checked in but not checked out yet | |
self.state.value = .Started | |
} else if isCheckInStarted { | |
// Mark the booking as check-in started if the user started but did not finish the check-in | |
self.state.value = .CheckInStarted | |
} else { | |
// Mark the booking as needing check-in if it has started but was not checked in yet | |
self.state.value = .CanCheckIn | |
} | |
case .RentalEnded: | |
guard let canCheckIn = checkInCanStart, let isCheckInStarted = checkInStarted, let isCheckInCompleted = checkInCompleted, let canCheckOut = checkOutCanStart, let isCheckOutStarted = checkOutStarted, let isCheckOutCompleted = checkOutCompleted else { | |
// Check-in/out status is mandatory when booking has ended to prevent showing the wrong state | |
self.state.value = .Unknown | |
return | |
} | |
if isCheckOutCompleted { | |
// Mark the booking as finished if it was checked out | |
self.state.value = .Finished | |
} else if isCheckInCompleted { | |
if canCheckOut { | |
if isCheckOutStarted { | |
// Mark the booking as check-out started if the user started but did not finish the check-out, and can still be checked out | |
self.state.value = .CheckOutStarted | |
} else { | |
// Mark the booking as needing check-out if it was checked in and not checked out yet, and can still be checked out | |
self.state.value = .NeedsCheckOut | |
} | |
} else { | |
// Mark the booking as finished if it was checked in and not checked out yet, and cannot be checked out anymore | |
self.state.value = .Finished | |
} | |
} else if canCheckIn { | |
if isCheckInStarted { | |
// Mark the booking as check-in started if it has ended and the user started the check-in and can still finish it | |
self.state.value = .CheckInStarted | |
} else { | |
// Mark the booking as needing check-in if it has ended and was never checked in, but can still be checked in | |
self.state.value = .CanCheckIn | |
} | |
} else { | |
// Mark the booking as finished if it has ended and was never checked in, and cannot be checked in anymore | |
self.state.value = .Finished | |
} | |
case .TransactionDone: | |
if isRenter { | |
// The renter doesn't see whether the owner has been paid out or not | |
self.state.value = .Finished | |
} else { | |
self.state.value = .PaidOut | |
} | |
case .DeclinedBySystem: | |
self.state.value = .CancelledBySystem | |
case .Overdue: | |
self.state.value = .Expired | |
case .Unknown: | |
// Should never happen | |
self.state.value = .Unknown | |
} | |
// Store last update timestamp | |
lastUpdated = Date() | |
// Update address if necessary | |
if showAddressDetailsBeforeUpdatingState != showAddressDetails() { | |
updateAddress() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment