sharee.bike-App/SharedBusinessLogic/ViewModel/Help/HelpPageViewModel.cs
2024-04-09 12:53:23 +02:00

187 lines
5.6 KiB
C#

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Services.CopriApi;
using Xamarin.Forms;
namespace ShareeBike.ViewModel.Help
{
public class HelpPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Holds the current ui two letter ISO language name.
/// </summary>
private string UiIsoLanguageName { get; }
/// <summary> Holds value whether site caching is on or off.</summary>
bool IsSiteCachingOn { get; }
/// <summary>
/// Relative path to fees resources of empty if value was not yet querried from backend.
/// </summary>
private string TariffsResourcePath { get; set; }
/// <summary>
/// Relative path to bike info resources of empty if value was not yet querried from backend.
/// </summary>
private string ManualResourcePath { get; set; }
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>
/// Object to query resources urls object from backend if required.
/// This object is used to update resources path values <see cref="TariffsResourcePath"/>, and <see cref="ManualResourcePath"/> 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; }
/// <summary> Constructs view model.</summary>
/// <param name="isSiteCachingOn">Set of user permissions</param>
/// <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>
/// <param name="query">Object to query resources path values if required.</param>
public HelpPageViewModel(
string hostName,
string tariffsResourcePath,
string manualResourcePath,
bool isSiteCachingOn,
string uiIsoLangugageName,
Func<IQuery> queryProvider,
Action<IResourceUrls> updateUrlsAction)
{
HostName = hostName;
TariffsResourcePath = tariffsResourcePath;
ManualResourcePath = manualResourcePath;
IsSiteCachingOn = isSiteCachingOn;
QueryProvider = queryProvider;
UpdateUrlsAction = updateUrlsAction;
UiIsoLanguageName = uiIsoLangugageName;
}
/// <summary> Holds the name of the host.</summary>
private string HostName { get; }
// Get resource urls object from backend.
public async Task<IResourceUrls> GetUrls()
{
Result<StationsAndBikesContainer> stations;
try
{
stations = await QueryProvider().GetBikesAndStationsAsync();
}
catch (Exception ex)
{
Log.ForContext<HelpPageViewModel>().Error($"Getting resource urls from COPRI failed. {ex.Message}");
return new ResourceUrls();
}
return stations?.GeneralData?.ResourceUrls ?? new ResourceUrls();
}
/// <summary> Called when page is shown. </summary>
public async void OnAppearing()
{
// Set state to busy.
IsIdle = false;
if (string.IsNullOrEmpty(TariffsResourcePath))
{
var urls = await GetUrls();
TariffsResourcePath = urls.TariffsResourcePath;
ManualResourcePath = urls.ManualResourcePath;
UpdateUrlsAction(urls); // Update main model to prevent duplicate queries.
}
//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>";
ManualHtml = new HtmlWebViewSource
{
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."))
};
TariffsHtml = new HtmlWebViewSource
{
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."))
};
FaqHtml = new HtmlWebViewSource
{
Html = UiIsoLanguageName == "de"
? await ViewModelHelper.GetSource($"https://sharee.bike/faqHtml/fahrrad-nutzung/", IsSiteCachingOn)
: await ViewModelHelper.GetSource($"https://sharee.bike/faqHtml/bike-usage/", IsSiteCachingOn)
};
// Set state to idle.
IsIdle = true;
}
private HtmlWebViewSource manualHtml;
private HtmlWebViewSource tariffsHtml;
private HtmlWebViewSource faqHtml;
public HtmlWebViewSource ManualHtml
{
get => manualHtml;
set
{
manualHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ManualHtml)));
}
}
public HtmlWebViewSource TariffsHtml
{
get => tariffsHtml;
set
{
tariffsHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TariffsHtml)));
}
}
public HtmlWebViewSource FaqHtml
{
get => faqHtml;
set
{
faqHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FaqHtml)));
}
}
}
}