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