Exemple #1
0
        /// <summary>
        /// Make a reservation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public async void OnButtonClicked(object sender, EventArgs args)
        {
            TimeSlot timeSlot = new TimeSlot();

            timeSlot.startReservation   = DatePickerStart.Date + TimePickerStart.Time;
            timeSlot.endReservation     = DatePickerEnd.Date + TimePickerEnd.Time;
            timeSlot.licensePlateNumber = labelLicensePlate.Text;

            if (timeSlot.endReservation <= timeSlot.startReservation || timeSlot.startReservation <= DateTime.Now)
            {
                await DisplayAlert("Error", "Invalid time slot, please fill in the correct values", "Ok");

                return;
            }

            if (timeSlot.licensePlateNumber == null)
            {
                await DisplayAlert("Error", "Please fill in a correct license plate number", "Ok");

                return;
            }

            try
            {
                //confirm reservation
                string start   = String.Format("{0:dd/MM/yyyy - HH:mm}", timeSlot.startReservation);
                string end     = String.Format("{0:dd/MM/yyyy - HH:mm}", timeSlot.endReservation);
                bool   confirm = await DisplayAlert("Creating new reservation", "Starting: " + start + "\n Ending: " + end, "Yes", "No");

                if (confirm)
                {
                    //check for availability
                    int spots = await parkingSpotViewModel.GetFreeSpot(timeSlot);

                    if (spots > 0)
                    {
                        //navigate to payment page
                        PaymentPage paymentPage = new PaymentPage(accountViewModel);
                        await Navigation.PushModalAsync(paymentPage);

                        //await payment completion
                        await paymentPage.PageClosedTask;
                    }
                    else
                    {
                        await DisplayAlert("Error", "No more parking spots available.", "Ok");

                        return;
                    }

                    //payment success
                    if (PaymentInformation.pay)
                    {
                        timeSlot = await parkingSpotViewModel.Reservation(timeSlot);
                        await DisplayAlert("Success", "Reservation planned on: " + start + " untill: " + end, "Ok");
                    }
                    else
                    {
                        await DisplayAlert("Error", "Payment not succeeded.", "Ok");
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                await DisplayAlert("Error", "Could not find current user.", "Ok");
            }
            catch (TimeoutException)
            {
                await DisplayAlert("Connection error", "Please check your network settings.", "Ok");
            }
            catch (KeyNotFoundException)
            {
                await DisplayAlert("Error", "No more parking spots available at the suggested time", "Ok");
            }
            catch (Exception)
            {
                await DisplayAlert("Error", "An unexpected error occured. Please check if you have entered correct values in all fields.", "Ok");
            }
            finally
            {
                PaymentInformation.pay = false;
            }
        }