mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-23 23:56:26 +01:00
295 lines
14 KiB
C#
295 lines
14 KiB
C#
|
using System;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using TINK.Model.Connector;
|
|||
|
using TINK.Model.State;
|
|||
|
using TINK.View;
|
|||
|
using TINK.Services.Geolocation;
|
|||
|
using Serilog;
|
|||
|
using TINK.Repository.Exception;
|
|||
|
using TINK.MultilingualResources;
|
|||
|
using TINK.Model.User;
|
|||
|
using TINK.Model.Device;
|
|||
|
using TINK.Model;
|
|||
|
using TINK.Model.Bikes.Bike.CopriLock;
|
|||
|
using TINK.Services.CopriLock.Exception;
|
|||
|
|
|||
|
namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler
|
|||
|
{
|
|||
|
using IRequestHandler = BluetoothLock.IRequestHandler;
|
|||
|
|
|||
|
public class BookedClosed : Base, IRequestHandler
|
|||
|
{
|
|||
|
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
|
|||
|
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
|||
|
public BookedClosed(
|
|||
|
IBikeInfoMutable selectedBike,
|
|||
|
Func<bool> isConnectedDelegate,
|
|||
|
Func<bool, IConnector> connectorFactory,
|
|||
|
IGeolocation geolocation,
|
|||
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|||
|
ISmartDevice smartDevice,
|
|||
|
IViewService viewService,
|
|||
|
IBikesViewModel bikesViewModel,
|
|||
|
IUser activeUser) : base(
|
|||
|
selectedBike,
|
|||
|
AppResources.ActionReturn, // Copri button text "Miete beenden"
|
|||
|
true, // Show button to enabled returning of bike.
|
|||
|
isConnectedDelegate,
|
|||
|
connectorFactory,
|
|||
|
geolocation,
|
|||
|
viewUpdateManager,
|
|||
|
smartDevice,
|
|||
|
viewService,
|
|||
|
bikesViewModel,
|
|||
|
activeUser)
|
|||
|
{
|
|||
|
LockitButtonText = AppResources.ActionOpenAndPause;
|
|||
|
IsLockitButtonVisible = true; // Show button to enable opening lock in case user took a pause and does not want to return the bike.
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Gets the bike state. </summary>
|
|||
|
public override InUseStateEnum State => InUseStateEnum.Booked;
|
|||
|
|
|||
|
/// <summary> Return bike. </summary>
|
|||
|
public async Task<IRequestHandler> HandleRequestOption1() => await ReturnBike();
|
|||
|
|
|||
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|||
|
public async Task<IRequestHandler> HandleRequestOption2() => await OpenLock();
|
|||
|
|
|||
|
|
|||
|
/// <summary> Return bike. </summary>
|
|||
|
public async Task<IRequestHandler> ReturnBike()
|
|||
|
{
|
|||
|
BikesViewModel.IsIdle = false;
|
|||
|
|
|||
|
// Ask whether to really return bike?
|
|||
|
var l_oResult = await ViewService.DisplayAlert(
|
|||
|
string.Empty,
|
|||
|
string.Format(AppResources.QuestionReturnBike, SelectedBike.GetFullDisplayName()),
|
|||
|
AppResources.MessageAnswerYes,
|
|||
|
AppResources.MessageAnswerNo);
|
|||
|
|
|||
|
if (l_oResult == false)
|
|||
|
{
|
|||
|
// User aborted returning bike process
|
|||
|
Log.ForContext<BookedClosed>().Information("User selected booked bike {l_oId} in order to return but action was canceled.", SelectedBike.Id);
|
|||
|
|
|||
|
BikesViewModel.IsIdle = true;
|
|||
|
return this;
|
|||
|
}
|
|||
|
|
|||
|
// Lock list to avoid multiple taps while copri action is pending.
|
|||
|
Log.ForContext<BookedClosed>().Information("Request to return bike {bike} detected.", SelectedBike);
|
|||
|
|
|||
|
// Stop polling before returning bike.
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|||
|
await ViewUpdateManager().StopUpdatePeridically();
|
|||
|
|
|||
|
BikesViewModel.ActionText = "Returning bike...";
|
|||
|
IsConnected = IsConnectedDelegate();
|
|||
|
|
|||
|
var feedBackUri = SelectedBike?.OperatorUri;
|
|||
|
BookingFinishedModel bookingFinished;
|
|||
|
try
|
|||
|
{
|
|||
|
bookingFinished = await ConnectorFactory(IsConnected).Command.DoReturn(SelectedBike);
|
|||
|
|
|||
|
// If canceling bike succedes remove bike because it is not ready to be booked again
|
|||
|
IsRemoveBikeRequired = true;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
|
|||
|
if (exception is WebConnectFailureException)
|
|||
|
{
|
|||
|
// Copri server is not reachable.
|
|||
|
Log.ForContext<BookedClosed>().Information("User selected booked bike {bike} but returing failed (Copri server not reachable).", SelectedBike);
|
|||
|
|
|||
|
await ViewService.DisplayAdvancedAlert(
|
|||
|
AppResources.ErrorReturnBikeNoWebTitle,
|
|||
|
string.Format("{0}\r\n{1}", AppResources.ErrorReturnBikeNoWebMessage, WebConnectFailureException.GetHintToPossibleExceptionsReasons),
|
|||
|
exception.Message,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
}
|
|||
|
else if (exception is NotAtStationException notAtStationException)
|
|||
|
{
|
|||
|
// COPRI returned an error.
|
|||
|
Log.ForContext<BookedClosed>().Information("User selected booked bike {bike} but returning failed. COPRI returned an not at station error.", SelectedBike);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorReturnBikeTitle,
|
|||
|
string.Format(AppResources.ErrorReturnBikeNotAtStationMessage, notAtStationException.StationNr, notAtStationException.Distance),
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
}
|
|||
|
else if (exception is NoGPSDataException)
|
|||
|
{
|
|||
|
// COPRI returned an error.
|
|||
|
Log.ForContext<BookedClosed>().Information("User selected booked bike {bike} but returing failed. COPRI returned an no GPS- data error.", SelectedBike);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorReturnBikeTitle,
|
|||
|
string.Format(AppResources.ErrorReturnBikeLockClosedNoGPSMessage),
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
}
|
|||
|
else if (exception is ResponseException copriException)
|
|||
|
{
|
|||
|
// COPRI returned an error.
|
|||
|
Log.ForContext<BookedClosed>().Information("User selected booked bike {bike} but returing failed. COPRI returned an error.", SelectedBike);
|
|||
|
|
|||
|
await ViewService.DisplayAdvancedAlert(
|
|||
|
"Statusfehler beim Zurückgeben des Rads!",
|
|||
|
copriException.Message,
|
|||
|
copriException.Response,
|
|||
|
"OK");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Log.ForContext<BookedClosed>().Error("User selected booked bike {bike} but returning failed. {@l_oException}", SelectedBike.Id, exception);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorReturnBikeTitle,
|
|||
|
exception.Message,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
}
|
|||
|
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|||
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|||
|
BikesViewModel.ActionText = "";
|
|||
|
BikesViewModel.IsIdle = true;
|
|||
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|||
|
}
|
|||
|
|
|||
|
Log.ForContext<BookedClosed>().Information("User returned bike {bike} successfully.", SelectedBike);
|
|||
|
|
|||
|
#if !USERFEEDBACKDLG_OFF
|
|||
|
// Do get Feedback
|
|||
|
var feedback = await ViewService.DisplayUserFeedbackPopup(bookingFinished?.Co2Saving);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
await ConnectorFactory(IsConnected).Command.DoSubmitFeedback(
|
|||
|
new UserFeedbackDto { BikeId = SelectedBike.Id, IsBikeBroken = feedback.IsBikeBroken, Message = feedback.Message },
|
|||
|
feedBackUri);
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
|
|||
|
if (exception is ResponseException copriException)
|
|||
|
{
|
|||
|
// Copri server is not reachable.
|
|||
|
Log.ForContext<BookedClosed>().Information("Submitting feedback for bike {bike} failed. COPRI returned an error.", SelectedBike);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Log.ForContext<BookedClosed>().Error("Submitting feedback for bike {bike} failed. {@l_oException}", SelectedBike.Id, exception);
|
|||
|
}
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorReturnSubmitFeedbackTitle,
|
|||
|
AppResources.ErrorReturnSubmitFeedbackMessage,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
|
|||
|
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|||
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|||
|
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
BikesViewModel.IsIdle = true;
|
|||
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|||
|
}
|
|||
|
#endif
|
|||
|
if (bookingFinished != null && bookingFinished.MiniSurvey.Questions.Count > 0)
|
|||
|
{
|
|||
|
await ViewService.PushModalAsync(ViewTypes.MiniSurvey);
|
|||
|
}
|
|||
|
|
|||
|
// Restart polling again.
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|||
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
BikesViewModel.IsIdle = true;
|
|||
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|||
|
public async Task<IRequestHandler> OpenLock()
|
|||
|
{
|
|||
|
// Unlock bike.
|
|||
|
Log.ForContext<BookedClosed>().Information("User request to unlock bike {bike}.", SelectedBike);
|
|||
|
|
|||
|
// Stop polling before returning bike.
|
|||
|
BikesViewModel.IsIdle = false;
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|||
|
await ViewUpdateManager().StopUpdatePeridically();
|
|||
|
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
|
|||
|
try
|
|||
|
{
|
|||
|
await ConnectorFactory(IsConnected).Command.OpenLockAsync(SelectedBike);
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
|
|||
|
if (exception is OutOfReachException)
|
|||
|
{
|
|||
|
Log.ForContext<BookedClosed>().Debug("Lock can not be opened. {Exception}", exception);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorOpenLockTitle,
|
|||
|
AppResources.ErrorOpenLockOutOfReachMessage,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
}
|
|||
|
else if (exception is CouldntOpenBoldIsBlockedException)
|
|||
|
{
|
|||
|
Log.ForContext<BookedClosed>().Debug("Lock can not be opened. Bold is blocked. {Exception}", exception);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorOpenLockTitle,
|
|||
|
AppResources.ErrorOpenLockBoldBlockedMessage,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
}
|
|||
|
else if (exception is CouldntOpenBoldWasBlockedException)
|
|||
|
{
|
|||
|
Log.ForContext<BookedClosed>().Debug("Lock can not be opened. Bold was or is blocked. {Exception}", exception);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorOpenLockStillOpenTitle,
|
|||
|
AppResources.ErrorOpenLockBoldWasBlockedMessage,
|
|||
|
"OK");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Log.ForContext<BookedClosed>().Error("Lock can not be opened. {Exception}", exception);
|
|||
|
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.ErrorOpenLockTitle,
|
|||
|
exception.Message,
|
|||
|
"OK");
|
|||
|
}
|
|||
|
|
|||
|
// When bold is blocked lock is still closed even if exception occurres.
|
|||
|
// In all other cases state is supposed to be unknown. Example: Lock is out of reach and no more bluetooth connected.
|
|||
|
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
|
|||
|
? stateAwareException.State
|
|||
|
: LockingState.UnknownDisconnected;
|
|||
|
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|||
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
BikesViewModel.IsIdle = true;
|
|||
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|||
|
}
|
|||
|
|
|||
|
Log.ForContext<BookedClosed>().Information("User paused ride using {bike} successfully.", SelectedBike);
|
|||
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|||
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|||
|
BikesViewModel.ActionText = string.Empty;
|
|||
|
BikesViewModel.IsIdle = true;
|
|||
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|