sharee.bike-App/SharedBusinessLogic/ViewModel/Bikes/Bike/BluetoothLock/RequestHandler/DisposableDisconnected.cs

131 lines
4.3 KiB
C#
Raw Normal View History

2022-09-16 11:19:46 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Threading.Tasks;
using Serilog;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Device;
using ShareeBike.Model.User;
using ShareeBike.MultilingualResources;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.Geolocation;
using ShareeBike.View;
using StartReservationCommand = ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.StartReservationCommand;
using static ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandlerFactory;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
2021-05-13 20:03:07 +02:00
{
2024-04-09 12:53:23 +02:00
public class DisposableDisconnected : Base, IRequestHandler, StartReservationCommand.IStartReservationCommandListener
2022-09-06 16:08:19 +02:00
{
2024-04-09 12:53:23 +02:00
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
2022-09-06 16:08:19 +02:00
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
2024-04-09 12:53:23 +02:00
public DisposableDisconnected(
2022-09-06 16:08:19 +02:00
IBikeInfoMutable selectedBike,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
2023-04-05 15:02:10 +02:00
IGeolocationService geolocation,
2022-09-06 16:08:19 +02:00
ILocksService lockService,
Func<IPollingUpdateTaskManager> viewUpdateManager,
ISmartDevice smartDevice,
IViewService viewService,
IBikesViewModel bikesViewModel,
IUser activeUser) : base(
selectedBike,
2024-04-09 12:53:23 +02:00
AppResources.ActionRequestBike, // Reserve / Rent Bike
true, // Show button
2022-09-06 16:08:19 +02:00
isConnectedDelegate,
connectorFactory,
geolocation,
lockService,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser)
{
2024-04-09 12:53:23 +02:00
LockitButtonText = GetType().Name;
2023-08-31 12:20:06 +02:00
IsLockitButtonVisible = false;
2024-04-09 12:53:23 +02:00
_startReservationOrRentalActionViewModel = new StartReservationOrRentalActionViewModel<DisposableDisconnected>(
2023-08-31 12:20:06 +02:00
selectedBike,
viewUpdateManager,
viewService,
bikesViewModel);
2022-09-06 16:08:19 +02:00
}
2023-08-31 12:20:06 +02:00
/// <summary>
2024-04-09 12:53:23 +02:00
/// Holds the view model for requesting a bike action.
2023-08-31 12:20:06 +02:00
/// </summary>
2024-04-09 12:53:23 +02:00
private readonly StartReservationOrRentalActionViewModel<DisposableDisconnected> _startReservationOrRentalActionViewModel;
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
/// <summary>
/// Processes the reserving a bike progress.
/// </summary>
/// <remarks>
/// Only used for testing.
/// </remarks>
/// <param name="step">Current step to process.</param>
public void ReportStep(StartReservationCommand.Step step) => _startReservationOrRentalActionViewModel?.ReportStep(step);
/// <summary>
/// Processes the reserving a bike state.
/// </summary>
/// <remarks>
/// Only used for testing.
/// </remarks>
/// <param name="state">State to process.</param>
/// <param name="details">Textual details describing current state.</param>
public async Task ReportStateAsync(StartReservationCommand.State state, string details) => await _startReservationOrRentalActionViewModel.ReportStateAsync(state, details);
/// <summary>Reserve bike, connect to lock, open lock and rent bike.</summary>
2023-08-31 12:20:06 +02:00
public async Task<IRequestHandler> HandleRequestOption1()
2022-09-06 16:08:19 +02:00
{
2024-04-09 12:53:23 +02:00
await _startReservationOrRentalActionViewModel.StartReservationOrRentalAsync();
if (_startReservationOrRentalActionViewModel.ContinueWithOpenLock == false)
2022-09-06 16:08:19 +02:00
{
2023-08-31 12:20:06 +02:00
return Create(
2022-09-16 11:19:46 +02:00
SelectedBike,
2023-08-31 12:20:06 +02:00
IsConnectedDelegate,
ConnectorFactory,
GeolocationService,
LockService,
ViewUpdateManager,
SmartDevice,
ViewService,
BikesViewModel,
ActiveUser);
}
2024-04-09 12:53:23 +02:00
var _openLockActionViewModel = new OpenLockActionViewModel<BookedClosed>(
2023-08-31 12:20:06 +02:00
SelectedBike,
ViewUpdateManager,
ViewService,
BikesViewModel);
2024-04-09 12:53:23 +02:00
await _openLockActionViewModel.OpenLockAsync();
2023-08-31 12:20:06 +02:00
return Create(
SelectedBike,
IsConnectedDelegate,
ConnectorFactory,
GeolocationService,
LockService,
ViewUpdateManager,
SmartDevice,
ViewService,
BikesViewModel,
ActiveUser);
2022-09-06 16:08:19 +02:00
}
2024-04-09 12:53:23 +02:00
public async Task<IRequestHandler> HandleRequestOption2() => await UnsupportedRequest();
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
/// <summary> Request is not supported, button should be disabled. </summary>
/// <returns></returns>
public async Task<IRequestHandler> UnsupportedRequest()
{
Log.ForContext<DisposableDisconnected>().Error("Click of unsupported button click detected.");
return await Task.FromResult<IRequestHandler>(this);
}
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:03:07 +02:00
}