2023-01-18 14:22:51 +01:00
|
|
|
using System;
|
2021-05-13 20:03:07 +02:00
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Runtime.CompilerServices;
|
2022-08-30 15:42:25 +02:00
|
|
|
using System.Threading.Tasks;
|
2024-04-09 12:53:23 +02:00
|
|
|
using ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command;
|
|
|
|
using ShareeBike.Model.Connector;
|
|
|
|
using ShareeBike.Model.Device;
|
|
|
|
using ShareeBike.Model.User;
|
|
|
|
using ShareeBike.MultilingualResources;
|
|
|
|
using ShareeBike.Services.BluetoothLock;
|
|
|
|
using ShareeBike.Services.Geolocation;
|
|
|
|
using ShareeBike.View;
|
|
|
|
using BikeInfoMutable = ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.BikeInfoMutable;
|
|
|
|
using DisconnectCommand = ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.DisconnectCommand;
|
|
|
|
|
|
|
|
namespace ShareeBike.ViewModel.Bikes.Bike.BluetoothLock
|
2021-05-13 20:03:07 +02:00
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
|
|
|
/// View model for a ILockIt bike.
|
|
|
|
/// Provides functionality for views
|
|
|
|
/// - MyBikes
|
|
|
|
/// - BikesAtStation
|
|
|
|
/// </summary>
|
2024-04-09 12:53:23 +02:00
|
|
|
public class BikeViewModel : BikeViewModelBase, INotifyPropertyChanged, DisconnectCommand.IDisconnectCommandListener
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2023-02-22 14:03:35 +01:00
|
|
|
public Xamarin.Forms.Command ShowTrackingInfoCommand { get; private set; }
|
2023-04-19 12:14:14 +02:00
|
|
|
public Xamarin.Forms.Command ShowRideTypeInfoCommand { get; private set; }
|
2023-08-31 12:31:38 +02:00
|
|
|
public Xamarin.Forms.Command ShowBikeIsBoundToCityInfoCommand { get; private set; }
|
2023-02-22 14:03:35 +01:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Notifies GUI about changes. </summary>
|
|
|
|
public override event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
2023-04-05 15:02:10 +02:00
|
|
|
private IGeolocationService GeolocationService { get; }
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Holds the view model for requesting a bike action.
|
|
|
|
/// </summary>
|
|
|
|
private readonly DisconnectLockActionViewModel<BikeViewModel> _disconnectLockActionViewModel;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the renting a bike progress.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Only used for testing.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="step">Current step to process.</param>
|
|
|
|
public void ReportStep(DisconnectCommand.Step step) => _disconnectLockActionViewModel?.ReportStep(step);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the renting a bike state.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Only used for testing.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="state">State to process.</param>
|
|
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
|
|
public async Task ReportStateAsync(DisconnectCommand.State state, string details) => await _disconnectLockActionViewModel.ReportStateAsync(state, details);
|
|
|
|
|
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="user">Object holding logged in user or an empty user object.</param>
|
2023-07-04 11:06:38 +02:00
|
|
|
/// <param name="viewContext"> Holds the view context in which bike view model is used.</param>
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <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,
|
2023-04-05 15:02:10 +02:00
|
|
|
IGeolocationService geolocationService,
|
2022-09-06 16:08:19 +02:00
|
|
|
ILocksService lockService,
|
|
|
|
Action<string> bikeRemoveDelegate,
|
|
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
IViewService viewService,
|
|
|
|
BikeInfoMutable selectedBike,
|
|
|
|
IUser user,
|
2023-07-04 11:06:38 +02:00
|
|
|
ViewContext viewContext,
|
2022-09-06 16:08:19 +02:00
|
|
|
IInUseStateInfoProvider stateInfoProvider,
|
|
|
|
IBikesViewModel bikesViewModel,
|
2023-07-04 11:06:38 +02:00
|
|
|
Action<string> openUrlInBrowser) : base(isConnectedDelegate, connectorFactory, bikeRemoveDelegate, viewUpdateManager, smartDevice, viewService, selectedBike, user, viewContext, stateInfoProvider, bikesViewModel, openUrlInBrowser)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2023-02-22 14:03:35 +01:00
|
|
|
ShowTrackingInfoCommand = new Xamarin.Forms.Command(async () => {
|
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
"Tracking",
|
|
|
|
TariffDescription.TrackingInfoText,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2023-04-19 12:14:14 +02:00
|
|
|
ShowRideTypeInfoCommand = new Xamarin.Forms.Command(async () => {
|
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageAaRideTypeInfoTitle,
|
|
|
|
TariffDescription.RideTypeText,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2023-08-31 12:31:38 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
_disconnectLockActionViewModel = new DisconnectLockActionViewModel<BikeViewModel>(
|
|
|
|
selectedBike,
|
|
|
|
viewUpdateManager,
|
|
|
|
bikesViewModel);
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
RequestHandler = user.IsLoggedIn
|
|
|
|
? RequestHandlerFactory.Create(
|
|
|
|
selectedBike,
|
|
|
|
isConnectedDelegate,
|
|
|
|
connectorFactory,
|
2023-04-05 15:02:10 +02:00
|
|
|
geolocationService,
|
2022-09-06 16:08:19 +02:00
|
|
|
lockService,
|
|
|
|
viewUpdateManager,
|
|
|
|
smartDevice,
|
|
|
|
viewService,
|
|
|
|
bikesViewModel,
|
|
|
|
user)
|
|
|
|
: new NotLoggedIn(
|
|
|
|
selectedBike.State.Value,
|
|
|
|
viewService,
|
|
|
|
bikesViewModel);
|
|
|
|
|
2023-04-05 15:02:10 +02:00
|
|
|
GeolocationService = geolocationService
|
|
|
|
?? throw new ArgumentException($"Can not instantiate {this.GetType().Name}-object. Parameter {nameof(geolocationService)} can not be null.");
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
LockService = lockService
|
|
|
|
?? throw new ArgumentException($"Can not instantiate {this.GetType().Name}-object. Parameter {nameof(lockService)} can not be null.");
|
2023-01-18 14:22:51 +01:00
|
|
|
|
|
|
|
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)));
|
|
|
|
};
|
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>
|
2024-04-09 12:53:23 +02:00
|
|
|
public override async void OnSelectedBikeStateChanged()
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
if (Bike.State.Value == Model.State.InUseStateEnum.Disposable)
|
|
|
|
{
|
|
|
|
await _disconnectLockActionViewModel.DisconnectLockAsync();
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
var lastHandler = RequestHandler;
|
|
|
|
RequestHandler = RequestHandlerFactory.Create(
|
|
|
|
Bike,
|
|
|
|
IsConnectedDelegate,
|
|
|
|
ConnectorFactory,
|
2023-04-05 15:02:10 +02:00
|
|
|
GeolocationService,
|
2022-09-06 16:08:19 +02:00
|
|
|
LockService,
|
|
|
|
ViewUpdateManager,
|
|
|
|
SmartDevice,
|
|
|
|
ViewService,
|
|
|
|
BikesViewModel,
|
|
|
|
ActiveUser);
|
|
|
|
|
|
|
|
RaisePropertyChangedEvent(lastHandler);
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:31:38 +02:00
|
|
|
public bool IsBikeBoundToCity
|
2024-04-09 12:53:23 +02:00
|
|
|
=> Bike.AaRideType == ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.AaRideType.NoAaRide ? true : false;
|
2023-08-31 12:31:38 +02:00
|
|
|
|
2023-06-06 12:00:24 +02:00
|
|
|
/// <summary> Gets visibility of the copri command button. </summary>
|
2023-01-18 14:22:51 +01:00
|
|
|
public bool IsButtonVisible
|
|
|
|
=> RequestHandler.IsButtonVisible;
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <summary> Gets the text of the copri command button. </summary>
|
|
|
|
public string ButtonText => RequestHandler.ButtonText;
|
|
|
|
|
2023-06-06 12:00:24 +02:00
|
|
|
/// <summary> Gets visibility of the ILockIt command button. </summary>
|
2023-01-18 14:22:51 +01:00
|
|
|
public bool IsLockitButtonVisible
|
|
|
|
=> RequestHandler.IsLockitButtonVisible;
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <summary> Gets the text of the ILockIt command button. </summary>
|
|
|
|
public string LockitButtonText => RequestHandler.LockitButtonText;
|
|
|
|
|
2023-02-22 14:03:35 +01:00
|
|
|
/// <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()));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
2023-02-22 14:03:35 +01:00
|
|
|
/// <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()));
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <summary> Processes request to perform a copri action (reserve bike and cancel reservation). </summary>
|
|
|
|
private async Task ClickButton(Task<IRequestHandler> handleRequest)
|
|
|
|
{
|
|
|
|
var lastHandler = RequestHandler;
|
2023-07-04 11:06:38 +02:00
|
|
|
var lastState = Bike.State.Value;
|
2022-09-06 16:08:19 +02:00
|
|
|
var lastStateText = StateText;
|
|
|
|
var lastStateColor = StateColor;
|
|
|
|
|
|
|
|
RequestHandler = await handleRequest;
|
|
|
|
|
2023-07-04 11:06:38 +02:00
|
|
|
CheckRemoveBike(Id, lastState);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
if (RuntimeHelpers.Equals(lastHandler, RequestHandler))
|
|
|
|
{
|
|
|
|
// No state change occurred (same instance is returned).
|
|
|
|
return;
|
|
|
|
}
|
2023-04-05 15:02:10 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
RaisePropertyChangedEvent(
|
|
|
|
lastHandler,
|
|
|
|
lastStateText,
|
|
|
|
lastStateColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ErrorText => RequestHandler.ErrorText;
|
2023-08-31 12:20:06 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
}
|