mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 02:56:32 +01:00
200 lines
6.4 KiB
C#
200 lines
6.4 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using ShareeBike.Model.Connector;
|
|
using ShareeBike.MultilingualResources;
|
|
using ShareeBike.View;
|
|
using Serilog;
|
|
using CancelReservationCommand = ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.CancelReservationCommand;
|
|
using DisconnectCommand = ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.DisconnectCommand;
|
|
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
|
|
|
namespace ShareeBike.ViewModel.Bikes.Bike
|
|
{
|
|
/// <summary>
|
|
/// Return bike action.
|
|
/// </summary>
|
|
/// <typeparam name="T">Type of owner.</typeparam>
|
|
public class CancelReservationActionViewModel<T> :
|
|
CancelReservationCommand.ICancelReservationCommandListener,
|
|
DisconnectCommand.IDisconnectCommandListener
|
|
{
|
|
/// <summary>
|
|
/// View model to be used for progress report and unlocking/ locking view.
|
|
/// </summary>
|
|
private IBikesViewModel BikesViewModel { get; set; }
|
|
|
|
/// <summary>
|
|
/// View service to show modal notifications.
|
|
/// </summary>
|
|
private IViewService ViewService { get; }
|
|
|
|
/// <summary>Object to start or stop update of view model objects from Copri.</summary>
|
|
private Func<IPollingUpdateTaskManager> ViewUpdateManager { get; }
|
|
|
|
/// <summary> Bike close. </summary>
|
|
private IBikeInfoMutable SelectedBike { get; }
|
|
|
|
/// <summary> Provides a connector object.</summary>
|
|
protected Func<bool, IConnector> ConnectorFactory { get; }
|
|
|
|
/// <summary>
|
|
/// Constructs the object.
|
|
/// </summary>
|
|
/// <param name="selectedBike">Bike to close.</param>
|
|
/// <param name="viewUpdateManager">Object to start or stop update of view model objects from Copri.</param>
|
|
/// <param name="viewService">View service to show modal notifications.</param>
|
|
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
|
/// <exception cref="ArgumentException"></exception>
|
|
public CancelReservationActionViewModel(
|
|
IBikeInfoMutable selectedBike,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel)
|
|
{
|
|
SelectedBike = selectedBike;
|
|
ViewUpdateManager = viewUpdateManager;
|
|
ViewService = viewService;
|
|
BikesViewModel = bikesViewModel
|
|
?? throw new ArgumentException($"Can not construct {typeof(EndRentalActionViewModel<T>)}-object. {nameof(bikesViewModel)} must not be null.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the start reservation progress.
|
|
/// </summary>
|
|
/// <param name="step">Current step to process.</param>
|
|
public void ReportStep(CancelReservationCommand.Step step)
|
|
{
|
|
switch (step)
|
|
{
|
|
case CancelReservationCommand.Step.CancelReservation:
|
|
BikesViewModel.ActionText = AppResources.ActivityTextCancelingReservation;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the start reservation state.
|
|
/// </summary>
|
|
/// <param name="state">State to process.</param>
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
public async Task ReportStateAsync(CancelReservationCommand.State state, string details)
|
|
{
|
|
switch (state)
|
|
{
|
|
case CancelReservationCommand.State.InvalidResponse:
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCancelReservationTitle,
|
|
AppResources.ErrorAccountInvalidAuthorization,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case CancelReservationCommand.State.WebConnectFailed:
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorNoConnectionTitle,
|
|
AppResources.ErrorNoWeb,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case CancelReservationCommand.State.GeneralCancelReservationError:
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.ErrorCancelReservationTitle,
|
|
details,
|
|
AppResources.ErrorTryAgain,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the disconnect from lock progress.
|
|
/// </summary>
|
|
/// <param name="step">Current step to process.</param>
|
|
public void ReportStep(DisconnectCommand.Step step)
|
|
{
|
|
switch (step)
|
|
{
|
|
case DisconnectCommand.Step.DisconnectLock:
|
|
BikesViewModel.ActionText = AppResources.ActivityTextDisconnectingLock;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the disconnect from lock state.
|
|
/// </summary>
|
|
/// <param name="state">State to process.</param>
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
public async Task ReportStateAsync(DisconnectCommand.State state, string details)
|
|
{
|
|
switch (state)
|
|
{
|
|
|
|
case DisconnectCommand.State.GeneralDisconnectError:
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.ErrorAccountInvalidAuthorization,
|
|
details,
|
|
AppResources.ErrorTryAgain,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary> Cancel reservation. </summary>
|
|
public async Task CancelReservationAsync()
|
|
{
|
|
// lock GUI
|
|
BikesViewModel.IsIdle = false;
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopAsync();
|
|
|
|
// 1.Step: Cancel reservation
|
|
try
|
|
{
|
|
#if USELOCALINSTANCE
|
|
var command = new CancelReservationCommand(SelectedBike, ConnectorFactory, ViewUpdateManager);
|
|
await command.Invoke(this);
|
|
#else
|
|
await SelectedBike.CancelReservationAsync(this);
|
|
#endif
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<T>().Information("Reservation of bike {bikeId} could not be canceled. {@exception}", SelectedBike.Id, exception);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StartAsync();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return;
|
|
}
|
|
|
|
// 2. Step: Disconnect Lock
|
|
if (SelectedBike.LockInfo.State != LockingState.UnknownDisconnected)
|
|
{
|
|
try
|
|
{
|
|
#if USELOCALINSTANCE
|
|
var command = new DisconnectCommand(SelectedBike, ConnectorFactory, ViewUpdateManager);
|
|
await command.Invoke(this);
|
|
#else
|
|
await SelectedBike.DisconnectAsync(this);
|
|
#endif
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<T>().Information("Lock of bike {bikeId} could not be disconnected. {@exception}", SelectedBike.Id, exception);
|
|
}
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StartAsync();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return;
|
|
}
|
|
}
|
|
}
|