mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-23 07:36:31 +01:00
133 lines
4.6 KiB
C#
133 lines
4.6 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.Bikes.BikeInfoNS.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>
|
|
/// <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)));
|
|
}
|
|
});
|
|
}
|
|
}
|