mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 18:46:30 +01:00
148 lines
6.7 KiB
C#
148 lines
6.7 KiB
C#
using System;
|
|
using Serilog;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.State;
|
|
using TINK.View;
|
|
using TINK.Repository.Exception;
|
|
using TINK.Services.Geolocation;
|
|
using TINK.MultilingualResources;
|
|
using TINK.Model.User;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.Bikes.Bike.CopriLock;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler
|
|
{
|
|
using IRequestHandler = BluetoothLock.IRequestHandler;
|
|
|
|
public class DisposabledAway : 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 DisposabledAway(
|
|
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.ActionRequest, // Copri text: "Rad reservieren"
|
|
true, // Show copri button to enable reserving and opening
|
|
isConnectedDelegate,
|
|
connectorFactory,
|
|
geolocation,
|
|
viewUpdateManager,
|
|
smartDevice,
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser)
|
|
{
|
|
LockitButtonText = GetType().Name;
|
|
IsLockitButtonVisible = false; // If bike is not reserved/ booked app can not connect to lock
|
|
}
|
|
|
|
/// <summary> Gets the bike state. </summary>
|
|
public override InUseStateEnum State => InUseStateEnum.Disposable;
|
|
|
|
/// <summary>Reserve bike and connect to lock.</summary>
|
|
public async Task<IRequestHandler> HandleRequestOption1() => await Reserve();
|
|
|
|
public async Task<IRequestHandler> HandleRequestOption2() => await UnsupportedRequest();
|
|
|
|
/// <summary>Reserve bike and connect to lock.</summary>
|
|
public async Task<IRequestHandler> Reserve()
|
|
{
|
|
BikesViewModel.IsIdle = false;
|
|
|
|
// Ask whether to really reserve bike?
|
|
var alertResult = await ViewService.DisplayAlert(
|
|
string.Empty,
|
|
string.Format(AppResources.QuestionReserveBike, SelectedBike.GetFullDisplayName(), StateRequestedInfo.MaximumReserveTime.Minutes),
|
|
AppResources.MessageAnswerYes,
|
|
AppResources.MessageAnswerNo);
|
|
|
|
if (alertResult == false)
|
|
{
|
|
// User aborted booking process
|
|
Log.ForContext<DisposabledAway>().Information("User selected availalbe bike {bike} in order to reserve but action was canceled.", SelectedBike);
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
|
|
// Lock list to avoid multiple taps while copri action is pending.
|
|
Log.ForContext<DisposabledAway>().Information("Request to book and open lock for bike {bike} detected.", SelectedBike);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
|
|
// Stop polling before requesting bike.
|
|
await ViewUpdateManager().StopUpdatePeridically();
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextReservingBike;
|
|
IsConnected = IsConnectedDelegate();
|
|
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.DoReserve(SelectedBike);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
if (exception is BookingDeclinedException)
|
|
{
|
|
// Too many bikes booked.
|
|
Log.ForContext<DisposabledAway>().Information("Request declined because maximum count of bikes {l_oException.MaxBikesCount} already requested/ booked.", (exception as BookingDeclinedException).MaxBikesCount);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageTitleHint,
|
|
string.Format(AppResources.MessageReservationBikeErrorTooManyReservationsRentals, SelectedBike.Id, (exception as BookingDeclinedException).MaxBikesCount),
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is WebConnectFailureException)
|
|
{
|
|
// Copri server is not reachable.
|
|
Log.ForContext<DisposabledAway>().Information("User selected availalbe bike {bike} but reserving failed (Copri server not reachable).", SelectedBike);
|
|
|
|
await ViewService.DisplayAlert(
|
|
"Verbingungsfehler beim Reservieren des Rads!",
|
|
string.Format("{0}\r\n{1}", exception.Message, WebConnectFailureException.GetHintToPossibleExceptionsReasons),
|
|
"OK");
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<DisposabledAway>().Error("User selected availalbe bike {bike} but reserving failed. {@l_oException}", SelectedBike, exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Reservieren des Rads!",
|
|
exception.Message,
|
|
"OK");
|
|
}
|
|
|
|
// Restart polling again.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
|
|
Log.ForContext<DisposabledAway>().Information("User reserved bike {bike} successfully.", SelectedBike);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
/// <summary> Requst is not supported, button should be disabled. </summary>
|
|
/// <returns></returns>
|
|
public async Task<IRequestHandler> UnsupportedRequest()
|
|
{
|
|
Log.ForContext<DisposabledAway>().Error("Click of unsupported button click detected.");
|
|
return await Task.FromResult<IRequestHandler>(this);
|
|
}
|
|
}
|
|
}
|