2022-10-03 17:55:10 +02:00
using System ;
using System.ComponentModel ;
2022-01-22 18:28:01 +01:00
using System.Threading.Tasks ;
2022-10-03 17:55:10 +02:00
using Serilog ;
using TINK.Model ;
using TINK.Model.Bikes ;
using TINK.Model.Connector ;
using TINK.Model.Services.CopriApi ;
using TINK.ViewModel.Info ;
2022-08-30 15:42:25 +02:00
using Xamarin.Forms ;
2021-06-26 20:57:55 +02:00
2021-05-13 20:16:41 +02:00
namespace TINK.ViewModel.Contact
{
2022-09-06 16:08:19 +02:00
public class FeesAndBikesPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged ;
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
2023-05-09 08:47:52 +02:00
/// <summary> Holds value whether site caching is on or off.</summary>
2022-09-06 16:08:19 +02:00
bool IsSiteCachingOn { get ; }
2021-05-13 20:16:41 +02:00
2022-10-03 17:55:10 +02:00
/// <summary>
/// Relative path to fees resources of empty if value was not yet querried from backend.
/// </summary>
private string FeesResourcePath { get ; set ; }
2022-01-22 18:28:01 +01:00
2022-10-03 17:55:10 +02:00
/// <summary>
/// Relative path to bike info resources of empty if value was not yet querried from backend.
/// </summary>
private string BikesResourcePath { get ; set ; }
2022-01-22 18:28:01 +01:00
2022-10-03 17:55:10 +02:00
private bool _IsIdle = false ;
/// <summary>
/// Is true if no action is pending, false otherwise.
/// </summary>
public bool IsIdle
{
get = > _IsIdle ;
private set
{
if ( _IsIdle = = value )
{
// Nothing to do.
return ;
}
_IsIdle = value ;
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( IsIdle ) ) ) ;
return ;
}
}
/// <summary>
2023-04-19 12:14:14 +02:00
/// Object to query resources urls object from backend if required.
2022-10-03 17:55:10 +02:00
/// This object is used to update resources path values <see cref="FeesResourcePath"/>, and <see cref="BikesResourcePath"/> from.
/// </summary>
private Func < IQuery > QueryProvider { get ; }
/// <summary>
/// Action to update shared resources urls object exposed by main model.
/// </summary>
private Action < IResourceUrls > UpdateUrlsAction { get ; }
2022-01-22 18:28:01 +01:00
2022-09-06 16:08:19 +02:00
/// <summary> Constructs view model.</summary>
/// <param name="isSiteCachingOn">Set of user permissions</param>
2023-05-09 08:47:52 +02:00
/// <param name="resourceProvider">Delegate to get embedded html resource. Used as fallback if download from web page does not work and cache is empty.</param>
2023-04-19 12:14:14 +02:00
/// <param name="query">Object to query resources path values if required.</param>
2022-09-06 16:08:19 +02:00
public FeesAndBikesPageViewModel (
string hostName ,
string feesResourcePath ,
string bikesResourcePath ,
2022-10-03 17:55:10 +02:00
bool isSiteCachingOn ,
Func < IQuery > queryProvider ,
Action < IResourceUrls > updateUrlsAction )
2022-09-06 16:08:19 +02:00
{
HostName = hostName ;
FeesResourcePath = feesResourcePath ;
BikesResourcePath = bikesResourcePath ;
IsSiteCachingOn = isSiteCachingOn ;
2022-10-03 17:55:10 +02:00
QueryProvider = queryProvider ;
UpdateUrlsAction = updateUrlsAction ;
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Holds the name of the host.</summary>
private string HostName { get ; }
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Called when page is shown. </summary>
public async void OnAppearing ( )
{
2022-10-03 17:55:10 +02:00
// Get resource urls object from backend.
async Task < IResourceUrls > GetUrls ( )
{
Result < BikeCollection > bikes ;
try
{
bikes = await QueryProvider ( ) . GetBikesAsync ( ) ;
}
catch ( Exception ex )
{
Log . ForContext < FeesAndBikesPageViewModel > ( ) . Error ( $"Getting resource urls from COPRI failed. {ex.Message}" ) ;
return new ResourceUrls ( ) ;
}
return bikes ? . GeneralData ? . ResourceUrls ? ? new ResourceUrls ( ) ;
}
// Set state to busy.
IsIdle = false ;
if ( string . IsNullOrEmpty ( FeesResourcePath ) )
{
var urls = await GetUrls ( ) ;
FeesResourcePath = urls . FeesResourcePath ;
BikesResourcePath = urls . BikesResourcePath ;
UpdateUrlsAction ( urls ) ; // Update main model to prevent duplicate queries.
}
2022-09-06 16:08:19 +02:00
RentBikeText = new HtmlWebViewSource
{
Html = ! string . IsNullOrEmpty ( FeesResourcePath )
2022-10-03 17:55:10 +02:00
? await ViewModelHelper . GetSource ( $"https://{HostName}/{FeesResourcePath}" , IsSiteCachingOn )
2022-09-06 16:08:19 +02:00
: await Task . FromResult ( ViewModelHelper . FromBody ( "No fees resource available. Resource path is null or empty." ) )
} ;
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
TypesOfBikesText = new HtmlWebViewSource
{
Html = ! string . IsNullOrEmpty ( BikesResourcePath )
? await ViewModelHelper . GetSource ( $"https://{HostName}/{BikesResourcePath}" /*"site/bike_info.html"*/ , IsSiteCachingOn )
: await Task . FromResult ( ViewModelHelper . FromBody ( "No bikes instruction resource available. Resource path is null or empty." ) )
} ;
2022-10-03 17:55:10 +02:00
// Set state to idle.
IsIdle = true ;
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
private HtmlWebViewSource rentBikeText ;
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
private HtmlWebViewSource typesOfBikesText ;
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
public HtmlWebViewSource RentBikeText
{
get = > rentBikeText ;
set
{
rentBikeText = value ;
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( RentBikeText ) ) ) ;
}
}
2021-05-13 20:16:41 +02:00
2022-09-06 16:08:19 +02:00
public HtmlWebViewSource TypesOfBikesText
{
get = > typesOfBikesText ;
set
{
typesOfBikesText = value ;
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( TypesOfBikesText ) ) ) ;
}
}
}
2021-05-13 20:16:41 +02:00
}