mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-06-22 05:47:28 +02:00
Version 3.0.381
This commit is contained in:
parent
f963c0a219
commit
3a363acf3a
1525 changed files with 60589 additions and 125098 deletions
133
SharedBusinessLogic/ViewModel/Bikes/Bike/BC/BikeViewModel.cs
Normal file
133
SharedBusinessLogic/ViewModel/Bikes/Bike/BC/BikeViewModel.cs
Normal file
|
@ -0,0 +1,133 @@
|
|||
using System;
|
||||
using System.ComponentModel;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.View;
|
||||
using ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler;
|
||||
using BikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC
|
||||
{
|
||||
/// <summary>
|
||||
/// View model for a BC bike.
|
||||
/// Provides functionality for views
|
||||
/// - MyBikes
|
||||
/// - BikesAtStation
|
||||
/// </summary>
|
||||
public class BikeViewModel : BikeViewModelBase, INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// Notifies GUI about changes.
|
||||
/// </summary>
|
||||
public override event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
/// <summary> Holds object which manages requests. </summary>
|
||||
private IRequestHandler RequestHandler { get; set; }
|
||||
|
||||
/// <summary> Raises events in order to update GUI.</summary>
|
||||
public override void RaisePropertyChanged(object sender, PropertyChangedEventArgs eventArgs) => PropertyChanged?.Invoke(sender, eventArgs);
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a bike view model object.
|
||||
/// </summary>
|
||||
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
|
||||
/// <param name="selectedBike">Bike to be displayed.</param>
|
||||
/// <param name="activeUser">Object holding logged in user or an empty user object.</param>
|
||||
/// <param name="stateInfoProvider">Provides in use state information.</param>
|
||||
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
||||
/// <param name="openUrlInBrowser">Delegate to open browser.</param>
|
||||
public BikeViewModel(
|
||||
Func<bool> isConnectedDelegate,
|
||||
Func<bool, IConnector> connectorFactory,
|
||||
Action<string> bikeRemoveDelegate,
|
||||
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
||||
ISmartDevice smartDevice,
|
||||
IViewService viewService,
|
||||
BikeInfoMutable selectedBike,
|
||||
IUser activeUser,
|
||||
IInUseStateInfoProvider stateInfoProvider,
|
||||
IBikesViewModel bikesViewModel,
|
||||
Action<string> openUrlInBrowser) : base(isConnectedDelegate, connectorFactory, bikeRemoveDelegate, viewUpdateManager, smartDevice, viewService, selectedBike, activeUser, new ViewContext(PageContext.BikesAtStation), stateInfoProvider, bikesViewModel, openUrlInBrowser)
|
||||
{
|
||||
RequestHandler = activeUser.IsLoggedIn
|
||||
? RequestHandlerFactory.Create(
|
||||
selectedBike,
|
||||
isConnectedDelegate,
|
||||
connectorFactory,
|
||||
viewUpdateManager,
|
||||
smartDevice,
|
||||
viewService,
|
||||
bikesViewModel,
|
||||
ActiveUser)
|
||||
: new NotLoggedIn(
|
||||
selectedBike.State.Value,
|
||||
viewService,
|
||||
bikesViewModel,
|
||||
ActiveUser);
|
||||
|
||||
selectedBike.PropertyChanged += (sender, ev) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles BikeInfoMutable events.
|
||||
/// Helper member to raise events. Maps model event change notification to view model events.
|
||||
/// Todo: Check which events are received here and filter, to avoid event storm.
|
||||
/// </summary>
|
||||
/// <param name="p_strNameOfProp"></param>
|
||||
public override void OnSelectedBikeStateChanged()
|
||||
{
|
||||
RequestHandler = RequestHandlerFactory.Create(
|
||||
Bike,
|
||||
IsConnectedDelegate,
|
||||
ConnectorFactory,
|
||||
ViewUpdateManager,
|
||||
SmartDevice,
|
||||
ViewService,
|
||||
BikesViewModel,
|
||||
ActiveUser);
|
||||
|
||||
var handler = PropertyChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new PropertyChangedEventArgs(nameof(ButtonText)));
|
||||
handler(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Gets visibility of the copri command button. </summary>
|
||||
public bool IsButtonVisible
|
||||
=> RequestHandler.IsButtonVisible
|
||||
&& Bike.DataSource == Model.Bikes.BikeInfoNS.BC.DataSource.Copri /* do not show button if data is from cache */ ;
|
||||
|
||||
/// <summary> Gets the text of the copri command button. </summary>
|
||||
public string ButtonText => RequestHandler.ButtonText;
|
||||
|
||||
/// <summary> Processes request to perform a copri action (reserve bike and cancel reservation). </summary>
|
||||
public System.Windows.Input.ICommand OnButtonClicked => new Xamarin.Forms.Command(async () =>
|
||||
{
|
||||
var lastHandler = RequestHandler;
|
||||
var lastState = Bike.State.Value;
|
||||
RequestHandler = await RequestHandler.HandleRequest();
|
||||
|
||||
CheckRemoveBike(Id, lastState);
|
||||
|
||||
if (lastHandler.GetType() == RequestHandler.GetType())
|
||||
{
|
||||
// No state change occurred.
|
||||
return;
|
||||
}
|
||||
|
||||
// State changed and instance of request handler was switched.
|
||||
if (lastHandler.ButtonText != ButtonText)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ButtonText)));
|
||||
}
|
||||
|
||||
if (lastHandler.IsButtonVisible != IsButtonVisible)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.View;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
|
||||
{
|
||||
public abstract class Base<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// View model to be used by subclasses for progress report and unlocking/ locking view.
|
||||
/// </summary>
|
||||
public IBikesViewModel BikesViewModel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the button to reserve bike is visible or not.
|
||||
/// </summary>
|
||||
public bool IsButtonVisible { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the button when bike is disposable.
|
||||
/// </summary>
|
||||
public string ButtonText { get; }
|
||||
|
||||
/// <summary> Provides info about the smart device (phone, tablet, ...).</summary>
|
||||
protected ISmartDevice SmartDevice;
|
||||
|
||||
/// <summary>
|
||||
/// Reference on view service to show modal notifications and to perform navigation.
|
||||
/// </summary>
|
||||
protected IViewService ViewService { get; }
|
||||
|
||||
/// <summary> Provides a connector object.</summary>
|
||||
protected Func<bool, IConnector> ConnectorFactory { get; }
|
||||
|
||||
/// <summary> Delegate to retrieve connected state. </summary>
|
||||
protected Func<bool> IsConnectedDelegate { get; }
|
||||
|
||||
/// <summary> Object to manage update of view model objects from Copri.</summary>
|
||||
protected Func<IPollingUpdateTaskManager> ViewUpdateManager { get; }
|
||||
|
||||
/// <summary> Reference on the user </summary>
|
||||
protected IUser ActiveUser { get; }
|
||||
|
||||
/// <summary> Bike to display. </summary>
|
||||
protected T SelectedBike { get; }
|
||||
|
||||
/// <summary>Holds the is connected state. </summary>
|
||||
private bool isConnected;
|
||||
|
||||
/// <summary>Gets the is connected state. </summary>
|
||||
public bool IsConnected
|
||||
{
|
||||
get => isConnected;
|
||||
set
|
||||
{
|
||||
if (value == isConnected)
|
||||
return;
|
||||
|
||||
isConnected = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs the request handler base.
|
||||
/// </summary>
|
||||
/// <param name="selectedBike">Bike which is reserved or for which reservation is canceled.</param>
|
||||
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
|
||||
/// <param name="bikesViewModel">View model to be used by subclasses for progress report and unlocking/ locking view.</param>
|
||||
public Base(
|
||||
T selectedBike,
|
||||
string buttonText,
|
||||
bool isCopriButtonVisible,
|
||||
Func<bool> isConnectedDelegate,
|
||||
Func<bool, IConnector> connectorFactory,
|
||||
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
||||
ISmartDevice smartDevice,
|
||||
IViewService viewService,
|
||||
IBikesViewModel bikesViewModel,
|
||||
IUser activeUser)
|
||||
{
|
||||
ButtonText = buttonText;
|
||||
IsButtonVisible = isCopriButtonVisible;
|
||||
SelectedBike = selectedBike;
|
||||
IsConnectedDelegate = isConnectedDelegate;
|
||||
ConnectorFactory = connectorFactory;
|
||||
ViewUpdateManager = viewUpdateManager;
|
||||
SmartDevice = smartDevice;
|
||||
ViewService = viewService;
|
||||
ActiveUser = activeUser;
|
||||
BikesViewModel = bikesViewModel
|
||||
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Serilog;
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BC;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.MultilingualResources;
|
||||
using ShareeBike.View;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
|
||||
{
|
||||
public class Booked : IRequestHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// If a bike is booked unbooking can not be done by though app.
|
||||
/// </summary>
|
||||
public bool IsButtonVisible => false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the button when bike is cancel reservation.
|
||||
/// </summary>
|
||||
public string ButtonText => AppResources.ActionEndRental; // "Miete beenden"
|
||||
|
||||
/// <summary>
|
||||
/// Reference on view service to show modal notifications and to perform navigation.
|
||||
/// </summary>
|
||||
protected IViewService ViewService { get; }
|
||||
|
||||
/// <summary>View model to be used for progress report and unlocking/ locking view.</summary>
|
||||
public IBikesViewModel BikesViewModel { get; }
|
||||
|
||||
/// <summary> Executes user request to cancel reservation. </summary>
|
||||
public async Task<IRequestHandler> HandleRequest()
|
||||
{
|
||||
// Lock list to avoid multiple taps while copri action is pending.
|
||||
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
||||
BikesViewModel.IsIdle = false;
|
||||
|
||||
Log.ForContext<BikesViewModel>().Error("User selected booked bike {l_oId}.", SelectedBike.Id);
|
||||
|
||||
BikesViewModel.ActionText = string.Empty;
|
||||
await ViewService.DisplayAlert(
|
||||
string.Empty,
|
||||
"Rückgabe nur über Bordcomputer des Fahrrads durch Drücken der Taste 2 möglich!",
|
||||
"Ok");
|
||||
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bike to display.
|
||||
/// </summary>
|
||||
protected IBikeInfoMutable SelectedBike { get; }
|
||||
|
||||
/// <summary>Gets the is connected state. </summary>
|
||||
public bool IsConnected { get; set; }
|
||||
|
||||
/// <summary> Gets if the bike has to be removed after action has been completed. </summary>
|
||||
public bool IsRemoveBikeRequired => false;
|
||||
|
||||
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
||||
public Booked(
|
||||
IBikeInfoMutable selectedBike,
|
||||
IViewService viewService,
|
||||
IBikesViewModel bikesViewModel,
|
||||
IUser activeUser)
|
||||
{
|
||||
SelectedBike = selectedBike;
|
||||
ViewService = viewService;
|
||||
BikesViewModel = bikesViewModel
|
||||
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Serilog;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Model.State;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.MultilingualResources;
|
||||
using ShareeBike.Repository.Exception;
|
||||
using ShareeBike.Services.CopriApi.Exception;
|
||||
using ShareeBike.View;
|
||||
using BikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
|
||||
{
|
||||
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
|
||||
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
||||
public class Disposable : Base<BikeInfoMutable>, IRequestHandler
|
||||
{
|
||||
public Disposable(
|
||||
BikeInfoMutable selectedBike,
|
||||
Func<bool> isConnectedDelegate,
|
||||
Func<bool, IConnector> connectorFactory,
|
||||
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
||||
ISmartDevice smartDevice,
|
||||
IViewService viewService,
|
||||
IBikesViewModel bikesViewModel,
|
||||
IUser activeUser) : base(selectedBike, selectedBike.State.Value.GetActionText(), true, isConnectedDelegate, connectorFactory, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser)
|
||||
{
|
||||
}
|
||||
/// <summary> Request bike. </summary>
|
||||
public async Task<IRequestHandler> HandleRequest()
|
||||
{
|
||||
// Lock list to avoid multiple taps while copri action is pending.
|
||||
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
||||
BikesViewModel.IsIdle = false;
|
||||
|
||||
var l_oResult = await ViewService.DisplayAlert(
|
||||
string.Empty,
|
||||
string.Format(
|
||||
AppResources.QuestionReserveBike,
|
||||
SelectedBike.GetFullDisplayName(),
|
||||
SelectedBike.TariffDescription?.MaxReservationTimeSpan.TotalMinutes ?? 0),
|
||||
AppResources.MessageAnswerYes,
|
||||
AppResources.MessageAnswerNo);
|
||||
|
||||
if (l_oResult == false)
|
||||
{
|
||||
// User aborted booking process
|
||||
Log.ForContext<Disposable>().Information("User selected centered bike {l_oId} in order to reserve but action was canceled.", SelectedBike.Id);
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
||||
|
||||
// Stop polling before requesting bike.
|
||||
await ViewUpdateManager().StopAsync();
|
||||
|
||||
IsConnected = IsConnectedDelegate();
|
||||
|
||||
try
|
||||
{
|
||||
await ConnectorFactory(IsConnected).Command.DoReserve(SelectedBike);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
if (exception is BookingDeclinedException)
|
||||
{
|
||||
// Too many bikes booked.
|
||||
Log.ForContext<Disposable>().Information("Request declined because maximum count of bikes {l_oException.MaxBikesCount} already requested/ booked.", (exception as BookingDeclinedException).MaxBikesCount);
|
||||
|
||||
BikesViewModel.ActionText = string.Empty;
|
||||
await ViewService.DisplayAlert(
|
||||
AppResources.MessageHintTitle,
|
||||
string.Format(AppResources.ErrorReservingBikeTooManyReservationsRentals, SelectedBike.Id, (exception as BookingDeclinedException).MaxBikesCount),
|
||||
AppResources.MessageAnswerOk);
|
||||
}
|
||||
else if (exception is WebConnectFailureException
|
||||
|| exception is RequestNotCachableException)
|
||||
{
|
||||
// Copri server is not reachable.
|
||||
Log.ForContext<Disposable>().Information("User selected centered bike {l_oId} but reserving failed (Copri server not reachable).", SelectedBike.Id);
|
||||
|
||||
BikesViewModel.ActionText = string.Empty;
|
||||
await ViewService.DisplayAlert(
|
||||
AppResources.ErrorNoConnectionTitle,
|
||||
AppResources.ErrorNoWeb,
|
||||
AppResources.MessageAnswerOk);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.ForContext<Disposable>().Error("User selected centered bike {l_oId} but reserving failed. {@l_oException}", SelectedBike.Id, exception);
|
||||
|
||||
BikesViewModel.ActionText = string.Empty;
|
||||
await ViewService.DisplayAlert(AppResources.ErrorReservingBikeTitle, exception.Message, AppResources.MessageAnswerOk);
|
||||
}
|
||||
|
||||
BikesViewModel.ActionText = string.Empty; // Todo: Remove this statement because in catch block ActionText is already set to empty above.
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
// Restart polling again.
|
||||
await ViewUpdateManager().StartAsync();
|
||||
|
||||
// Update status text and unlock list of bikes because no more action is pending.
|
||||
BikesViewModel.ActionText = string.Empty; // Todo: Move this statement in front of finally block because in catch block ActionText is already set to empty.
|
||||
BikesViewModel.IsIdle = true;
|
||||
}
|
||||
|
||||
Log.ForContext<Disposable>().Information("User reserved bike {l_oId} successfully.", SelectedBike.Id);
|
||||
|
||||
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
|
||||
{
|
||||
public interface IRequestHandler : IRequestHandlerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs the copri action to be executed when user presses the copri button managed by request handler.
|
||||
/// </summary>
|
||||
/// <returns>New handler object if action suceesed, same handler otherwise.</returns>
|
||||
Task<IRequestHandler> HandleRequest();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Serilog;
|
||||
using ShareeBike.Model.State;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.View;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
|
||||
{
|
||||
public class NotLoggedIn : IRequestHandler
|
||||
{
|
||||
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
||||
public NotLoggedIn(
|
||||
InUseStateEnum state,
|
||||
IViewService viewService,
|
||||
IBikesViewModel bikesViewModel,
|
||||
IUser activeUser)
|
||||
{
|
||||
ButtonText = state.GetActionText();
|
||||
IsIdle = true;
|
||||
ViewService = viewService;
|
||||
BikesViewModel = bikesViewModel
|
||||
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
|
||||
}
|
||||
|
||||
public bool IsButtonVisible => true;
|
||||
|
||||
public bool IsIdle { get; private set; }
|
||||
|
||||
public string ButtonText { get; private set; }
|
||||
|
||||
public string ActionText { get => BikesViewModel.ActionText; private set => BikesViewModel.ActionText = value; }
|
||||
|
||||
/// <summary>
|
||||
/// Reference on view service to show modal notifications and to perform navigation.
|
||||
/// </summary>
|
||||
protected IViewService ViewService { get; }
|
||||
|
||||
/// <summary>View model to be used for progress report and unlocking/ locking view.</summary>
|
||||
public IBikesViewModel BikesViewModel { get; }
|
||||
|
||||
public bool IsConnected => throw new NotImplementedException();
|
||||
|
||||
/// <summary> Gets if the bike has to be removed after action has been completed. </summary>
|
||||
public bool IsRemoveBikeRequired => false;
|
||||
|
||||
public async Task<IRequestHandler> HandleRequest()
|
||||
{
|
||||
Log.ForContext<NotLoggedIn>().Information("User selected bike but is not logged in.");
|
||||
|
||||
// User is not logged in
|
||||
ActionText = string.Empty;
|
||||
var l_oResult = await ViewService.DisplayAlert(
|
||||
"Hinweis",
|
||||
"Bitte anmelden vor Reservierung eines Fahrrads!\r\nAuf Anmeldeseite wechseln?",
|
||||
"Ja",
|
||||
"Nein");
|
||||
|
||||
if (l_oResult == false)
|
||||
{
|
||||
// User aborted booking process
|
||||
IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Switch to login page
|
||||
await ViewService.ShowPage("//LoginPage");
|
||||
|
||||
}
|
||||
catch (Exception p_oException)
|
||||
{
|
||||
Log.ForContext<BikesViewModel>().Error("Ein unerwarteter Fehler ist in der Klasse NotLoggedIn aufgetreten. Kontext: Aufruf nach Reservierungsversuch ohne Anmeldung. {@Exception}", p_oException);
|
||||
IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Serilog;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.MultilingualResources;
|
||||
using ShareeBike.Repository.Exception;
|
||||
using ShareeBike.Services.CopriApi.Exception;
|
||||
using ShareeBike.View;
|
||||
using BikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler
|
||||
{
|
||||
public class Reserved : Base<BikeInfoMutable>, IRequestHandler
|
||||
{
|
||||
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
|
||||
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
||||
public Reserved(
|
||||
BikeInfoMutable selectedBike,
|
||||
Func<bool> isConnectedDelegate,
|
||||
Func<bool, IConnector> connectorFactory,
|
||||
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
||||
ISmartDevice smartDevice,
|
||||
IViewService viewService,
|
||||
IBikesViewModel bikesViewModel,
|
||||
IUser activeUser) : base(selectedBike, AppResources.ActionCancelReservation, true, isConnectedDelegate, connectorFactory, viewUpdateManager, smartDevice, viewService, bikesViewModel, activeUser)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary> Executes user request to cancel reservation. </summary>
|
||||
public async Task<IRequestHandler> HandleRequest()
|
||||
{
|
||||
// Lock list to avoid multiple taps while copri action is pending.
|
||||
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
||||
BikesViewModel.IsIdle = false;
|
||||
|
||||
BikesViewModel.ActionText = string.Empty;
|
||||
var l_oResult = await ViewService.DisplayAlert(
|
||||
string.Empty,
|
||||
string.Format("Reservierung für Fahrrad {0} aufheben?", SelectedBike.GetFullDisplayName()),
|
||||
"Ja",
|
||||
"Nein");
|
||||
|
||||
if (l_oResult == false)
|
||||
{
|
||||
// User aborted cancel process
|
||||
Log.ForContext<Reserved>().Information("User selected reserved bike {l_oId} in order to cancel reservation but action was canceled.", SelectedBike.Id);
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
||||
|
||||
// Stop polling before cancel request.
|
||||
await ViewUpdateManager().StopAsync();
|
||||
|
||||
try
|
||||
{
|
||||
IsConnected = IsConnectedDelegate();
|
||||
|
||||
await ConnectorFactory(IsConnected).Command.DoCancelReservation(SelectedBike);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
if (exception is InvalidAuthorizationResponseException)
|
||||
{
|
||||
// Copri response is invalid.
|
||||
Log.ForContext<Reserved>().Error("User selected reserved bike {l_oId} but canceling reservation failed (Invalid auth. response).", SelectedBike.Id);
|
||||
BikesViewModel.ActionText = String.Empty;
|
||||
await ViewService.DisplayAlert("Fehler beim Stornieren der Buchung!", exception.Message, "OK");
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
else if (exception is WebConnectFailureException
|
||||
|| exception is RequestNotCachableException)
|
||||
{
|
||||
// Copri server is not reachable.
|
||||
Log.ForContext<Reserved>().Information("User selected reserved bike {l_oId} but cancel reservation failed (Copri server not reachable).", SelectedBike.Id);
|
||||
BikesViewModel.ActionText = String.Empty;
|
||||
await ViewService.DisplayAlert(
|
||||
"Verbingungsfehler beim Stornieren der Buchung!",
|
||||
AppResources.ErrorNoWeb,
|
||||
"OK");
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.ForContext<Reserved>().Error("User selected reserved bike {l_oId} but cancel reservation failed. {@l_oException}.", SelectedBike.Id, exception);
|
||||
BikesViewModel.ActionText = String.Empty;
|
||||
await ViewService.DisplayAlert("Fehler beim Stornieren der Buchung!", exception.Message, "OK");
|
||||
BikesViewModel.IsIdle = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Restart polling again.
|
||||
await ViewUpdateManager().StartAsync();
|
||||
|
||||
// Unlock list of bikes because no more action is pending.
|
||||
BikesViewModel.ActionText = string.Empty; // Todo: Move this statement in front of finally block because in catch block ActionText is already set to empty.
|
||||
}
|
||||
|
||||
Log.ForContext<Reserved>().Information("User canceled reservation of bike {l_oId} successfully.", SelectedBike.Id);
|
||||
|
||||
BikesViewModel.IsIdle = true;
|
||||
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using ShareeBike.Model.Connector;
|
||||
using ShareeBike.Model.Device;
|
||||
using ShareeBike.Model.User;
|
||||
using ShareeBike.View;
|
||||
using ShareeBike.ViewModel.Bikes.Bike.BC.RequestHandler;
|
||||
using BikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC
|
||||
{
|
||||
public static class RequestHandlerFactory
|
||||
{
|
||||
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
|
||||
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
||||
public static IRequestHandler Create(
|
||||
BikeInfoMutable selectedBike,
|
||||
Func<bool> isConnectedDelegate,
|
||||
Func<bool, IConnector> connectorFactory,
|
||||
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
||||
ISmartDevice smartDevice,
|
||||
IViewService viewService,
|
||||
IBikesViewModel bikesViewModel,
|
||||
IUser activeUser)
|
||||
{
|
||||
switch (selectedBike.State.Value)
|
||||
{
|
||||
case Model.State.InUseStateEnum.Disposable:
|
||||
// Bike can be booked.
|
||||
return new Disposable(
|
||||
selectedBike,
|
||||
isConnectedDelegate,
|
||||
connectorFactory,
|
||||
viewUpdateManager,
|
||||
smartDevice,
|
||||
viewService,
|
||||
bikesViewModel,
|
||||
activeUser);
|
||||
|
||||
case Model.State.InUseStateEnum.Reserved:
|
||||
// Reservation can be canceled.
|
||||
return new Reserved(
|
||||
selectedBike,
|
||||
isConnectedDelegate,
|
||||
connectorFactory,
|
||||
viewUpdateManager,
|
||||
smartDevice,
|
||||
viewService,
|
||||
bikesViewModel,
|
||||
activeUser);
|
||||
|
||||
default:
|
||||
// No action using app possible.
|
||||
return new Booked(
|
||||
selectedBike,
|
||||
viewService,
|
||||
bikesViewModel,
|
||||
activeUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
26
SharedBusinessLogic/ViewModel/Bikes/Bike/BC/StateToText.cs
Normal file
26
SharedBusinessLogic/ViewModel/Bikes/Bike/BC/StateToText.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using ShareeBike.Model.State;
|
||||
|
||||
namespace ShareeBike.ViewModel.Bikes.Bike.BC
|
||||
{
|
||||
public static class StateToText
|
||||
{
|
||||
/// <summary> Get button text for given copri state. </summary>
|
||||
public static string GetActionText(this InUseStateEnum state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case InUseStateEnum.Disposable:
|
||||
return "Rad reservieren";
|
||||
|
||||
case InUseStateEnum.Reserved:
|
||||
return "Reservierung aufheben";
|
||||
|
||||
case InUseStateEnum.Booked:
|
||||
return "Miete beenden";
|
||||
|
||||
default:
|
||||
return $"{state}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue