sharee.bike-App/TINKLib/ViewModel/Info/InfoPageViewModel.cs

207 lines
7.7 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
using System.ComponentModel;
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Serilog;
2021-05-13 20:03:07 +02:00
using TINK.Model.Device;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace TINK.ViewModel.Info
{
/// <summary> Manges the tabbed info page. </summary>
2022-01-22 18:28:01 +01:00
public class InfoPageViewModel : INotifyPropertyChanged
2021-05-13 20:03:07 +02:00
{
/// <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 wether site caching is on or off.</summary>
bool IsSiteCachingOn { get; }
2022-01-22 18:28:01 +01:00
private string AgbResourcePath { get; }
private string PrivacyResourcePath { get; }
private string ImpressResourcePath { get; }
2022-08-30 15:42:25 +02:00
/// <summary>
/// Holds the current ui two letter ISO language name.
/// </summary>
private string UiIsoLanguageName { get; }
2021-05-13 20:03:07 +02:00
/// <summary> Constructs Info view model</summary>
/// <param name="isSiteCachingOn">Holds value wether site caching is on or off.</param>
2022-08-30 15:42:25 +02:00
/// <param name="agbResourcePath"> Agb resouce path received from backend.</param>
/// <param name="privacyResourcePath"> Privacy resouce path received from backend.</param>
/// <param name="impressResourcePath"> Impress resouce path received from backend.</param>
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
2021-05-13 20:03:07 +02:00
/// <param name="resourceProvider">Delegate to get an an embedded html ressource. Used as fallback if download from web page does not work and cache is empty.</param>
2022-01-22 18:28:01 +01:00
public InfoPageViewModel(
2021-05-13 20:03:07 +02:00
string hostName,
2022-01-22 18:28:01 +01:00
string agbResourcePath,
string privacyResourcePath,
string impressResourcePath,
bool isSiteCachingOn,
2022-08-30 15:42:25 +02:00
string uiIsoLangugageName,
2021-05-13 20:03:07 +02:00
Func<string, string> resourceProvider)
{
HostName = hostName;
2022-01-22 18:28:01 +01:00
AgbResourcePath = agbResourcePath;
2022-08-30 15:42:25 +02:00
PrivacyResourcePath = privacyResourcePath;
ImpressResourcePath = impressResourcePath;
2021-05-13 20:03:07 +02:00
IsSiteCachingOn = isSiteCachingOn;
2022-08-30 15:42:25 +02:00
UiIsoLanguageName = uiIsoLangugageName;
2021-05-13 20:03:07 +02:00
InfoAgb = new HtmlWebViewSource { Html = "<html>Loading...</html>" };
ResourceProvider = resourceProvider
2022-01-22 18:28:01 +01:00
?? throw new ArgumentException($"Can not instantiate {typeof(InfoPageViewModel)}-object. No ressource provider availalbe.");
2021-05-13 20:03:07 +02:00
}
/// <summary> Called when page is shown. </summary>
public async void OnAppearing()
{
2022-08-30 15:42:25 +02:00
// Gets privacy info from server.
async Task<HtmlWebViewSource> GetInfoPrivacy()
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
string GetUriText()
=> $"https://{HostName}/{PrivacyResourcePath}";
if (string.IsNullOrEmpty(PrivacyResourcePath))
{
// Information to access ressource is missing
return new HtmlWebViewSource
{
Html = 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 = await ViewModelHelper.GetSource(
GetUriText(), // "site/privacy.html"
IsSiteCachingOn)
};
}
2021-05-13 20:03:07 +02:00
2022-08-30 15:42:25 +02:00
// Gets impress info from server.
async Task<HtmlWebViewSource> GetImpressum()
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
string GetUriText()
=> $"https://{HostName}/{ImpressResourcePath}";
if (string.IsNullOrEmpty(ImpressResourcePath))
{
// Information to access ressource is missing
return new HtmlWebViewSource
{
Html = 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 = await ViewModelHelper.GetSource(
GetUriText(), // "site/privacy.html"
IsSiteCachingOn)
};
}
InfoAgb = await GetAgb(HostName, AgbResourcePath, IsSiteCachingOn, UiIsoLanguageName);
InfoPrivacy = await GetInfoPrivacy();
InfoImpressum = await GetImpressum();
2021-05-13 20:03:07 +02:00
}
/// <summary> Gets the AGBs</summary>
/// <param name="resourceProvider"></param>
/// <returns> AGBs</returns>
public static async Task<HtmlWebViewSource> GetAgb(
string hostName,
2022-01-22 18:28:01 +01:00
string agbResourcePath,
2022-08-30 15:42:25 +02:00
bool isSiteCachingOn,
string uiIsoLangugageName)
2021-05-13 20:03:07 +02:00
{
2022-08-30 15:42:25 +02:00
string GetUriText()
=> $"https://{hostName}/{agbResourcePath}";
if (string.IsNullOrEmpty(agbResourcePath))
{
return new HtmlWebViewSource
{
Html = 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.");
2021-05-13 20:03:07 +02:00
return new HtmlWebViewSource
{
2022-08-30 15:42:25 +02:00
Html = await ViewModelHelper.GetSource(
GetUriText(), // "agb.html"
2022-01-22 18:28:01 +01:00
isSiteCachingOn)
2021-05-13 20:03:07 +02:00
};
}
/// <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 InfoLicenses => new HtmlWebViewSource
{
Html = ResourceProvider("HtmlResouces.V02.InfoLicenses.html")
.Replace("CURRENT_VERSION_TINKAPP", DependencyService.Get<IAppInfo>().Version.ToString())
.Replace("ACTIVE_APPNAME", AppInfo.Name)
2022-08-30 15:42:25 +02:00
.Replace("APPSUPPORTMAILADDRESS", ContactPageViewModel.APPSUPPORTMAILADDRESS)
2021-05-13 20:03:07 +02:00
};
/// <summary> Privacy text.</summary>
private HtmlWebViewSource infoImpress;
/// <summary> Gets the privacy related information. </summary>
2022-08-30 15:42:25 +02:00
public HtmlWebViewSource InfoImpressum
2021-05-13 20:03:07 +02:00
{
get => infoImpress;
set
{
infoImpress = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InfoImpressum)));
}
}
/// <summary> Agb information text.</summary>
private HtmlWebViewSource infoAgb;
/// <summary> Privacy text.</summary>
private HtmlWebViewSource infoPrivacy;
/// <summary> Agb information text.</summary>
public HtmlWebViewSource InfoAgb
{
get => infoAgb;
set
{
infoAgb = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InfoAgb)));
}
}
/// <summary> Agb information text.</summary>
public HtmlWebViewSource InfoPrivacy
{
get => infoPrivacy;
set
{
infoPrivacy = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InfoPrivacy)));
}
}
}
}