2022-09-22 20:58:30 +02:00
|
|
|
|
2021-07-14 00:16:50 +02:00
|
|
|
using System;
|
2022-08-30 15:42:25 +02:00
|
|
|
using System.Collections.Generic;
|
2021-07-14 00:16:50 +02:00
|
|
|
using System.Collections.Specialized;
|
|
|
|
using System.ComponentModel;
|
2022-08-30 15:42:25 +02:00
|
|
|
using System.Linq;
|
2021-07-14 00:16:50 +02:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2022-08-30 15:42:25 +02:00
|
|
|
using Plugin.BLE.Abstractions.Contracts;
|
|
|
|
using Serilog;
|
|
|
|
using TINK.Model;
|
|
|
|
using TINK.Model.Bikes;
|
|
|
|
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
|
2021-07-14 00:16:50 +02:00
|
|
|
using TINK.Model.Connector;
|
2022-08-30 15:42:25 +02:00
|
|
|
using TINK.Model.Device;
|
2022-10-17 18:45:38 +02:00
|
|
|
using TINK.Model.Services.CopriApi;
|
2022-08-30 15:42:25 +02:00
|
|
|
using TINK.Model.Station;
|
2021-07-14 00:16:50 +02:00
|
|
|
using TINK.Model.User;
|
2022-08-30 15:42:25 +02:00
|
|
|
using TINK.MultilingualResources;
|
2022-10-17 18:45:38 +02:00
|
|
|
using TINK.Repository.Exception;
|
2021-07-14 00:16:50 +02:00
|
|
|
using TINK.Services.BluetoothLock;
|
|
|
|
using TINK.Services.BluetoothLock.Tdo;
|
2022-08-30 15:42:25 +02:00
|
|
|
using TINK.Services.Geolocation;
|
2021-11-07 19:42:59 +01:00
|
|
|
using TINK.Services.Permissions;
|
2022-08-30 15:42:25 +02:00
|
|
|
using TINK.Settings;
|
|
|
|
using TINK.View;
|
|
|
|
using TINK.ViewModel.Bikes;
|
|
|
|
using Xamarin.Forms;
|
2023-01-18 14:22:51 +01:00
|
|
|
using Command = Xamarin.Forms.Command;
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
|
|
namespace TINK.ViewModel.FindBike
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
public class FindBikePageViewModel : BikesViewModel, INotifyCollectionChanged, INotifyPropertyChanged
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Debug($"Switch value of {nameof(IsIdle)} to {value}.");
|
|
|
|
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>
|
|
|
|
public bool IsSelectBikeEnabled => IsIdle && BikeIdUserInput != null && BikeIdUserInput.Length > 0;
|
|
|
|
|
|
|
|
/// <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; }
|
|
|
|
|
2023-01-18 14:22:51 +01:00
|
|
|
/// <summary>
|
|
|
|
/// True if ListView of Bikes is refreshing after user pulled;
|
|
|
|
/// </summary>
|
|
|
|
private bool _isRefreshing = false;
|
|
|
|
public bool IsRefreshing
|
|
|
|
{
|
|
|
|
get { return _isRefreshing; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_isRefreshing = value;
|
|
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsRefreshing)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Command RefreshCommand { get; }
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs bike collection view model in case information about occupied bikes is available.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="p_oUser">Mail address of active user.</param>
|
|
|
|
/// <param name="isReportLevelVerbose">True if report level is verbose, false if not.</param>
|
|
|
|
/// <param name="permissions">Holds object to query location permisions.</param>
|
|
|
|
/// <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>
|
|
|
|
/// <param name="polling"> Holds whether to poll or not and the periode leght is polling is on. </param>
|
|
|
|
/// <param name="postAction">Executes actions on GUI thread.</param>
|
|
|
|
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
|
|
|
|
/// <param name="viewService">Interface to actuate methodes on GUI.</param>
|
|
|
|
/// <param name="openUrlInBrowser">Delegate to open browser.</param>
|
|
|
|
public FindBikePageViewModel(
|
|
|
|
User p_oUser,
|
|
|
|
ILocationPermission permissions,
|
|
|
|
IBluetoothLE bluetoothLE,
|
|
|
|
string runtimPlatform,
|
|
|
|
Func<bool> isConnectedDelegate,
|
|
|
|
Func<bool, IConnector> connectorFactory,
|
|
|
|
IGeolocation geolocation,
|
|
|
|
ILocksService lockService,
|
|
|
|
IEnumerable<IStation> stations,
|
|
|
|
PollingParameters polling,
|
|
|
|
Action<SendOrPostCallback, object> postAction,
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
IViewService viewService,
|
|
|
|
Action<string> openUrlInBrowser) : base(p_oUser, permissions, bluetoothLE, runtimPlatform, isConnectedDelegate, connectorFactory, geolocation, lockService, polling, postAction, smartDevice, viewService, openUrlInBrowser, () => new MyBikeInUseStateInfoProvider())
|
|
|
|
{
|
|
|
|
CollectionChanged += (sender, eventargs) =>
|
|
|
|
{
|
|
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsSelectBikeVisible)));
|
|
|
|
};
|
|
|
|
|
|
|
|
Stations = stations ?? throw new ArgumentException(nameof(stations));
|
2023-01-18 14:22:51 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Holds what should be executed on pull to refresh
|
|
|
|
/// </summary>
|
|
|
|
RefreshCommand = new Command(async () => {
|
|
|
|
|
|
|
|
IsRefreshing = true;
|
|
|
|
await OnAppearing();
|
|
|
|
IsRefreshing = false;
|
|
|
|
|
|
|
|
});
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Invoked when page is shown.
|
|
|
|
/// Starts update process.
|
|
|
|
/// </summary>
|
|
|
|
public async Task OnAppearing()
|
|
|
|
{
|
2022-10-17 18:45:38 +02:00
|
|
|
IsIdle = false;
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
Log.ForContext<FindBikePageViewModel>().Information("User request to show page FindBike- page re-appearing");
|
|
|
|
|
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
|
|
|
|
2022-10-17 18:45:38 +02:00
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
|
|
|
return;
|
|
|
|
}
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary> Command object to bind select bike button to view model. </summary>
|
|
|
|
public System.Windows.Input.ICommand OnSelectBikeRequest => new Xamarin.Forms.Command(async () => await SelectBike(), () => IsSelectBikeEnabled);
|
|
|
|
|
|
|
|
/// <summary> Select a bike by ID</summary>
|
|
|
|
public async Task SelectBike()
|
|
|
|
{
|
2022-10-17 18:45:38 +02:00
|
|
|
// Get List of bike to be able to connect to.
|
|
|
|
ActionText = AppResources.ActivityTextFindBikeLoadingBikes;
|
|
|
|
IsIdle = false;
|
|
|
|
|
|
|
|
Result<BikeCollection> bikes = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bikes = await ConnectorFactory(IsConnected).Query.GetBikesAsync();
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
if (exception is WebConnectFailureException)
|
|
|
|
{
|
|
|
|
// Copri server is not reachable.
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Information("Getting bikes failed failed (Copri server not reachable).");
|
|
|
|
|
|
|
|
await ViewService.DisplayAdvancedAlert(
|
|
|
|
AppResources.ErrorReturnBikeNoWebTitle,
|
|
|
|
string.Format("{0}\r\n{1}", AppResources.ErrorReturnBikeNoWebMessage, WebConnectFailureException.GetHintToPossibleExceptionsReasons),
|
|
|
|
exception.Message,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Error("Getting bikes failed. {Exception}", exception);
|
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
exception.Message,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Exception = bikes?.Exception ?? null; // Update communication error from query for bikes occupied.
|
|
|
|
Bikes = bikes.Response;
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var selectedBike = Bikes.FirstOrDefault(x => x.Id.Equals(BikeIdUserInput.Trim(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
if (selectedBike == null)
|
|
|
|
{
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
string.Format(AppResources.MessageErrorSelectBikeNoBikeFound, BikeIdUserInput),
|
|
|
|
AppResources.MessageAnswerOk);
|
2022-10-17 18:45:38 +02:00
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
2022-09-06 16:08:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bikeCollection = new BikeCollection(new Dictionary<string, Model.Bikes.BikeInfoNS.BC.BikeInfo> { { selectedBike.Id, selectedBike } });
|
|
|
|
|
|
|
|
var lockIdList = bikeCollection
|
|
|
|
.GetLockIt()
|
2022-09-22 20:58:30 +02:00
|
|
|
.Cast<BikeInfo>()
|
2022-09-06 16:08:19 +02:00
|
|
|
.Select(x => x.LockInfo)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
if (LockService is ILocksServiceFake serviceFake)
|
|
|
|
{
|
|
|
|
serviceFake.UpdateSimulation(bikeCollection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check bluetooth and location permission and states
|
|
|
|
ActionText = AppResources.ActivityTextCheckBluetoothState;
|
|
|
|
|
2022-09-22 20:58:30 +02:00
|
|
|
if (bikeCollection.FirstOrDefault(x => x is BikeInfo btBike) != null
|
2022-12-07 16:54:52 +01:00
|
|
|
//&& RuntimePlatform == Device.Android
|
|
|
|
)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
|
|
|
// Check location permission
|
|
|
|
var status = await PermissionsService.CheckStatusAsync();
|
|
|
|
if (status != Status.Granted)
|
|
|
|
{
|
2022-12-07 16:54:52 +01:00
|
|
|
if (RuntimePlatform == Device.Android)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2022-12-07 16:54:52 +01:00
|
|
|
var permissionResult = await PermissionsService.RequestAsync();
|
2022-09-06 16:08:19 +02:00
|
|
|
|
2022-12-07 16:54:52 +01:00
|
|
|
if (permissionResult != Status.Granted)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2022-12-07 16:54:52 +01:00
|
|
|
var dialogResult = await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
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 = "";
|
|
|
|
IsIdle = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open permissions dialog.
|
|
|
|
PermissionsService.OpenAppSettings();
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
2022-12-07 16:54:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var dialogResult = await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
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 = "";
|
|
|
|
IsIdle = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open permissions dialog.
|
|
|
|
PermissionsService.OpenAppSettings();
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location state
|
|
|
|
if (Geolocation.IsGeolcationEnabled == false)
|
|
|
|
{
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
AppResources.MessageBikesManagementLocationActivation,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
|
|
BikeCollection.Update(bikeCollection, Stations);
|
|
|
|
|
2022-10-17 18:45:38 +02:00
|
|
|
await StartUpdateTask(() => UpdateTask());
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bluetooth state
|
|
|
|
if (await BluetoothService.GetBluetoothState() != BluetoothState.On)
|
|
|
|
{
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
AppResources.MessageBikesManagementBluetoothActivation,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
|
|
BikeCollection.Update(bikeCollection, Stations);
|
|
|
|
|
2022-10-17 18:45:38 +02:00
|
|
|
await StartUpdateTask(() => UpdateTask());
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to bluetooth devices.
|
|
|
|
ActionText = AppResources.ActivityTextSearchBikes;
|
|
|
|
IEnumerable<LockInfoTdo> locksInfoTdo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
locksInfoTdo = await LockService.GetLocksStateAsync(
|
|
|
|
lockIdList.Select(x => x.ToLockInfoTdo()).ToList(),
|
|
|
|
LockService.TimeOut.MultiConnect);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Error("Getting bluetooth state failed. {Exception}", exception);
|
|
|
|
locksInfoTdo = new List<LockInfoTdo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
var locksInfo = lockIdList.UpdateById(locksInfoTdo);
|
|
|
|
|
|
|
|
BikeCollection.Update(bikeCollection.UpdateLockInfo(locksInfo), Stations);
|
|
|
|
|
2022-10-17 18:45:38 +02:00
|
|
|
await StartUpdateTask(() => UpdateTask());
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.MessageErrorSelectBikeTitle,
|
|
|
|
exception.Message,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Error("Running command to select bike failed. {Exception}", exception);
|
2022-10-17 18:45:38 +02:00
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
IsIdle = true;
|
2022-09-06 16:08:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Error("Getting bikes occupied in polling context failed with exception {Exception}.", exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 00:16:50 +02:00
|
|
|
}
|