2022-09-22 20:58:30 +02:00
using System ;
2022-08-30 15:42:25 +02:00
using System.Collections.Generic ;
2021-05-13 20:03:07 +02:00
using System.Collections.Specialized ;
using System.ComponentModel ;
2022-08-30 15:42:25 +02:00
using System.Linq ;
2021-05-13 20:03:07 +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-05-13 20:03:07 +02:00
using TINK.Model.Connector ;
2022-08-30 15:42:25 +02:00
using TINK.Model.Device ;
using TINK.Model.Station ;
2021-05-13 20:03:07 +02:00
using TINK.Model.User ;
2022-08-30 15:42:25 +02:00
using TINK.MultilingualResources ;
2021-05-13 20:03:07 +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 ;
2023-02-22 14:03:35 +01:00
using Xamarin.Essentials ;
2022-08-30 15:42:25 +02:00
using Xamarin.Forms ;
2023-01-18 14:22:51 +01:00
using Command = Xamarin . Forms . Command ;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.MyBikes
{
2022-09-06 16:08:19 +02:00
public class MyBikesPageViewModel : BikesViewModel , INotifyCollectionChanged , INotifyPropertyChanged
{
/// <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 ) ) ) ;
}
}
2023-02-22 14:03:35 +01:00
/// <summary>
/// Holds what should be executed on pull to refresh
/// </summary>
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="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="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 MyBikesPageViewModel (
User p_oUser ,
ILocationPermission permissions ,
IBluetoothLE bluetoothLE ,
string runtimPlatform ,
Func < bool > isConnectedDelegate ,
Func < bool , IConnector > connectorFactory ,
IGeolocation geolocation ,
ILocksService lockService ,
IEnumerable < IStation > stations ,
PollingParameters p_oPolling ,
Action < SendOrPostCallback , object > postAction ,
ISmartDevice smartDevice ,
IViewService viewService ,
Action < string > openUrlInBrowser ) : base ( p_oUser , permissions , bluetoothLE , runtimPlatform , isConnectedDelegate , connectorFactory , geolocation , lockService , p_oPolling , postAction , smartDevice , viewService , openUrlInBrowser , ( ) = > new MyBikeInUseStateInfoProvider ( ) )
{
CollectionChanged + = ( sender , eventargs ) = >
{
OnPropertyChanged ( new PropertyChangedEventArgs ( nameof ( IsNoBikesOccupiedVisible ) ) ) ;
OnPropertyChanged ( new PropertyChangedEventArgs ( nameof ( NoBikesOccupiedText ) ) ) ;
} ;
Stations = stations ? ? throw new ArgumentException ( nameof ( stations ) ) ;
2023-01-18 14:22:51 +01:00
RefreshCommand = new Command ( async ( ) = > {
IsRefreshing = false ;
2023-02-22 14:03:35 +01:00
await OnAppearingOrRefresh ( ) ;
2023-01-18 14:22:51 +01:00
} ) ;
2022-09-06 16:08:19 +02:00
}
/// <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
2023-01-18 14:22:51 +01:00
? string . Format ( AppResources . MarkingMyBikesNoBikesReservedRented , ActiveUser ? . Mail )
2022-09-06 16:08:19 +02:00
: string . Empty ;
}
}
/// <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
{
2023-01-18 14:22:51 +01:00
IsIdle = false ;
2023-02-22 14:03:35 +01:00
IsConnected = IsConnectedDelegate ( ) ;
2022-09-06 16:08:19 +02:00
// Get my bikes from COPRI
Log . ForContext < MyBikesPageViewModel > ( ) . Information ( "User request to show page MyBikes/ page re-appearing" ) ;
ActionText = AppResources . ActivityTextMyBikesLoadingBikes ;
2023-02-22 14:03:35 +01:00
// Stop polling before getting bikes info.
await m_oViewUpdateManager . StopUpdatePeridically ( ) ;
2022-09-06 16:08:19 +02:00
var bikesOccupied = await ConnectorFactory ( IsConnected ) . Query . GetBikesOccupiedAsync ( ) ;
Exception = bikesOccupied . Exception ; // Update communication error from query for bikes occupied.
var lockIdList = bikesOccupied . Response
. 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 ( bikesOccupied . Response ) ;
}
// Check bluetooth and location permission and states
ActionText = AppResources . ActivityTextCheckBluetoothState ;
2022-09-22 20:58:30 +02:00
if ( bikesOccupied . Response . FirstOrDefault ( x = > x is BikeInfo btBike ) ! = null
2022-09-06 16:08:19 +02:00
& & RuntimePlatform = = Device . Android )
{
// Check location permission
var status = await PermissionsService . CheckStatusAsync ( ) ;
if ( status ! = Status . Granted )
{
var permissionResult = await PermissionsService . RequestAsync ( ) ;
if ( permissionResult ! = Status . 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 , Stations ) ;
await OnAppearing ( ( ) = > UpdateTask ( ) ) ;
2023-02-22 14:03:35 +01:00
ActionText = string . Empty ;
2022-09-06 16:08:19 +02:00
IsIdle = true ;
return ;
}
// Open permissions dialog.
PermissionsService . OpenAppSettings ( ) ;
}
}
// Location state
if ( Geolocation . IsGeolcationEnabled = = false )
{
await ViewService . DisplayAlert (
AppResources . MessageTitleHint ,
AppResources . MessageBikesManagementLocationActivation ,
AppResources . MessageAnswerOk ) ;
BikeCollection . Update ( bikesOccupied . Response , Stations ) ;
await OnAppearing ( ( ) = > UpdateTask ( ) ) ;
2023-02-22 14:03:35 +01:00
ActionText = string . Empty ;
2022-09-06 16:08:19 +02:00
IsIdle = true ;
return ;
}
// Bluetooth state
if ( await BluetoothService . GetBluetoothState ( ) ! = BluetoothState . On )
{
await ViewService . DisplayAlert (
AppResources . MessageTitleHint ,
AppResources . MessageBikesManagementBluetoothActivation ,
AppResources . MessageAnswerOk ) ;
BikeCollection . Update ( bikesOccupied . Response , Stations ) ;
await OnAppearing ( ( ) = > UpdateTask ( ) ) ;
2023-02-22 14:03:35 +01:00
ActionText = string . Empty ;
2022-09-06 16:08:19 +02:00
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 ) , Stations ) ;
await OnAppearing ( ( ) = > UpdateTask ( ) ) ;
2023-02-22 14:03:35 +01:00
ActionText = string . Empty ;
2022-09-06 16:08:19 +02:00
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 ) ) ) ;
2022-09-22 20:58:30 +02:00
base . OnPropertyChanged ( new PropertyChangedEventArgs ( nameof ( FlyoutBehavior ) ) ) ; // Hide flyout menu if app is busy. Prevents especially navigation on returning bike.
2022-09-06 16:08:19 +02:00
}
}
2022-09-22 20:58:30 +02:00
/// <summary>
/// Determines if flyout menu is available or not.
/// </summary>
public FlyoutBehavior FlyoutBehavior = > base . IsIdle ? FlyoutBehavior . Flyout : FlyoutBehavior . Disabled ;
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 . 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 , Stations ) ; // Updating collection leads to update of GUI.
Exception = result . Exception ;
ActionText = string . Empty ;
} ,
null ) ;
}
}
2021-05-13 20:03:07 +02:00
}