sharee.bike-App/TINKLib/ViewModel/Bikes/Bike/CopriLock/RequestHandlerFactory.cs

130 lines
3.8 KiB
C#
Raw Normal View History

2022-04-25 22:15:15 +02:00
using System;
2022-08-30 15:42:25 +02:00
using Serilog;
using TINK.Model.Bikes.BikeInfoNS.CopriLock;
2022-04-25 22:15:15 +02:00
using TINK.Model.Connector;
2022-08-30 15:42:25 +02:00
using TINK.Model.Device;
using TINK.Model.User;
2022-04-25 22:15:15 +02:00
using TINK.View;
using TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler;
namespace TINK.ViewModel.Bikes.Bike.CopriLock
{
2022-09-06 16:08:19 +02:00
public static class RequestHandlerFactory
{
/// <summary> Creates a request handler.</summary>
/// <param name="selectedBike"></param>
/// <param name="isConnectedDelegate"></param>
/// <param name="connectorFactory"></param>
/// <param name="bikeRemoveDelegate"></param>
/// <param name="viewUpdateManager"></param>
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
/// <param name="viewService"></param>
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
/// <returns>Request handler.</returns>
public static BluetoothLock.IRequestHandler Create(
Model.Bikes.BikeInfoNS.BC.IBikeInfoMutable selectedBike,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
Func<IPollingUpdateTaskManager> viewUpdateManager,
ISmartDevice smartDevice,
IViewService viewService,
IBikesViewModel bikesViewModel,
IUser activeUser)
{
if (!(selectedBike is IBikeInfoMutable selectedCopriLock))
{
Log.Error("Bike of unexpected type {type} detected.", selectedBike?.GetType());
return null;
}
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
switch (selectedCopriLock.State.Value)
{
// User has rented this bike before. Bike is now available but feedback was not yet given.
case Model.State.InUseStateEnum.FeedbackPending:
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
return new FeedbackPending(
selectedCopriLock,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
case Model.State.InUseStateEnum.Disposable:
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
// Bike is reserved, selecte action depending on lock state.
return new DisposableClosed(
selectedCopriLock,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
case Model.State.InUseStateEnum.Reserved:
// Lock could not be opened after reserving bike.
return new ReservedClosed(
selectedCopriLock,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
case Model.State.InUseStateEnum.Booked:
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
// Bike is booked, selecte action depending on lock state.
var lockState = selectedCopriLock.LockInfo.State;
switch (lockState)
{
case LockingState.Closed:
// Frame lock is closed. User paused ride and closed lock.
return new BookedClosed(
selectedCopriLock,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
default:
if (lockState != LockingState.Open)
Log.Error("Unexpected locking {LockingState} of a rented bike with copri lock bike detected.", selectedCopriLock.LockInfo.State);
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
return new BookedOpen(
selectedCopriLock,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
}
2022-04-25 22:15:15 +02:00
2022-09-06 16:08:19 +02:00
default:
// Unexpected copri state detected.
Log.Error("Unexpected locking {BookingState}/ {LockingState} of a copri lock bike detected.", selectedCopriLock.State.Value, selectedCopriLock.LockInfo.State);
return new DisposableClosed(
selectedCopriLock,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser);
}
}
}
2022-04-25 22:15:15 +02:00
}