2021-07-14 00:16:50 +02:00
|
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TINK.Model.Bike;
|
|
|
|
|
using TINK.Model.Connector;
|
|
|
|
|
using TINK.Model.User;
|
|
|
|
|
using TINK.View;
|
|
|
|
|
using TINK.Settings;
|
|
|
|
|
using TINK.Model.Bike.BluetoothLock;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using TINK.Services.BluetoothLock;
|
|
|
|
|
using TINK.Model.Services.Geolocation;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using TINK.Model;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using TINK.ViewModel.Bikes;
|
|
|
|
|
using TINK.Services.BluetoothLock.Tdo;
|
2021-11-07 19:42:59 +01:00
|
|
|
|
using TINK.Services.Permissions;
|
2021-07-14 00:16:50 +02:00
|
|
|
|
using Plugin.BLE.Abstractions.Contracts;
|
|
|
|
|
using TINK.MultilingualResources;
|
|
|
|
|
using TINK.Model.Device;
|
2022-01-04 18:59:16 +01:00
|
|
|
|
using TINK.Model.Station;
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
|
|
|
|
namespace TINK.ViewModel.FindBike
|
|
|
|
|
{
|
|
|
|
|
public class FindBikePageViewModel : BikesViewModel, INotifyCollectionChanged, INotifyPropertyChanged
|
|
|
|
|
{
|
2021-07-14 22:39:31 +02:00
|
|
|
|
private string bikeIdUserInput = string.Empty;
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
/// <summary> Text entered by user to specify a bike.</summary>
|
2021-07-14 22:39:31 +02:00
|
|
|
|
public string BikeIdUserInput
|
|
|
|
|
{
|
|
|
|
|
get => bikeIdUserInput;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == bikeIdUserInput)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bikeIdUserInput = value;
|
|
|
|
|
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsSelectBikeEnabled)));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
/// <summary> Holds all bikes available.</summary>
|
|
|
|
|
public BikeCollection Bikes { get; set; }
|
|
|
|
|
|
2021-07-14 22:39:31 +02:00
|
|
|
|
/// <summary> Do not allow to select bike if id is not set.</summary>
|
|
|
|
|
public bool IsSelectBikeEnabled => 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;
|
|
|
|
|
|
2022-01-04 18:59:16 +01:00
|
|
|
|
/// <summary> Holds the stations to get station names form station ids. </summary>
|
|
|
|
|
private IEnumerable<IStation> Stations { get; }
|
|
|
|
|
|
2021-07-14 00:16:50 +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>
|
2022-01-04 18:59:16 +01:00
|
|
|
|
/// <param name="stations">Stations to get station name from station id.</param>
|
2021-07-14 00:16:50 +02:00
|
|
|
|
/// <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>
|
|
|
|
|
public FindBikePageViewModel(
|
|
|
|
|
User p_oUser,
|
2021-11-07 19:42:59 +01:00
|
|
|
|
ILocationPermission permissions,
|
2021-07-14 00:16:50 +02:00
|
|
|
|
IBluetoothLE bluetoothLE,
|
|
|
|
|
string runtimPlatform,
|
|
|
|
|
Func<bool> isConnectedDelegate,
|
|
|
|
|
Func<bool, IConnector> connectorFactory,
|
|
|
|
|
IGeolocation geolocation,
|
|
|
|
|
ILocksService lockService,
|
2022-01-04 18:59:16 +01:00
|
|
|
|
IEnumerable<IStation> stations,
|
2021-07-14 00:16:50 +02:00
|
|
|
|
PollingParameters polling,
|
|
|
|
|
Action<SendOrPostCallback, object> postAction,
|
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
|
IViewService viewService) : base(p_oUser, permissions, bluetoothLE, runtimPlatform, isConnectedDelegate, connectorFactory, geolocation, lockService, polling, postAction, smartDevice, viewService, () => new MyBikeInUseStateInfoProvider())
|
|
|
|
|
{
|
2021-07-14 22:39:31 +02:00
|
|
|
|
CollectionChanged += (sender, eventargs) =>
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsSelectBikeVisible)));
|
|
|
|
|
};
|
2022-01-04 18:59:16 +01:00
|
|
|
|
|
|
|
|
|
Stations = stations ?? throw new ArgumentException(nameof(stations));
|
2021-07-14 00:16:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when page is shown.
|
|
|
|
|
/// Starts update process.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task OnAppearing()
|
|
|
|
|
{
|
2021-07-14 20:40:55 +02:00
|
|
|
|
Log.ForContext<FindBikePageViewModel>().Information("User request to show page FindBike- page re-appearing");
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
2021-08-28 10:04:10 +02:00
|
|
|
|
ActionText = AppResources.ActivityTextFindBikeLoadingBikes;
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
|
|
|
|
var bikes = await ConnectorFactory(IsConnected).Query.GetBikesAsync();
|
|
|
|
|
|
|
|
|
|
Exception = bikes.Exception; // Update communication error from query for bikes occupied.
|
2021-07-14 20:40:55 +02:00
|
|
|
|
Bikes = bikes.Response;
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
|
IsIdle = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 20:40:55 +02:00
|
|
|
|
/// <summary> Command object to bind select bike button to view model. </summary>
|
2021-07-14 22:39:31 +02:00
|
|
|
|
public System.Windows.Input.ICommand OnSelectBikeRequest => new Xamarin.Forms.Command(async () => await SelectBike(), () => IsSelectBikeEnabled);
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
/// <summary> Select a bike by ID</summary>
|
|
|
|
|
public async Task SelectBike()
|
|
|
|
|
{
|
2021-07-14 23:30:17 +02:00
|
|
|
|
var selectedBike = Bikes.FirstOrDefault(x => x.Id.Equals(BikeIdUserInput.Trim(), StringComparison.OrdinalIgnoreCase));
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
if (selectedBike == null)
|
|
|
|
|
{
|
|
|
|
|
await ViewService.DisplayAlert("Fehler bei Radauswahl!", $"Kein Rad mit Id {BikeIdUserInput} gefunden.", "OK");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bikeCollection = new BikeCollection(new Dictionary<string, Model.Bike.BC.BikeInfo> { { selectedBike.Id, selectedBike } });
|
|
|
|
|
|
|
|
|
|
var lockIdList = bikeCollection
|
|
|
|
|
.GetLockIt()
|
|
|
|
|
.Cast<BikeInfo>()
|
|
|
|
|
.Select(x => x.LockInfo)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (LockService is ILocksServiceFake serviceFake)
|
|
|
|
|
{
|
|
|
|
|
serviceFake.UpdateSimulation(bikeCollection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check bluetooth and location permission and states
|
|
|
|
|
ActionText = AppResources.ActivityTextCheckBluetoothState;
|
|
|
|
|
|
|
|
|
|
if (bikeCollection.FirstOrDefault(x => x is BikeInfo btBike) != null
|
|
|
|
|
&& RuntimePlatform == Device.Android)
|
|
|
|
|
{
|
|
|
|
|
// Check location permission
|
2021-11-07 19:42:59 +01:00
|
|
|
|
var status = await PermissionsService.CheckStatusAsync();
|
|
|
|
|
if (status != Status.Granted)
|
2021-07-14 20:40:55 +02:00
|
|
|
|
{
|
2021-11-07 19:42:59 +01:00
|
|
|
|
var permissionResult = await PermissionsService.RequestAsync();
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
2021-11-07 19:42:59 +01:00
|
|
|
|
if (permissionResult != Status.Granted)
|
2021-07-14 20:40:55 +02: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.
|
2022-01-04 18:59:16 +01:00
|
|
|
|
BikeCollection.Update(bikeCollection, Stations);
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
//await OnAppearing(() => UpdateTask());
|
|
|
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
|
IsIdle = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open permissions dialog.
|
|
|
|
|
PermissionsService.OpenAppSettings();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Location state
|
|
|
|
|
if (Geolocation.IsGeolcationEnabled == false)
|
|
|
|
|
{
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
|
AppResources.MessageBikesManagementLocationActivation,
|
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
2022-01-04 18:59:16 +01:00
|
|
|
|
BikeCollection.Update(bikeCollection, Stations);
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
await OnAppearing(() => UpdateTask());
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
2021-07-14 20:40:55 +02:00
|
|
|
|
ActionText = "";
|
|
|
|
|
IsIdle = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-14 00:16:50 +02:00
|
|
|
|
|
2021-07-14 20:40:55 +02:00
|
|
|
|
// Bluetooth state
|
|
|
|
|
if (await BluetoothService.GetBluetoothState() != BluetoothState.On)
|
|
|
|
|
{
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
|
AppResources.MessageTitleHint,
|
|
|
|
|
AppResources.MessageBikesManagementBluetoothActivation,
|
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
|
|
2022-01-04 18:59:16 +01:00
|
|
|
|
BikeCollection.Update(bikeCollection, Stations);
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
await OnAppearing(() => UpdateTask());
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2022-01-04 18:59:16 +01:00
|
|
|
|
BikeCollection.Update(bikeCollection.UpdateLockInfo(locksInfo), Stations);
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
await OnAppearing(() => UpdateTask());
|
|
|
|
|
|
|
|
|
|
ActionText = "";
|
|
|
|
|
IsIdle = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
2021-07-14 23:31:30 +02:00
|
|
|
|
var result = ConnectorFactory(IsConnected).Query.GetBikesAsync().Result;
|
2021-07-14 20:40:55 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 23:31:30 +02:00
|
|
|
|
var selectedBike = bikes.FirstOrDefault(x => x.Id.Equals(BikeIdUserInput.Trim(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
bikes = selectedBike != null
|
|
|
|
|
? new BikeCollection(new Dictionary<string, Model.Bike.BC.BikeInfo> { { selectedBike.Id, selectedBike } })
|
|
|
|
|
: new BikeCollection();
|
|
|
|
|
|
2021-07-14 20:40:55 +02:00
|
|
|
|
PostAction(
|
|
|
|
|
unused =>
|
|
|
|
|
{
|
2022-01-04 18:59:16 +01:00
|
|
|
|
BikeCollection.Update(bikes, Stations); // Updating collection leads to update of GUI.
|
2021-07-14 20:40:55 +02:00
|
|
|
|
Exception = result.Exception;
|
|
|
|
|
ActionText = string.Empty;
|
|
|
|
|
},
|
|
|
|
|
null);
|
|
|
|
|
}
|
2021-07-14 00:16:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|