mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 02:56:32 +01:00
224 lines
7.8 KiB
C#
224 lines
7.8 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Serilog;
|
|
using ShareeBike.MultilingualResources;
|
|
using ShareeBike.Services.Logging;
|
|
using ShareeBike.View;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.OpenCommand;
|
|
using System.ComponentModel;
|
|
|
|
namespace ShareeBike.ViewModel.Bikes.Bike.BluetoothLock
|
|
{
|
|
/// <summary>
|
|
/// View model for action open bluetooth lock.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
internal class OpenLockActionViewModel<T> : IOpenCommandListener
|
|
{
|
|
/// <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 open. </summary>
|
|
private Model.Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable SelectedBike { get; }
|
|
|
|
/// <summary>
|
|
/// Constructs the object.
|
|
/// </summary>
|
|
/// <param name="selectedBike">Bike to open.</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 OpenLockActionViewModel(
|
|
Model.Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable selectedBike,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel)
|
|
{
|
|
SelectedBike = selectedBike;
|
|
ViewUpdateManager = viewUpdateManager;
|
|
ViewService = viewService;
|
|
BikesViewModel = bikesViewModel
|
|
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the open lock progress.
|
|
/// </summary>
|
|
/// <param name="step">Current step to process.</param>
|
|
public void ReportStep(Step step)
|
|
{
|
|
switch (step)
|
|
{
|
|
case Step.OpeningLock:
|
|
// 1a.Step: Open lock
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
|
|
BikesViewModel.RentalProcess.StepInfoText = AppResources.MarkingRentalProcessOpenLockStepOpenLock;
|
|
BikesViewModel.RentalProcess.ImportantStepInfoText = AppResources.MarkingRentalProcessOpenLockObserve;
|
|
break;
|
|
|
|
case Step.GetLockInfos:
|
|
// 1b.Step: Get lock infos
|
|
BikesViewModel.RentalProcess.ImportantStepInfoText = AppResources.MarkingRentalProcessOpenLockWait;
|
|
break;
|
|
|
|
case Step.WaitStopPolling:
|
|
// 1c.Step: Wait for polling to be stopped
|
|
break;
|
|
|
|
case Step.UpdateLockingState:
|
|
// 1c.Step: Sent info to backend
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdatingLockingState;
|
|
BikesViewModel.RentalProcess.StepInfoText = AppResources.MarkingRentalProcessOpenLockStepUpload;
|
|
BikesViewModel.RentalProcess.ImportantStepInfoText = AppResources.MarkingRentalProcessOpenLockWait;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the open lock state.
|
|
/// </summary>
|
|
/// <param name="state">State to process.</param>
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
public async Task ReportStateAsync(State state, string details)
|
|
{
|
|
switch (state)
|
|
{
|
|
case State.OutOfReachError:
|
|
BikesViewModel.RentalProcess.Result = CurrentStepStatus.Failed;
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorLockOutOfReach,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case State.CouldntOpenBoldStatusIsUnknownError:
|
|
BikesViewModel.RentalProcess.Result = CurrentStepStatus.Failed;
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorOpenLockStatusUnknown,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case State.CouldntOpenBoldIsBlockedError:
|
|
BikesViewModel.RentalProcess.Result = CurrentStepStatus.Failed;
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorOpenLockBoldBlocked,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case State.CouldntOpenInconsistentStateError:
|
|
BikesViewModel.RentalProcess.Result = CurrentStepStatus.Failed;
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorOpenLockStillClosed,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case State.GeneralOpenError:
|
|
BikesViewModel.RentalProcess.Result = CurrentStepStatus.Failed;
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
details,
|
|
AppResources.MessageAnswerOk);
|
|
break;
|
|
|
|
case State.StopPollingFailed:
|
|
break;
|
|
|
|
case State.WebConnectFailed:
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorNoWebUpdateingLockstate;
|
|
break;
|
|
|
|
case State.ResponseIsInvalid:
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorStatusUpdateingLockstate;
|
|
break;
|
|
|
|
case State.BackendUpdateFailed:
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorConnectionUpdateingLockstate;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary> Open lock in order to pause ride and update COPRI lock state.</summary>
|
|
public async Task OpenLockAsync()
|
|
{
|
|
Log.ForContext<T>().Information("User request to open lock of bike {bikeId}.", SelectedBike.Id);
|
|
|
|
// lock GUI
|
|
BikesViewModel.IsIdle = false;
|
|
|
|
// Stop Updater
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
var stopPollingTask = ViewUpdateManager().StopAsync();
|
|
|
|
// Clear logging memory sink to avoid passing log data not related to returning of bike to backend.
|
|
// Log data is passed to backend when calling CopriCallsHttps.DoReturn().
|
|
MemoryStackSink.ClearMessages();
|
|
|
|
// 1. Step
|
|
// Parameter for RentalProcess View
|
|
BikesViewModel.StartRentalProcess(new RentalProcessViewModel(SelectedBike.Id)
|
|
{
|
|
State = CurrentRentalProcess.OpenLock,
|
|
StepIndex = 1,
|
|
Result = CurrentStepStatus.None
|
|
});
|
|
|
|
try
|
|
{
|
|
#if USELOCALINSTANCE
|
|
var command = new OpenCommand(SelectedBike, GeolocationService, LockService, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager);
|
|
await command.Invoke(this);
|
|
#else
|
|
await SelectedBike.OpenLockAsync(this, stopPollingTask);
|
|
#endif
|
|
Log.ForContext<T>().Information("Lock of bike {bikeId} opened successfully.", SelectedBike.Id);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<T>().Information("Lock of bike {bikeId} can not be opened. {@exception}", SelectedBike.Id, exception);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StartAsync();
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.RentalProcess.State = CurrentRentalProcess.None;
|
|
BikesViewModel.IsIdle = true;
|
|
return;
|
|
}
|
|
|
|
BikesViewModel.RentalProcess.Result = CurrentStepStatus.Succeeded;
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StartAsync();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
//Confirmation message
|
|
Log.ForContext<T>().Information("User request to park bike {bikeId}.", SelectedBike.Id);
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageRentalProcessOpenLockFinishedTitle,
|
|
AppResources.MessageRentalProcessOpenLockFinishedText,
|
|
AppResources.MessageAnswerOk);
|
|
|
|
BikesViewModel.RentalProcess.State = CurrentRentalProcess.None;
|
|
BikesViewModel.IsIdle = true;
|
|
return;
|
|
}
|
|
}
|
|
}
|