sharee.bike-App/TINKLib/ViewModel/Bikes/Bike/BC/BikeViewModel.cs

136 lines
4.6 KiB
C#
Raw Normal View History

2023-01-18 14:22:51 +01:00
using System;
2021-05-13 20:03:07 +02:00
using System.ComponentModel;
using TINK.Model.Connector;
2021-06-26 20:57:55 +02:00
using TINK.Model.Device;
2021-05-13 20:03:07 +02:00
using TINK.Model.User;
using TINK.View;
using TINK.ViewModel.Bikes.Bike.BC.RequestHandler;
2022-08-30 15:42:25 +02:00
using BikeInfoMutable = TINK.Model.Bikes.BikeInfoNS.BC.BikeInfoMutable;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Bikes.Bike.BC
{
2022-09-06 16:08:19 +02:00
/// <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;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds object which manages requests. </summary>
private IRequestHandler RequestHandler { get; set; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Raises events in order to update GUI.</summary>
public override void RaisePropertyChanged(object sender, PropertyChangedEventArgs eventArgs) => PropertyChanged?.Invoke(sender, eventArgs);
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <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, stateInfoProvider, bikesViewModel, openUrlInBrowser)
{
RequestHandler = activeUser.IsLoggedIn
? RequestHandlerFactory.Create(
selectedBike,
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
ActiveUser)
: new NotLoggedIn(
selectedBike.State.Value,
viewService,
bikesViewModel,
ActiveUser);
2023-01-18 14:22:51 +01:00
selectedBike.PropertyChanged += (sender, ev) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <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);
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(nameof(ButtonText)));
handler(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
}
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets visiblity of the copri command button. </summary>
2023-01-18 14:22:51 +01:00
public bool IsButtonVisible
=> RequestHandler.IsButtonVisible
&& Bike.DataSource == Model.Bikes.BikeInfoNS.BC.DataSource.Copri /* do not show button if data is from cache */ ;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets the text of the copri command button. </summary>
public string ButtonText => RequestHandler.ButtonText;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <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();
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
if (lastHandler.IsRemoveBikeRequired)
{
BikeRemoveDelegate(Id);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
if (lastHandler.GetType() == RequestHandler.GetType())
{
// No state change occurred.
return;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
// State changed and instance of request handler was switched.
if (lastHandler.ButtonText != ButtonText)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ButtonText)));
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
if (lastHandler.IsButtonVisible != IsButtonVisible)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
}
});
}
2021-05-13 20:03:07 +02:00
}