mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 18:46:30 +01:00
135 lines
6.3 KiB
C#
135 lines
6.3 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.Bikes.Bike.CopriLock;
|
|
using TINK.Model.User;
|
|
using TINK.Model.Device;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler
|
|
{
|
|
using IRequestHandler = BluetoothLock.IRequestHandler;
|
|
|
|
public class DisposableClosed : 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 DisposableClosed(
|
|
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.ActionOpenAndBook, // Button text: "Schloss öffnen & Rad mieten"
|
|
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 BookAndOpen();
|
|
|
|
public async Task<IRequestHandler> HandleRequestOption2() => await UnsupportedRequest();
|
|
|
|
/// <summary>Reserve bike and connect to lock.</summary>
|
|
public async Task<IRequestHandler> BookAndOpen()
|
|
{
|
|
BikesViewModel.IsIdle = false;
|
|
|
|
// Ask whether to really book bike?
|
|
var alertResult = await ViewService.DisplayAlert(
|
|
string.Empty,
|
|
string.Format(AppResources.QuestionOpenLockAndBookBike, SelectedBike.GetFullDisplayName()),
|
|
AppResources.MessageAnswerYes,
|
|
AppResources.MessageAnswerNo);
|
|
|
|
if (alertResult == false)
|
|
{
|
|
// User aborted booking process
|
|
Log.ForContext<DisposableClosed>().Information("User selected recently requested bike {bike} in order to reserve but did deny to book bike.", SelectedBike);
|
|
|
|
// Restart polling again.
|
|
BikesViewModel.IsIdle = true;
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
Log.ForContext<DisposableClosed>().Information("User selected recently requested bike {bike} in order to book.", SelectedBike);
|
|
|
|
// Book bike prior to opening lock.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextRentingBike;
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.BookAndOpenAync(SelectedBike);
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
if (l_oException is WebConnectFailureException)
|
|
{
|
|
// Copri server is not reachable.
|
|
Log.ForContext<DisposableClosed>().Information("User selected recently requested bike {l_oId} but booking failed (Copri server not reachable).", SelectedBike.Id);
|
|
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.MessageRentingBikeErrorConnectionTitle,
|
|
WebConnectFailureException.GetHintToPossibleExceptionsReasons,
|
|
l_oException.Message,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<DisposableClosed>().Error("User selected recently requested bike {l_oId} but reserving failed. {@l_oException}", SelectedBike.Id, l_oException);
|
|
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.MessageRentingBikeErrorGeneralTitle,
|
|
string.Empty,
|
|
l_oException.Message,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
Log.ForContext<DisposableClosed>().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<DisposableClosed>().Error("Click of unsupported button click detected.");
|
|
return await Task.FromResult<IRequestHandler>(this);
|
|
}
|
|
}
|
|
}
|