using System;
using System.ComponentModel;
using System.Threading.Tasks;
using TINK.Model.Device;
using TINK.Services.CopriApi.ServerUris;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace TINK.ViewModel.Info
{
/// Manges the tabbed info page.
public class InfoViewModel : 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; }
/// Constructs Info view model
/// Holds value wether site caching is on or off.
/// Delegate to get an an embedded html ressource. Used as fallback if download from web page does not work and cache is empty.
public InfoViewModel(
string hostName,
bool isSiteCachingOn,
Func resourceProvider)
{
HostName = hostName;
IsSiteCachingOn = isSiteCachingOn;
InfoAgb = new HtmlWebViewSource { Html = "Loading..." };
ResourceProvider = resourceProvider
?? throw new ArgumentException($"Can not instantiate {typeof(InfoViewModel)}-object. No ressource provider availalbe.");
}
/// Called when page is shown.
public async void OnAppearing()
{
InfoAgb = await GetAgb(HostName, IsSiteCachingOn, ResourceProvider);
InfoPrivacy = new HtmlWebViewSource
{
Html = await ViewModelHelper.GetSource(
$"https://{HostName}/{HostName.GetPrivacyResource()}",
IsSiteCachingOn,
HostName.GetIsCopri() ? () => ResourceProvider("HtmlResouces.V02.InfoDatenschutz.html") : (Func) null) /* offline resources only available for TINK */,
};
InfoImpressum = new HtmlWebViewSource
{
Html = HostName.GetIsCopri()
? ResourceProvider("HtmlResouces.V02.InfoImpressum.html")
: await ViewModelHelper.GetSource($"https://{HostName}/{CopriHelper.SHAREE_SILTEFOLDERNAME}/impress.html", IsSiteCachingOn)
};
}
/// Gets the AGBs
///
/// AGBs
public static async Task GetAgb(
string hostName,
bool isSiteCachingOn,
Func resourceProvider)
{
return new HtmlWebViewSource
{
Html = await ViewModelHelper.GetSource(
$"https://{hostName}/{hostName.GetAGBResource()}",
isSiteCachingOn,
hostName.GetIsCopri() ? () => resourceProvider("HtmlResouces.V02.InfoAGB.html") : (Func) null) /* offline resources only available for TINK */,
};
}
/// 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)
};
/// 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)));
}
}
}
}