mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 11:06:26 +01:00
119 lines
4.6 KiB
C#
119 lines
4.6 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Serilog;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.State;
|
|
using TINK.Model.User;
|
|
using TINK.MultilingualResources;
|
|
using TINK.Repository.Exception;
|
|
using TINK.Services.CopriApi.Exception;
|
|
using TINK.View;
|
|
using BikeInfoMutable = TINK.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BC.RequestHandler
|
|
{
|
|
/// <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 class Disposable : Base<BikeInfoMutable>, IRequestHandler
|
|
{
|
|
public Disposable(
|
|
BikeInfoMutable selectedBike,
|
|
Func<bool> isConnectedDelegate,
|
|
Func<bool, IConnector> connectorFactory,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel,
|
|
IUser activeUser) : base(selectedBike, selectedBike.State.Value.GetActionText(), true, isConnectedDelegate, connectorFactory, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser)
|
|
{
|
|
}
|
|
/// <summary> Request bike. </summary>
|
|
public async Task<IRequestHandler> HandleRequest()
|
|
{
|
|
// Lock list to avoid multiple taps while copri action is pending.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
BikesViewModel.IsIdle = false;
|
|
|
|
var l_oResult = await ViewService.DisplayAlert(
|
|
string.Empty,
|
|
string.Format(
|
|
AppResources.QuestionReserveBike,
|
|
SelectedBike.GetFullDisplayName(),
|
|
SelectedBike.TariffDescription?.MaxReservationTimeSpan.TotalMinutes ?? 0),
|
|
AppResources.MessageAnswerYes,
|
|
AppResources.MessageAnswerNo);
|
|
|
|
if (l_oResult == false)
|
|
{
|
|
// User aborted booking process
|
|
Log.ForContext<Disposable>().Information("User selected centered bike {l_oId} in order to reserve but action was canceled.", SelectedBike.Id);
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
|
|
// Stop polling before requesting bike.
|
|
await ViewUpdateManager().StopAsync();
|
|
|
|
IsConnected = IsConnectedDelegate();
|
|
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.DoReserve(SelectedBike);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
if (exception is BookingDeclinedException)
|
|
{
|
|
// Too many bikes booked.
|
|
Log.ForContext<Disposable>().Information("Request declined because maximum count of bikes {l_oException.MaxBikesCount} already requested/ booked.", (exception as BookingDeclinedException).MaxBikesCount);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageHintTitle,
|
|
string.Format(AppResources.ErrorReservingBikeTooManyReservationsRentals, SelectedBike.Id, (exception as BookingDeclinedException).MaxBikesCount),
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is WebConnectFailureException
|
|
|| exception is RequestNotCachableException)
|
|
{
|
|
// Copri server is not reachable.
|
|
Log.ForContext<Disposable>().Information("User selected centered bike {l_oId} but reserving failed (Copri server not reachable).", SelectedBike.Id);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorNoConnectionTitle,
|
|
AppResources.ErrorNoWeb,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<Disposable>().Error("User selected centered bike {l_oId} but reserving failed. {@l_oException}", SelectedBike.Id, exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(AppResources.ErrorReservingBikeTitle, exception.Message, AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
BikesViewModel.ActionText = string.Empty; // Todo: Remove this statement because in catch block ActionText is already set to empty above.
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
|
|
finally
|
|
{
|
|
// Restart polling again.
|
|
await ViewUpdateManager().StartAsync();
|
|
|
|
// Update status text and 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.
|
|
BikesViewModel.IsIdle = true;
|
|
}
|
|
|
|
Log.ForContext<Disposable>().Information("User reserved bike {l_oId} successfully.", SelectedBike.Id);
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
}
|
|
}
|