mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 11:06:26 +01:00
244 lines
8.7 KiB
C#
244 lines
8.7 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.User;
|
|
using TINK.MultilingualResources;
|
|
using TINK.Services.BluetoothLock;
|
|
using TINK.Services.Geolocation;
|
|
using TINK.View;
|
|
using BikeInfoMutable = TINK.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock
|
|
{
|
|
/// <summary>
|
|
/// View model for a ILockIt bike.
|
|
/// Provides functionality for views
|
|
/// - MyBikes
|
|
/// - BikesAtStation
|
|
/// </summary>
|
|
public class BikeViewModel : BikeViewModelBase, INotifyPropertyChanged
|
|
{
|
|
public Xamarin.Forms.Command ShowTrackingInfoCommand { get; private set; }
|
|
public Xamarin.Forms.Command ShowRideTypeInfoCommand { get; private set; }
|
|
public Xamarin.Forms.Command ShowBikeIsBoundToCityInfoCommand { get; private set; }
|
|
|
|
/// <summary> Notifies GUI about changes. </summary>
|
|
public override event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
private IGeolocationService GeolocationService { get; }
|
|
|
|
private ILocksService LockService { get; }
|
|
|
|
/// <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> Raises events if property values changed in order to update GUI.</summary>
|
|
private void RaisePropertyChangedEvent(
|
|
IRequestHandler lastHandler,
|
|
string lastStateText = null,
|
|
Xamarin.Forms.Color? lastStateColor = null)
|
|
{
|
|
if (lastHandler.ButtonText != ButtonText)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ButtonText)));
|
|
}
|
|
|
|
if (lastHandler.IsButtonVisible != IsButtonVisible)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
|
|
}
|
|
|
|
if (lastHandler.LockitButtonText != LockitButtonText)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(LockitButtonText)));
|
|
}
|
|
|
|
if (lastHandler.IsLockitButtonVisible != IsLockitButtonVisible)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsLockitButtonVisible)));
|
|
}
|
|
|
|
if (RequestHandler.ErrorText != lastHandler.ErrorText)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ErrorText)));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(lastStateText) && lastStateText != StateText)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StateText)));
|
|
}
|
|
|
|
if (lastStateColor != null && lastStateColor != StateColor)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StateColor)));
|
|
}
|
|
}
|
|
|
|
/// <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="user">Object holding logged in user or an empty user object.</param>
|
|
/// <param name="viewContext"> Holds the view context in which bike view model is used.</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,
|
|
IGeolocationService geolocationService,
|
|
ILocksService lockService,
|
|
Action<string> bikeRemoveDelegate,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
BikeInfoMutable selectedBike,
|
|
IUser user,
|
|
ViewContext viewContext,
|
|
IInUseStateInfoProvider stateInfoProvider,
|
|
IBikesViewModel bikesViewModel,
|
|
Action<string> openUrlInBrowser) : base(isConnectedDelegate, connectorFactory, bikeRemoveDelegate, viewUpdateManager, smartDevice, viewService, selectedBike, user, viewContext, stateInfoProvider, bikesViewModel, openUrlInBrowser)
|
|
{
|
|
ShowTrackingInfoCommand = new Xamarin.Forms.Command(async () => {
|
|
|
|
await ViewService.DisplayAlert(
|
|
"Tracking",
|
|
TariffDescription.TrackingInfoText,
|
|
AppResources.MessageAnswerOk);
|
|
|
|
});
|
|
|
|
ShowRideTypeInfoCommand = new Xamarin.Forms.Command(async () => {
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageAaRideTypeInfoTitle,
|
|
TariffDescription.RideTypeText,
|
|
AppResources.MessageAnswerOk);
|
|
|
|
});
|
|
|
|
ShowBikeIsBoundToCityInfoCommand = new Xamarin.Forms.Command(async () => {
|
|
|
|
// later, if value comes from backend: message = TariffDescription.CityAreaType
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageBikeIsBoundToCityInfoTitle,
|
|
String.Format(AppResources.MessageBikeIsBoundToCityInfoText,selectedBike.TypeOfBike),
|
|
AppResources.MessageAnswerOk);
|
|
|
|
});
|
|
|
|
RequestHandler = user.IsLoggedIn
|
|
? RequestHandlerFactory.Create(
|
|
selectedBike,
|
|
isConnectedDelegate,
|
|
connectorFactory,
|
|
geolocationService,
|
|
lockService,
|
|
viewUpdateManager,
|
|
smartDevice,
|
|
viewService,
|
|
bikesViewModel,
|
|
user)
|
|
: new NotLoggedIn(
|
|
selectedBike.State.Value,
|
|
viewService,
|
|
bikesViewModel);
|
|
|
|
GeolocationService = geolocationService
|
|
?? throw new ArgumentException($"Can not instantiate {this.GetType().Name}-object. Parameter {nameof(geolocationService)} can not be null.");
|
|
|
|
LockService = lockService
|
|
?? throw new ArgumentException($"Can not instantiate {this.GetType().Name}-object. Parameter {nameof(lockService)} can not be null.");
|
|
|
|
selectedBike.PropertyChanged += (sender, ev) =>
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsButtonVisible)));
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsLockitButtonVisible)));
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(OnButtonClicked)));
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(OnLockitButtonClicked)));
|
|
};
|
|
}
|
|
|
|
/// <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>
|
|
public override void OnSelectedBikeStateChanged()
|
|
{
|
|
var lastHandler = RequestHandler;
|
|
RequestHandler = RequestHandlerFactory.Create(
|
|
Bike,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
GeolocationService,
|
|
LockService,
|
|
ViewUpdateManager,
|
|
SmartDevice,
|
|
ViewService,
|
|
BikesViewModel,
|
|
ActiveUser);
|
|
|
|
RaisePropertyChangedEvent(lastHandler);
|
|
}
|
|
|
|
public bool IsBikeBoundToCity
|
|
=> Bike.AaRideType == TINK.Model.Bikes.BikeInfoNS.BikeNS.AaRideType.NoAaRide ? true : false;
|
|
|
|
/// <summary> Gets visibility 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> Gets visibility of the ILockIt command button. </summary>
|
|
public bool IsLockitButtonVisible
|
|
=> RequestHandler.IsLockitButtonVisible;
|
|
|
|
/// <summary> Gets the text of the ILockIt command button. </summary>
|
|
public string LockitButtonText => RequestHandler.LockitButtonText;
|
|
|
|
/// <summary> Processes request to perform a copri action (reserve bike and cancel reservation).
|
|
/// Button only enabled if data is up to date = not from cache. </summary>
|
|
public System.Windows.Input.ICommand OnButtonClicked => new Xamarin.Forms.Command(async () => await ClickButton(RequestHandler.HandleRequestOption1()));
|
|
|
|
/// <summary> Processes request to perform a ILockIt action (unlock bike and lock bike).
|
|
/// Button only enabled if data is up to date = not from cache. </summary>
|
|
public System.Windows.Input.ICommand OnLockitButtonClicked => new Xamarin.Forms.Command(async () => await ClickButton(RequestHandler.HandleRequestOption2()));
|
|
|
|
/// <summary> Processes request to perform a copri action (reserve bike and cancel reservation). </summary>
|
|
private async Task ClickButton(Task<IRequestHandler> handleRequest)
|
|
{
|
|
var lastHandler = RequestHandler;
|
|
var lastState = Bike.State.Value;
|
|
var lastStateText = StateText;
|
|
var lastStateColor = StateColor;
|
|
|
|
RequestHandler = await handleRequest;
|
|
|
|
CheckRemoveBike(Id, lastState);
|
|
|
|
if (RuntimeHelpers.Equals(lastHandler, RequestHandler))
|
|
{
|
|
// No state change occurred (same instance is returned).
|
|
return;
|
|
}
|
|
|
|
RaisePropertyChangedEvent(
|
|
lastHandler,
|
|
lastStateText,
|
|
lastStateColor);
|
|
}
|
|
|
|
public string ErrorText => RequestHandler.ErrorText;
|
|
|
|
}
|
|
}
|