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

289 lines
9.1 KiB
C#

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Serilog;
using ShareeBike.Model;
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
using Xamarin.Essentials;
using Xamarin.Forms;
using ShareeBike.Model.Connector;
using ShareeBike.ViewModel.Contact;
namespace ShareeBike.ViewModel.LegalInformation
{
/// <summary> Manges the tabbed info page. </summary>
public class LegalInformationPageViewModel : INotifyPropertyChanged
{
/// <summary> Fired whenever a property changed.</summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary> Holds the name of the host.</summary>
private string HostName { get; }
/// <summary> Holds value whether site caching is on or off.</summary>
bool IsSiteCachingOn { get; }
/// <summary>
/// Relative path to agb resources of empty if value was not yet querried from backend.
/// </summary>
private string GtcResourcePath { get; set; }
/// <summary>
/// Relative path to privacy resources of empty if value was not yet querried from backend.
/// </summary>
private string PrivacyResourcePath { get; set; }
/// <summary>
/// Relative path to impress resources of empty if value was not yet querried from backend.
/// </summary>
private string ImpressResourcePath { 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="GtcResourcePath"/>, <see cref="PrivacyResourcePath"/> and <see cref="ImpressResourcePath"/> 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 Info view model</summary>
/// <param name="hostName">Name of the host to get html resources from.</param>
/// <param name="isSiteCachingOn">Holds value whether site caching is on or off.</param>
/// <param name="gtcResourcePath"> Agb resource path received from backend.</param>
/// <param name="privacyResourcePath"> PrivacyHtml resource path received from backend.</param>
/// <param name="impressResourcePath"> ImpressHtml resource path received from backend.</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="queryProvider">Object to query resources urls object from backend if required.</param>
/// <param name="updateUrlsAction">Action to update shared resources urls object</param>
public LegalInformationPageViewModel(
string hostName,
string gtcResourcePath,
string privacyResourcePath,
string impressResourcePath,
bool isSiteCachingOn,
Func<string, string> resourceProvider,
Func<IQuery> queryProvider,
Action<IResourceUrls> updateUrlsAction)
{
HostName = hostName;
GtcResourcePath = gtcResourcePath;
PrivacyResourcePath = privacyResourcePath;
ImpressResourcePath = impressResourcePath;
IsSiteCachingOn = isSiteCachingOn;
QueryProvider = queryProvider;
UpdateUrlsAction = updateUrlsAction;
GtcHtml = new HtmlWebViewSource { Html = "<html>Loading...</html>" };
ResourceProvider = resourceProvider
?? throw new ArgumentException($"Can not instantiate {typeof(LegalInformationPageViewModel)}-object. No resource provider centered.");
}
// Get resource urls object from backend.
public async Task<IResourceUrls> GetUrls()
{
Result<StationsAndBikesContainer> stations;
try
{
stations = await QueryProvider().GetBikesAndStationsAsync();
}
catch (Exception ex)
{
Log.ForContext<LegalInformationPageViewModel>().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;
// Check if urls to show info from are already known.
if (string.IsNullOrEmpty(PrivacyResourcePath))
{
var urls = await GetUrls();
PrivacyResourcePath = urls.PrivacyResourcePath;
ImpressResourcePath = urls.ImpressResourcePath;
GtcResourcePath = urls.GtcResourcePath;
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>";
// Gets privacy info from server.
async Task<HtmlWebViewSource> GetPrivacy()
{
string GetUriText()
=> $"https://{HostName}/{PrivacyResourcePath}";
if (string.IsNullOrEmpty(PrivacyResourcePath))
{
// Information to access resource is missing
return new HtmlWebViewSource
{
Html = headerString + await Task.FromResult(ViewModelHelper.FromBody("No privacy resource available. Resource path is null or empty."))
};
}
Log.Debug($"Request to open url {GetUriText()} to get privacy info.");
return new HtmlWebViewSource
{
Html = headerString + await ViewModelHelper.GetSource(
GetUriText(), // "site/privacy.html"
IsSiteCachingOn)
};
}
// Gets impress info from server.
async Task<HtmlWebViewSource> GetImpress()
{
string GetUriText()
=> $"https://{HostName}/{ImpressResourcePath}";
if (string.IsNullOrEmpty(ImpressResourcePath))
{
// Information to access resource is missing
return new HtmlWebViewSource
{
Html = headerString + await Task.FromResult(ViewModelHelper.FromBody("No impress resource available. Resource path is null or empty."))
};
}
Log.Debug($"Request to open url {GetUriText()} to get impress info.");
return new HtmlWebViewSource
{
Html = headerString + await ViewModelHelper.GetSource(
GetUriText(), // "site/privacy.html"
IsSiteCachingOn)
};
}
GtcHtml = await GetGtc(HostName, GtcResourcePath, IsSiteCachingOn);
PrivacyHtml = await GetPrivacy();
ImpressHtml = await GetImpress();
// Set state to idle.
IsIdle = true;
}
/// <summary> Gets the AGBs</summary>
/// <param name="resourceProvider"></param>
/// <returns> AGBs</returns>
public static async Task<HtmlWebViewSource> GetGtc(
string hostName,
string gtcResourcePath,
bool isSiteCachingOn)
{
string GetUriText()
=> $"https://{hostName}/{gtcResourcePath}";
//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>";
if (string.IsNullOrEmpty(gtcResourcePath))
{
return new HtmlWebViewSource
{
Html = headerString + await Task.FromResult(ViewModelHelper.FromBody("No terms resource available. Resource path is null or empty."))
};
}
Log.Debug($"Request to open url {GetUriText()} to get AGB infos.");
return new HtmlWebViewSource
{
Html = headerString + await ViewModelHelper.GetSource(
GetUriText(), // "agb.html"
isSiteCachingOn)
};
}
/// <summary> Gets the platfrom specific prefix. </summary>
private Func<string, string> ResourceProvider { get; set; }
/// <summary> Gets the app related information (app version and licenses). </summary>
public HtmlWebViewSource AppHtml => new HtmlWebViewSource
{
Html = ResourceProvider("HtmlResouces.V02.InfoLicenses.html")
.Replace("CURRENT_VERSION_ShareeBikeAPP", DependencyService.Get<IAppInfo>().Version.ToString())
.Replace("ACTIVE_APPNAME", AppInfo.Name)
.Replace("APPSUPPORTMAILADDRESS", ContactPageViewModel.APPSUPPORTMAILADDRESS)
};
/// <summary> PrivacyHtml text.</summary>
private HtmlWebViewSource impressHtml;
/// <summary> Gets the privacy related information. </summary>
public HtmlWebViewSource ImpressHtml
{
get => impressHtml;
set
{
impressHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ImpressHtml)));
}
}
/// <summary> Agb information text.</summary>
private HtmlWebViewSource gtcHtml;
/// <summary> PrivacyHtml text.</summary>
private HtmlWebViewSource privacyHtml;
/// <summary> Agb information text.</summary>
public HtmlWebViewSource GtcHtml
{
get => gtcHtml;
set
{
gtcHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(GtcHtml)));
}
}
/// <summary> Agb information text.</summary>
public HtmlWebViewSource PrivacyHtml
{
get => privacyHtml;
set
{
privacyHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PrivacyHtml)));
}
}
}
}