sharee.bike-App/SharedBusinessLogic/ViewModel/Bikes/Bike/BC/RequestHandler/Reserved.cs
2024-04-09 12:53:23 +02:00

112 lines
4.3 KiB
C#

using System;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Device;
using ShareeBike.Model.User;
using ShareeBike.MultilingualResources;
using ShareeBike.Repository.Exception;
using ShareeBike.Services.CopriApi.Exception;
using ShareeBike.View;
using BikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
{
public class Reserved : Base<BikeInfoMutable>, 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 Reserved(
BikeInfoMutable selectedBike,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
Func<IPollingUpdateTaskManager> viewUpdateManager,
ISmartDevice smartDevice,
IViewService viewService,
IBikesViewModel bikesViewModel,
IUser activeUser) : base(selectedBike, AppResources.ActionCancelReservation, true, isConnectedDelegate, connectorFactory, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser)
{
}
/// <summary> Executes user request to cancel reservation. </summary>
public async Task<IRequestHandler> HandleRequest()
{
// Lock list to avoid multiple taps while copri action is pending.
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
BikesViewModel.IsIdle = false;
BikesViewModel.ActionText = string.Empty;
var l_oResult = await ViewService.DisplayAlert(
string.Empty,
string.Format("Reservierung für Fahrrad {0} aufheben?", SelectedBike.GetFullDisplayName()),
"Ja",
"Nein");
if (l_oResult == false)
{
// User aborted cancel process
Log.ForContext<Reserved>().Information("User selected reserved bike {l_oId} in order to cancel reservation but action was canceled.", SelectedBike.Id);
BikesViewModel.IsIdle = true;
return this;
}
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
// Stop polling before cancel request.
await ViewUpdateManager().StopAsync();
try
{
IsConnected = IsConnectedDelegate();
await ConnectorFactory(IsConnected).Command.DoCancelReservation(SelectedBike);
}
catch (Exception exception)
{
if (exception is InvalidAuthorizationResponseException)
{
// Copri response is invalid.
Log.ForContext<Reserved>().Error("User selected reserved bike {l_oId} but canceling reservation failed (Invalid auth. response).", SelectedBike.Id);
BikesViewModel.ActionText = String.Empty;
await ViewService.DisplayAlert("Fehler beim Stornieren der Buchung!", exception.Message, "OK");
BikesViewModel.IsIdle = true;
return this;
}
else if (exception is WebConnectFailureException
|| exception is RequestNotCachableException)
{
// Copri server is not reachable.
Log.ForContext<Reserved>().Information("User selected reserved bike {l_oId} but cancel reservation failed (Copri server not reachable).", SelectedBike.Id);
BikesViewModel.ActionText = String.Empty;
await ViewService.DisplayAlert(
"Verbingungsfehler beim Stornieren der Buchung!",
AppResources.ErrorNoWeb,
"OK");
BikesViewModel.IsIdle = true;
return this;
}
else
{
Log.ForContext<Reserved>().Error("User selected reserved bike {l_oId} but cancel reservation failed. {@l_oException}.", SelectedBike.Id, exception);
BikesViewModel.ActionText = String.Empty;
await ViewService.DisplayAlert("Fehler beim Stornieren der Buchung!", exception.Message, "OK");
BikesViewModel.IsIdle = true;
return this;
}
}
finally
{
// Restart polling again.
await ViewUpdateManager().StartAsync();
// Unlock list of bikes because no more action is pending.
BikesViewModel.ActionText = string.Empty; // Todo: Move this statement in front of finally block because in catch block ActionText is already set to empty.
}
Log.ForContext<Reserved>().Information("User canceled reservation of bike {l_oId} successfully.", SelectedBike.Id);
BikesViewModel.IsIdle = true;
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
}
}
}