sharee.bike-App/SharedBusinessLogic/ViewModel/SelectBike/SelectBikePageViewModel.cs

506 lines
16 KiB
C#
Raw Normal View History

using System;
2022-08-30 15:42:25 +02:00
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
2022-08-30 15:42:25 +02:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Plugin.BLE.Abstractions.Contracts;
using Serilog;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model;
using ShareeBike.Model.Bikes;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Model.Stations.StationNS;
using ShareeBike.Model.User;
using ShareeBike.MultilingualResources;
using ShareeBike.Repository.Exception;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.Services.BluetoothLock.Tdo;
using ShareeBike.Services.Geolocation;
using ShareeBike.Services.Permissions;
using ShareeBike.Settings;
using ShareeBike.View;
using ShareeBike.ViewModel.Bikes;
using ShareeBike.ViewModel.Map;
2022-08-30 15:42:25 +02:00
using Xamarin.Forms;
2023-01-18 14:22:51 +01:00
using Command = Xamarin.Forms.Command;
2024-04-09 12:53:23 +02:00
namespace ShareeBike.ViewModel.SelectBike
{
2023-11-21 15:26:57 +01:00
public class SelectBikePageViewModel : BikesViewModel, INotifyCollectionChanged, INotifyPropertyChanged
2022-09-06 16:08:19 +02:00
{
private string bikeIdUserInput = string.Empty;
/// <summary> Text entered by user to specify a bike.</summary>
public string BikeIdUserInput
{
get => bikeIdUserInput;
set
{
if (value == bikeIdUserInput)
{
return;
}
bikeIdUserInput = value;
2022-10-17 18:45:38 +02:00
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(BikeIdUserInput)));
2022-09-06 16:08:19 +02:00
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsSelectBikeEnabled)));
}
}
/// <summary>
/// True if any action can be performed (request and cancel request)
/// </summary>
public override bool IsIdle
{
get => base.IsIdle;
set
{
if (value == IsIdle)
return;
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Debug($"Switch value of {nameof(IsIdle)} to {value}.");
2022-09-06 16:08:19 +02:00
base.IsIdle = value;
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsSelectBikeEnabled))); // Enable select bike button.
}
}
/// <summary> Holds all bikes available.</summary>
public BikeCollection Bikes { get; set; }
/// <summary> Do not allow to select bike if id is not set.</summary>
2023-09-22 11:38:42 +02:00
public bool IsSelectBikeEnabled => IsIdle && BikeIdUserInput != null && BikeIdUserInput.Length > 1 && BikeIdUserInput.Any(x => char.IsLetter(x)) && BikeIdUserInput.Any(x => char.IsDigit(x));
2022-09-06 16:08:19 +02:00
/// <summary> Hide id input fields as soon as bike is found.</summary>
public bool IsSelectBikeVisible => BikeCollection != null && BikeCollection.Count == 0;
/// <summary> Holds the stations to get station names form station ids. </summary>
private IEnumerable<IStation> Stations { get; }
2024-04-09 12:53:23 +02:00
/// <summary> Reference on the shareeBike app instance. </summary>
private IShareeBikeApp ShareeBikeApp { get; }
2023-09-22 11:38:42 +02:00
2023-01-18 14:22:51 +01:00
/// <summary>
/// True if ListView of Bikes is refreshing after user pulled;
/// </summary>
2023-06-06 12:00:24 +02:00
private bool isRefreshing = false;
2023-01-18 14:22:51 +01:00
public bool IsRefreshing
{
2023-06-06 12:00:24 +02:00
get { return isRefreshing; }
2023-01-18 14:22:51 +01:00
set
{
2023-06-06 12:00:24 +02:00
isRefreshing = value;
2023-01-18 14:22:51 +01:00
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsRefreshing)));
}
}
2023-02-22 14:03:35 +01:00
/// <summary>
/// Holds what should be executed on pull to refresh
/// </summary>
2023-01-18 14:22:51 +01:00
public Command RefreshCommand { get; }
2023-06-06 12:00:24 +02:00
public Command ShowFilterBikeTypeInfoCommand { get; private set; }
2023-01-18 14:22:51 +01:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Constructs bike collection view model in case information about occupied bikes is available.
/// </summary>
2023-07-04 11:06:38 +02:00
/// <param name="user">Mail address of active user.</param>
2024-04-09 12:53:23 +02:00
/// <param name="shareeBikeApp"> Reference to shareeBike app model.</param>
2022-09-06 16:08:19 +02:00
/// <param name="isReportLevelVerbose">True if report level is verbose, false if not.</param>
2023-04-19 12:14:14 +02:00
/// <param name="permissions">Holds object to query location permissions.</param>
2022-09-06 16:08:19 +02:00
/// <param name="bluetoothLE">Holds object to query bluetooth state.</param>
/// <param name="runtimPlatform">Specifies on which platform code is run.</param>
/// <param name="isConnectedDelegate">Returns if mobile is connected to web or not.</param>
/// <param name="connectorFactory">Connects system to copri.</param>
/// <param name="lockService">Service to control lock retrieve info.</param>
/// <param name="stations">Stations to get station name from station id.</param>
2023-04-19 12:14:14 +02:00
/// <param name="polling"> Holds whether to poll or not and the period length is polling is on. </param>
2022-09-06 16:08:19 +02:00
/// <param name="postAction">Executes actions on GUI thread.</param>
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
2023-04-19 12:14:14 +02:00
/// <param name="viewService">Interface to actuate methods on GUI.</param>
2022-09-06 16:08:19 +02:00
/// <param name="openUrlInBrowser">Delegate to open browser.</param>
2023-11-21 15:26:57 +01:00
public SelectBikePageViewModel(
2023-07-04 11:06:38 +02:00
User user,
2024-04-09 12:53:23 +02:00
IShareeBikeApp shareeBikeApp,
2022-09-06 16:08:19 +02:00
ILocationPermission permissions,
IBluetoothLE bluetoothLE,
string runtimPlatform,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
2023-04-05 15:02:10 +02:00
IGeolocationService geolocation,
2022-09-06 16:08:19 +02:00
ILocksService lockService,
IEnumerable<IStation> stations,
PollingParameters polling,
Action<SendOrPostCallback, object> postAction,
ISmartDevice smartDevice,
IViewService viewService,
2023-11-21 15:26:57 +01:00
Action<string> openUrlInBrowser) : base(user, new ViewContext(PageContext.SelectBike), permissions, bluetoothLE, runtimPlatform, isConnectedDelegate, connectorFactory, geolocation, lockService, polling, postAction, smartDevice, viewService, openUrlInBrowser, () => new MyBikeInUseStateInfoProvider())
2022-09-06 16:08:19 +02:00
{
CollectionChanged += (sender, eventargs) =>
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsSelectBikeVisible)));
2024-04-09 12:53:23 +02:00
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsLoginRequiredHintVisible)));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsRefreshing)));
2022-09-06 16:08:19 +02:00
};
Stations = stations ?? throw new ArgumentException(nameof(stations));
2023-01-18 14:22:51 +01:00
2024-04-09 12:53:23 +02:00
ShareeBikeApp = shareeBikeApp
?? throw new ArgumentException("Can not instantiate settings page view model- object. No shareeBike app object available.");
2023-09-22 11:38:42 +02:00
2023-01-18 14:22:51 +01:00
RefreshCommand = new Command(async () => {
IsRefreshing = false;
2023-02-22 14:03:35 +01:00
await SelectBike();
2023-01-18 14:22:51 +01:00
});
2022-09-06 16:08:19 +02:00
}
/// <summary>
/// Invoked when page is shown.
/// Starts update process.
/// </summary>
2023-02-22 14:03:35 +01:00
public async Task OnAppearingOrRefresh()
2022-09-06 16:08:19 +02:00
{
2022-10-17 18:45:38 +02:00
IsIdle = false;
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Information("User request to show page SelectBike- page re-appearing");
2022-09-06 16:08:19 +02:00
2023-02-22 14:03:35 +01:00
IsConnected = IsConnectedDelegate();
// Stop polling before getting bikes info.
2023-08-31 12:20:06 +02:00
await m_oViewUpdateManager.StopAsync();
2023-02-22 14:03:35 +01:00
2022-10-17 18:45:38 +02:00
if (string.IsNullOrEmpty(BikeIdUserInput) /* Find bike page flyout was taped */
&& BikeCollection.Count > 0 /* Bike was successfully selected */)
{
// Find bike page flyout was taped and page was already opened before and bike has been selected.
// Clear bike collection to allow user to enter bike id and search for bike.
BikeCollection.Clear();
}
2022-09-06 16:08:19 +02:00
2022-10-17 18:45:38 +02:00
if (BikeCollection.Count > 0)
{
// Page is appearing not because page flyout was taped.
// Bike has already been selected.
// Restart update.
await StartUpdateTask(() => UpdateTask());
2022-09-06 16:08:19 +02:00
2023-02-22 14:03:35 +01:00
ActionText = string.Empty;
2022-10-17 18:45:38 +02:00
IsIdle = true;
return;
}
2022-09-06 16:08:19 +02:00
2023-11-21 15:26:57 +01:00
ActionText = string.Empty;
IsIdle = true;
2022-09-06 16:08:19 +02:00
}
/// <summary> Command object to bind select bike button to view model. </summary>
2023-11-21 15:26:57 +01:00
public System.Windows.Input.ICommand OnSelectBikeRequest => new Command(async () => await SelectBike());
2022-09-06 16:08:19 +02:00
/// <summary> Select a bike by ID</summary>
public async Task SelectBike()
{
2023-09-22 11:38:42 +02:00
if (!IsSelectBikeEnabled)
2022-10-17 18:45:38 +02:00
{
2023-09-22 11:38:42 +02:00
await ViewService.DisplayAlert(
String.Empty,
AppResources.ErrorSelectBikeInputNotSufficent,
AppResources.MessageAnswerOk);
return;
2022-10-17 18:45:38 +02:00
}
2023-09-22 11:38:42 +02:00
else
2022-10-17 18:45:38 +02:00
{
2023-09-22 11:38:42 +02:00
// Get List of bike to be able to connect to.
2023-11-21 15:26:57 +01:00
ActionText = AppResources.ActivityTextSelectBikeLoadingBikes;
2023-09-22 11:38:42 +02:00
IsIdle = false;
2022-10-17 18:45:38 +02:00
2023-09-22 11:38:42 +02:00
IsConnected = IsConnectedDelegate();
2022-10-17 18:45:38 +02:00
2023-09-22 11:38:42 +02:00
Result<BikeCollection> bikes = null;
try
{
2023-11-21 15:26:57 +01:00
bikes = await ConnectorFactory(IsConnected).Query.GetBikesAsync(bikeId: BikeIdUserInput.Trim());
2023-09-22 11:38:42 +02:00
}
catch (Exception exception)
{
if (exception is WebConnectFailureException)
{
// Copri server is not reachable.
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Information("Getting bikes failed (Copri server not reachable).");
2022-10-17 18:45:38 +02:00
2023-09-22 11:38:42 +02:00
await ViewService.DisplayAlert(
AppResources.ErrorSelectBikeTitle,
AppResources.ErrorNoWeb,
AppResources.MessageAnswerOk);
}
else
{
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Error("Getting bikes failed. {Exception}", exception);
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
await ViewService.DisplayAlert(
AppResources.ErrorSelectBikeTitle,
exception.Message,
AppResources.MessageAnswerOk);
}
2022-10-17 18:45:38 +02:00
2023-02-22 14:03:35 +01:00
ActionText = string.Empty;
2022-10-17 18:45:38 +02:00
IsIdle = true;
2022-09-06 16:08:19 +02:00
return;
}
2023-09-22 11:38:42 +02:00
finally
{
Exception = bikes?.Exception ?? null; // Update communication error from query for bikes occupied.
Bikes = bikes.Response;
}
try
{
2023-11-21 15:26:57 +01:00
var selectedBike = bikes.Response.FirstOrDefault();
2023-09-22 11:38:42 +02:00
if (selectedBike == null)
{
await ViewService.DisplayAlert(
AppResources.ErrorSelectBikeTitle,
2024-04-09 12:53:23 +02:00
string.Format(AppResources.ErrorSelectBikeNoBikeFound, BikeIdUserInput),
2023-09-22 11:38:42 +02:00
AppResources.MessageAnswerOk);
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
ActionText = string.Empty;
IsIdle = true;
return;
}
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
var bikeCollection = new BikeCollection(new Dictionary<string, Model.Bikes.BikeInfoNS.BC.BikeInfo> { { selectedBike.Id, selectedBike } });
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
var lockIdList = bikeCollection
.GetLockIt()
.Cast<BikeInfo>()
.Select(x => x.LockInfo)
.ToList();
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
if (LockService is ILocksServiceFake serviceFake)
{
serviceFake.UpdateSimulation(bikeCollection);
}
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
// Check bluetooth and location permission and states
ActionText = AppResources.ActivityTextCheckBluetoothState;
if (bikeCollection.FirstOrDefault(x => x is BikeInfo btBike) != null
//&& RuntimePlatform == Device.Android
)
2022-09-06 16:08:19 +02:00
{
2023-09-22 11:38:42 +02:00
// Check location permission
var status = await PermissionsService.CheckStatusAsync();
if (status != Status.Granted)
2022-09-06 16:08:19 +02:00
{
2023-09-22 11:38:42 +02:00
if (RuntimePlatform == Device.Android)
{
var permissionResult = await PermissionsService.RequestAsync();
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
if (permissionResult != Status.Granted)
{
var dialogResult = await ViewService.DisplayAlert(
AppResources.MessageHintTitle,
AppResources.MessageBikesManagementLocationPermissionOpenDialog,
AppResources.MessageAnswerYes,
AppResources.MessageAnswerNo);
if (!dialogResult)
{
// User decided not to give access to locations permissions.
BikeCollection.Update(bikeCollection, Stations);
await StartUpdateTask(() => UpdateTask());
ActionText = string.Empty;
IsIdle = true;
return;
}
// Open permissions dialog.
PermissionsService.OpenAppSettings();
}
}
else
2022-09-06 16:08:19 +02:00
{
2022-12-07 16:54:52 +01:00
var dialogResult = await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.MessageHintTitle,
2022-12-07 16:54:52 +01:00
AppResources.MessageBikesManagementLocationPermissionOpenDialog,
AppResources.MessageAnswerYes,
AppResources.MessageAnswerNo);
if (!dialogResult)
{
// User decided not to give access to locations permissions.
BikeCollection.Update(bikeCollection, Stations);
await StartUpdateTask(() => UpdateTask());
2023-02-22 14:03:35 +01:00
ActionText = string.Empty;
2022-12-07 16:54:52 +01:00
IsIdle = true;
return;
}
// Open permissions dialog.
PermissionsService.OpenAppSettings();
2022-09-06 16:08:19 +02:00
}
2022-12-07 16:54:52 +01:00
}
2023-09-22 11:38:42 +02:00
// Location state
if (GeolocationService.IsGeolcationEnabled == false)
{
await ViewService.DisplayAlert(
AppResources.MessageHintTitle,
AppResources.MessageBikesManagementLocationActivation,
AppResources.MessageAnswerOk);
2022-12-07 16:54:52 +01:00
2023-09-22 11:38:42 +02:00
BikeCollection.Update(bikeCollection, Stations);
2022-12-07 16:54:52 +01:00
2023-09-22 11:38:42 +02:00
await StartUpdateTask(() => UpdateTask());
2022-12-07 16:54:52 +01:00
2023-09-22 11:38:42 +02:00
ActionText = string.Empty;
IsIdle = true;
return;
2022-09-06 16:08:19 +02:00
}
2023-09-22 11:38:42 +02:00
// Bluetooth state
if (await BluetoothService.GetBluetoothState() != BluetoothState.On)
{
await ViewService.DisplayAlert(
AppResources.MessageHintTitle,
AppResources.MessageBikesManagementBluetoothActivation,
AppResources.MessageAnswerOk);
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
BikeCollection.Update(bikeCollection, Stations);
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
await StartUpdateTask(() => UpdateTask());
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
ActionText = string.Empty;
IsIdle = true;
return;
}
2022-09-06 16:08:19 +02:00
}
2023-09-22 11:38:42 +02:00
// Connect to bluetooth devices.
ActionText = AppResources.ActivityTextSearchBikes;
IEnumerable<LockInfoTdo> locksInfoTdo;
try
2022-09-06 16:08:19 +02:00
{
2023-09-22 11:38:42 +02:00
locksInfoTdo = await LockService.GetLocksStateAsync(
lockIdList.Select(x => x.ToLockInfoTdo()).ToList(),
LockService.TimeOut.MultiConnect);
}
catch (Exception exception)
{
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Error("Getting bluetooth state failed. {Exception}", exception);
2023-09-22 11:38:42 +02:00
locksInfoTdo = new List<LockInfoTdo>();
}
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
var locksInfo = lockIdList.UpdateById(locksInfoTdo);
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
BikeCollection.Update(bikeCollection.UpdateLockInfo(locksInfo), Stations);
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
await StartUpdateTask(() => UpdateTask());
2022-09-06 16:08:19 +02:00
2023-09-22 11:38:42 +02:00
ActionText = string.Empty;
IsIdle = true;
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
2023-09-22 11:38:42 +02:00
await ViewService.DisplayAlert(
AppResources.ErrorSelectBikeTitle,
exception.Message,
AppResources.MessageAnswerOk);
2022-09-06 16:08:19 +02:00
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Error("Running command to select bike failed. {Exception}", exception);
2022-10-17 18:45:38 +02:00
2023-09-22 11:38:42 +02:00
ActionText = string.Empty;
IsIdle = true;
return;
}
2022-09-06 16:08:19 +02:00
}
2023-06-06 12:00:24 +02:00
2022-09-06 16:08:19 +02:00
}
/// <summary> Create task which updates my bike view model.</summary>
private void UpdateTask()
{
// Start task which periodically updates pins.
PostAction(
unused =>
{
ActionText = AppResources.ActivityTextUpdating;
IsConnected = IsConnectedDelegate();
},
null);
var result = ConnectorFactory(IsConnected).Query.GetBikesAsync().Result;
var bikes = result.Response;
var exception = result.Exception;
if (exception != null)
{
2023-11-21 15:26:57 +01:00
Log.ForContext<SelectBikePageViewModel>().Error("Getting bikes in polling context failed with exception {Exception}.", exception);
2022-09-06 16:08:19 +02:00
}
var selectedBike = bikes.FirstOrDefault(x => x.Id.Equals(BikeIdUserInput.Trim(), StringComparison.OrdinalIgnoreCase));
bikes = selectedBike != null
? new BikeCollection(new Dictionary<string, Model.Bikes.BikeInfoNS.BC.BikeInfo> { { selectedBike.Id, selectedBike } })
: new BikeCollection();
PostAction(
unused =>
{
BikeCollection.Update(bikes, Stations); // Updating collection leads to update of GUI.
Exception = result.Exception;
ActionText = string.Empty;
},
null);
}
2023-06-06 12:00:24 +02:00
2023-11-21 15:26:57 +01:00
/// <summary>
2024-04-09 12:53:23 +02:00
/// Informs about need to log in before requesting an bike.
2023-11-21 15:26:57 +01:00
/// </summary>
2024-04-09 12:53:23 +02:00
public bool IsLoginRequiredHintVisible
2023-06-06 12:00:24 +02:00
{
2024-04-09 12:53:23 +02:00
get
2023-06-06 12:00:24 +02:00
{
2024-04-09 12:53:23 +02:00
return !ActiveUser.IsLoggedIn;
2023-06-06 12:00:24 +02:00
}
}
/// <summary>
2024-04-09 12:53:23 +02:00
/// Informs about need to log in before requesting an bike.
2023-06-06 12:00:24 +02:00
/// </summary>
2024-04-09 12:53:23 +02:00
public string LoginRequiredHintText
=> ActiveUser.IsLoggedIn
? string.Empty
: AppResources.MarkingLoginRequiredToRerserve;
/// <summary> Command object to bind login page redirect link to view model.</summary>
public System.Windows.Input.ICommand LoginRequiredHintClickedCommand
=> new Xamarin.Forms.Command(async () => await OpenLoginPageAsync());
/// <summary> Opens login page. </summary>
public async Task OpenLoginPageAsync()
2023-06-06 12:00:24 +02:00
{
2024-04-09 12:53:23 +02:00
try
2023-11-21 15:26:57 +01:00
{
2024-04-09 12:53:23 +02:00
// Switch to map page
await ViewService.ShowPage("//LoginPage");
2023-11-21 15:26:57 +01:00
}
2024-04-09 12:53:23 +02:00
catch (Exception p_oException)
2023-11-21 15:26:57 +01:00
{
2024-04-09 12:53:23 +02:00
Log.Error("Ein unerwarteter Fehler ist in der Klasse SelectBikePageViewModel aufgetreten. Kontext: Klick auf Hinweistext auf Rad auswählen- seite ohne Anmeldung. {@Exception}", p_oException);
return;
2023-06-06 12:00:24 +02:00
}
}
2022-09-06 16:08:19 +02:00
}
}