mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 23:26:26 +01:00
259 lines
9.9 KiB
C#
259 lines
9.9 KiB
C#
|
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;
|
|||
|
using Plugin.Permissions;
|
|||
|
using Plugin.Permissions.Abstractions;
|
|||
|
using Plugin.BLE.Abstractions.Contracts;
|
|||
|
using TINK.MultilingualResources;
|
|||
|
|
|||
|
namespace TINK.ViewModel.MyBikes
|
|||
|
{
|
|||
|
public class MyBikesPageViewModel : BikesViewModel, INotifyCollectionChanged, INotifyPropertyChanged
|
|||
|
{
|
|||
|
/// <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="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="p_oPolling"> 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="viewService">Interface to actuate methodes on GUI.</param>
|
|||
|
public MyBikesPageViewModel(
|
|||
|
User p_oUser,
|
|||
|
IPermissions permissions,
|
|||
|
IBluetoothLE bluetoothLE,
|
|||
|
string runtimPlatform,
|
|||
|
Func<bool> isConnectedDelegate,
|
|||
|
Func<bool, IConnector> connectorFactory,
|
|||
|
IGeolocation geolocation,
|
|||
|
ILocksService lockService,
|
|||
|
PollingParameters p_oPolling,
|
|||
|
Action<SendOrPostCallback, object> postAction,
|
|||
|
IViewService viewService) : base(p_oUser, permissions, bluetoothLE, runtimPlatform, isConnectedDelegate, connectorFactory, geolocation, lockService, p_oPolling, postAction, viewService, () => new MyBikeInUseStateInfoProvider())
|
|||
|
{
|
|||
|
CollectionChanged += (sender, eventargs) =>
|
|||
|
{
|
|||
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsNoBikesOccupiedVisible)));
|
|||
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(NoBikesOccupiedText)));
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Returns if info about the fact that user did not request or book any bikes is visible or not.<summary>
|
|||
|
/// Gets message that logged in user has not booked any bikes.
|
|||
|
/// </summary>
|
|||
|
public bool IsNoBikesOccupiedVisible
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return Count <= 0 && IsIdle == true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Info about the fact that user did not request or book any bikes. </summary>
|
|||
|
public string NoBikesOccupiedText
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return IsNoBikesOccupiedVisible
|
|||
|
? $"Momentan sind keine Fahrräder auf Benutzer {ActiveUser?.Mail} reserviert/ gebucht."
|
|||
|
: string.Empty;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Invoked when page is shown.
|
|||
|
/// Starts update process.
|
|||
|
/// </summary>
|
|||
|
public async Task OnAppearing()
|
|||
|
{
|
|||
|
// Get my bikes from COPRI
|
|||
|
Log.ForContext<MyBikesPageViewModel>().Information("User request to show page MyBikes/ page re-appearing");
|
|||
|
|
|||
|
ActionText = AppResources.ActivityTextMyBikesLoadingBikes;
|
|||
|
|
|||
|
var bikesOccupied = await ConnectorFactory(IsConnected).Query.GetBikesOccupiedAsync();
|
|||
|
|
|||
|
Exception = bikesOccupied.Exception; // Update communication error from query for bikes occupied.
|
|||
|
|
|||
|
var lockIdList = bikesOccupied.Response
|
|||
|
.GetLockIt()
|
|||
|
.Cast<BikeInfo>()
|
|||
|
.Select(x => x.LockInfo)
|
|||
|
.ToList();
|
|||
|
|
|||
|
if (LockService is ILocksServiceFake serviceFake)
|
|||
|
{
|
|||
|
serviceFake.UpdateSimulation(bikesOccupied.Response);
|
|||
|
}
|
|||
|
|
|||
|
// Check bluetooth and location permission and states
|
|||
|
ActionText = AppResources.ActivityTextMyBikesCheckBluetoothState;
|
|||
|
|
|||
|
if (bikesOccupied.Response.FirstOrDefault(x => x is BikeInfo btBike) != null
|
|||
|
&& RuntimePlatform == Device.Android)
|
|||
|
{
|
|||
|
// Check location permission
|
|||
|
var status = await Permissions.CheckPermissionStatusAsync<LocationPermission>();
|
|||
|
if (status != PermissionStatus.Granted)
|
|||
|
{
|
|||
|
var permissionResult = await Permissions.RequestPermissionAsync<LocationPermission>();
|
|||
|
|
|||
|
if (permissionResult != PermissionStatus.Granted)
|
|||
|
{
|
|||
|
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(bikesOccupied.Response);
|
|||
|
|
|||
|
await OnAppearing(() => UpdateTask());
|
|||
|
|
|||
|
ActionText = "";
|
|||
|
IsIdle = true;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// Open permissions dialog.
|
|||
|
Permissions.OpenAppSettings();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Location state
|
|||
|
if (Geolocation.IsGeolcationEnabled == false)
|
|||
|
{
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.MessageTitleHint,
|
|||
|
AppResources.MessageBikesManagementLocationActivation,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
|
|||
|
BikeCollection.Update(bikesOccupied.Response);
|
|||
|
|
|||
|
await OnAppearing(() => UpdateTask());
|
|||
|
|
|||
|
ActionText = "";
|
|||
|
IsIdle = true;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// Bluetooth state
|
|||
|
if (await BluetoothLE.GetBluetoothState() != BluetoothState.On)
|
|||
|
{
|
|||
|
await ViewService.DisplayAlert(
|
|||
|
AppResources.MessageTitleHint,
|
|||
|
AppResources.MessageBikesManagementBluetoothActivation,
|
|||
|
AppResources.MessageAnswerOk);
|
|||
|
|
|||
|
BikeCollection.Update(bikesOccupied.Response);
|
|||
|
|
|||
|
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<MyBikesPageViewModel>().Error("Getting bluetooth state failed. {Exception}", exception);
|
|||
|
locksInfoTdo = new List<LockInfoTdo>();
|
|||
|
}
|
|||
|
|
|||
|
var locksInfo = lockIdList.UpdateById(locksInfoTdo);
|
|||
|
|
|||
|
BikeCollection.Update(bikesOccupied.Response.UpdateLockInfo(locksInfo));
|
|||
|
|
|||
|
await OnAppearing(() => UpdateTask());
|
|||
|
|
|||
|
ActionText = "";
|
|||
|
IsIdle = true;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// True if any action can be performed (request and cancel request)
|
|||
|
/// </summary>
|
|||
|
public override bool IsIdle
|
|||
|
{
|
|||
|
get => base.IsIdle;
|
|||
|
set
|
|||
|
{
|
|||
|
if (value == base.IsIdle)
|
|||
|
return;
|
|||
|
|
|||
|
Log.ForContext<BikesViewModel>().Debug($"Switch value of {nameof(IsIdle)} to {value}.");
|
|||
|
base.IsIdle = value;
|
|||
|
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsNoBikesOccupiedVisible)));
|
|||
|
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(NoBikesOccupiedText)));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Create task which updates my bike view model.</summary>
|
|||
|
private void UpdateTask()
|
|||
|
{
|
|||
|
// Start task which periodically updates pins.
|
|||
|
PostAction(
|
|||
|
unused =>
|
|||
|
{
|
|||
|
ActionText = "Aktualisiere...";
|
|||
|
IsConnected = IsConnectedDelegate();
|
|||
|
},
|
|||
|
null);
|
|||
|
|
|||
|
var result = ConnectorFactory(IsConnected).Query.GetBikesOccupiedAsync().Result;
|
|||
|
|
|||
|
var bikes = result.Response;
|
|||
|
|
|||
|
var exception = result.Exception;
|
|||
|
if (exception != null)
|
|||
|
{
|
|||
|
Log.ForContext<MyBikesPageViewModel>().Error("Getting bikes occupied in polling context failed with exception {Exception}.", exception);
|
|||
|
}
|
|||
|
|
|||
|
PostAction(
|
|||
|
unused =>
|
|||
|
{
|
|||
|
BikeCollection.Update(bikes); // Updating collection leads to update of GUI.
|
|||
|
Exception = result.Exception;
|
|||
|
ActionText = string.Empty;
|
|||
|
},
|
|||
|
null);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|