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 ;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model ;
using ShareeBike.Model.Connector ;
using ShareeBike.Model.Services.CopriApi ;
2022-08-30 15:42:25 +02:00
using Xamarin.Forms ;
2021-06-26 20:57:55 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.ViewModel.Help
2021-05-13 20:16:41 +02:00
{
2023-11-21 15:26:57 +01:00
public class HelpPageViewModel : INotifyPropertyChanged
2022-09-06 16:08:19 +02:00
{
public event PropertyChangedEventHandler PropertyChanged ;
2021-05-13 20:16:41 +02:00
2023-08-31 12:31:38 +02:00
/// <summary>
/// Holds the current ui two letter ISO language name.
/// </summary>
private string UiIsoLanguageName { get ; }
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>
2023-11-21 15:26:57 +01:00
private string TariffsResourcePath { 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>
2023-11-21 15:26:57 +01:00
private string ManualResourcePath { 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.
2023-11-21 15:26:57 +01:00
/// This object is used to update resources path values <see cref="TariffsResourcePath"/>, and <see cref="ManualResourcePath"/> from.
2022-10-03 17:55:10 +02:00
/// </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>
2023-11-21 15:26:57 +01:00
public HelpPageViewModel (
2022-09-06 16:08:19 +02:00
string hostName ,
2023-11-21 15:26:57 +01:00
string tariffsResourcePath ,
string manualResourcePath ,
2022-10-03 17:55:10 +02:00
bool isSiteCachingOn ,
2023-08-31 12:31:38 +02:00
string uiIsoLangugageName ,
2022-10-03 17:55:10 +02:00
Func < IQuery > queryProvider ,
Action < IResourceUrls > updateUrlsAction )
2022-09-06 16:08:19 +02:00
{
HostName = hostName ;
2023-11-21 15:26:57 +01:00
TariffsResourcePath = tariffsResourcePath ;
ManualResourcePath = manualResourcePath ;
2022-09-06 16:08:19 +02:00
IsSiteCachingOn = isSiteCachingOn ;
2022-10-03 17:55:10 +02:00
QueryProvider = queryProvider ;
UpdateUrlsAction = updateUrlsAction ;
2023-08-31 12:31:38 +02:00
UiIsoLanguageName = uiIsoLangugageName ;
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
2023-11-21 15:26:57 +01:00
// Get resource urls object from backend.
public async Task < IResourceUrls > GetUrls ( )
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
Result < StationsAndBikesContainer > stations ;
try
2022-10-03 17:55:10 +02:00
{
2023-11-21 15:26:57 +01:00
stations = await QueryProvider ( ) . GetBikesAndStationsAsync ( ) ;
2022-10-03 17:55:10 +02:00
}
2023-11-21 15:26:57 +01:00
catch ( Exception ex )
{
Log . ForContext < HelpPageViewModel > ( ) . Error ( $"Getting resource urls from COPRI failed. {ex.Message}" ) ;
return new ResourceUrls ( ) ;
}
return stations ? . GeneralData ? . ResourceUrls ? ? new ResourceUrls ( ) ;
}
2022-10-03 17:55:10 +02:00
2023-11-21 15:26:57 +01:00
/// <summary> Called when page is shown. </summary>
public async void OnAppearing ( )
{
2022-10-03 17:55:10 +02:00
// Set state to busy.
IsIdle = false ;
2023-11-21 15:26:57 +01:00
if ( string . IsNullOrEmpty ( TariffsResourcePath ) )
2022-10-03 17:55:10 +02:00
{
var urls = await GetUrls ( ) ;
2023-11-21 15:26:57 +01:00
TariffsResourcePath = urls . TariffsResourcePath ;
ManualResourcePath = urls . ManualResourcePath ;
2022-10-03 17:55:10 +02:00
UpdateUrlsAction ( urls ) ; // Update main model to prevent duplicate queries.
}
2023-09-14 12:28:59 +02:00
//set font size for html content (necessary for iOS)
string headerString = "<header><meta name='viewport' content='width=device-width, intial-scale=2.0, maximum-scale=5.0, minimum-scale=1.0, user-scalable=no'></header>" ;
2023-11-21 15:26:57 +01:00
ManualHtml = new HtmlWebViewSource
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
Html = ! string . IsNullOrEmpty ( ManualResourcePath )
? headerString + await ViewModelHelper . GetSource ( $"https://{HostName}/{ManualResourcePath}" /*"site/bike_info_....html"*/ , IsSiteCachingOn )
: headerString + await Task . FromResult ( ViewModelHelper . FromBody ( "No manual resource available. Resource path is null or empty." ) )
2022-09-06 16:08:19 +02:00
} ;
2021-05-13 20:16:41 +02:00
2023-11-21 15:26:57 +01:00
TariffsHtml = new HtmlWebViewSource
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
Html = ! string . IsNullOrEmpty ( TariffsResourcePath )
? headerString + await ViewModelHelper . GetSource ( $"https://{HostName}/{TariffsResourcePath}" /*"site/tarif_info_....html"*/ , IsSiteCachingOn )
: headerString + await Task . FromResult ( ViewModelHelper . FromBody ( "No tariffs resource available. Resource path is null or empty." ) )
2022-09-06 16:08:19 +02:00
} ;
2022-10-03 17:55:10 +02:00
2023-11-21 15:26:57 +01:00
FaqHtml = new HtmlWebViewSource
2023-08-31 12:31:38 +02:00
{
Html = UiIsoLanguageName = = "de"
2023-11-21 15:26:57 +01:00
? await ViewModelHelper . GetSource ( $"https://sharee.bike/faqHtml/fahrrad-nutzung/" , IsSiteCachingOn )
: await ViewModelHelper . GetSource ( $"https://sharee.bike/faqHtml/bike-usage/" , IsSiteCachingOn )
2023-08-31 12:31:38 +02:00
} ;
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
2023-11-21 15:26:57 +01:00
private HtmlWebViewSource manualHtml ;
2021-05-13 20:16:41 +02:00
2023-11-21 15:26:57 +01:00
private HtmlWebViewSource tariffsHtml ;
2021-05-13 20:16:41 +02:00
2023-11-21 15:26:57 +01:00
private HtmlWebViewSource faqHtml ;
2023-08-31 12:31:38 +02:00
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource ManualHtml
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
get = > manualHtml ;
2022-09-06 16:08:19 +02:00
set
{
2023-11-21 15:26:57 +01:00
manualHtml = value ;
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( ManualHtml ) ) ) ;
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:16:41 +02:00
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource TariffsHtml
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
get = > tariffsHtml ;
2022-09-06 16:08:19 +02:00
set
{
2023-11-21 15:26:57 +01:00
tariffsHtml = value ;
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( TariffsHtml ) ) ) ;
2022-09-06 16:08:19 +02:00
}
}
2023-08-31 12:31:38 +02:00
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource FaqHtml
2023-08-31 12:31:38 +02:00
{
2023-11-21 15:26:57 +01:00
get = > faqHtml ;
2023-08-31 12:31:38 +02:00
set
{
2023-11-21 15:26:57 +01:00
faqHtml = value ;
PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( FaqHtml ) ) ) ;
2023-08-31 12:31:38 +02:00
}
}
2022-09-06 16:08:19 +02:00
}
2021-05-13 20:16:41 +02:00
}