mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 02:56:32 +01:00
117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using ShareeBike.MultilingualResources;
|
|
using Serilog;
|
|
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 DisconnectLockActionViewModel<T> :
|
|
DisconnectCommand.IDisconnectCommandListener
|
|
{
|
|
/// <summary>
|
|
/// View model to be used for progress report and unlocking/ locking view.
|
|
/// </summary>
|
|
private IBikesViewModel BikesViewModel { get; set; }
|
|
|
|
/// <summary>Object to start or stop update of view model objects from Copri.</summary>
|
|
private Func<IPollingUpdateTaskManager> ViewUpdateManager { get; }
|
|
|
|
/// <summary> Bike </summary>
|
|
private IBikeInfoMutable SelectedBike { get; set; }
|
|
|
|
/// <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="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
|
/// <exception cref="ArgumentException"></exception>
|
|
public DisconnectLockActionViewModel(
|
|
IBikeInfoMutable selectedBike,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
IBikesViewModel bikesViewModel)
|
|
{
|
|
SelectedBike = selectedBike;
|
|
ViewUpdateManager = viewUpdateManager;
|
|
BikesViewModel = bikesViewModel
|
|
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Processes the disconnect 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 lock state.
|
|
/// </summary>
|
|
/// <param name="state">State to process.</param>
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
public Task ReportStateAsync(DisconnectCommand.State state, string details)
|
|
{
|
|
switch (state)
|
|
{
|
|
case DisconnectCommand.State.GeneralDisconnectError:
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorDisconnect;
|
|
break;
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary> Disconnect lock. </summary>
|
|
public async Task DisconnectLockAsync()
|
|
{
|
|
Log.ForContext<T>().Information("User request to end rental of bike {bikeId}.", SelectedBike.Id);
|
|
|
|
// lock GUI
|
|
BikesViewModel.IsIdle = false;
|
|
|
|
// Stop Updater
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopAsync();
|
|
|
|
try
|
|
{
|
|
#if USELOCALINSTANCE
|
|
var command = new DisonnectCommand(SelectedBike, LockService);
|
|
await command.Invoke(this);
|
|
#else
|
|
await SelectedBike.DisconnectAsync(this);
|
|
#endif
|
|
Log.ForContext<T>().Information("Lock of {bikeId} disconnected successfully.", SelectedBike.Id);
|
|
}
|
|
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;
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StartAsync();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|