sharee.bike-App/TINKLib/ViewModel/Bikes/Bike/BC/BikeViewModel.cs
2021-06-26 20:57:55 +02:00

129 lines
5.1 KiB
C#

using System;
using System.ComponentModel;
using TINK.Model.Connector;
using TINK.Model.Device;
using TINK.Model.User;
using TINK.View;
using TINK.ViewModel.Bikes.Bike.BC.RequestHandler;
using BikeInfoMutable = TINK.Model.Bike.BC.BikeInfoMutable;
namespace TINK.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>
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) : base(isConnectedDelegate, connectorFactory, bikeRemoveDelegate, viewUpdateManager, smartDevice, viewService, selectedBike, activeUser, stateInfoProvider, bikesViewModel)
{
RequestHandler = activeUser.IsLoggedIn
? RequestHandlerFactory.Create(
selectedBike,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
ActiveUser)
: new NotLoggedIn(
selectedBike.State.Value,
viewService,
bikesViewModel,
ActiveUser);
}
/// <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 visiblity of the copri command button. </summary>
public bool IsButtonVisible => RequestHandler.IsButtonVisible;
/// <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;
RequestHandler = await RequestHandler.HandleRequest();
if (lastHandler.IsRemoveBikeRequired)
{
BikeRemoveDelegate(Id);
}
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)));
}
});
}
}