sharee.bike-App/SharedBusinessLogic/ViewModel/LegalInformation/LegalInformationPageViewModel.cs

290 lines
9.1 KiB
C#
Raw Normal View History

2022-10-03 17:55:10 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.ComponentModel;
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Serilog;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model;
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
2021-05-13 20:03:07 +02:00
using Xamarin.Essentials;
using Xamarin.Forms;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Connector;
using ShareeBike.ViewModel.Contact;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.ViewModel.LegalInformation
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
/// <summary> Manges the tabbed info page. </summary>
2023-11-21 15:26:57 +01:00
public class LegalInformationPageViewModel : INotifyPropertyChanged
2022-09-06 16:08:19 +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; }
2023-05-09 08:47:52 +02:00
/// <summary> Holds value whether site caching is on or off.</summary>
2022-09-06 16:08:19 +02:00
bool IsSiteCachingOn { get; }
2022-10-03 17:55:10 +02:00
/// <summary>
/// Relative path to agb resources of empty if value was not yet querried from backend.
/// </summary>
2023-11-21 15:26:57 +01:00
private string GtcResourcePath { get; set; }
2022-10-03 17:55:10 +02:00
/// <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;
2022-09-06 16:08:19 +02:00
2022-10-03 17:55:10 +02:00
/// <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;
}
}
2022-09-06 16:08:19 +02:00
2022-10-03 17:55:10 +02:00
/// <summary>
2023-04-19 12:14:14 +02:00
/// Object to query resources urls object from backend if required.
2023-11-21 15:26:57 +01:00
/// This object is used to update resources path values <see cref="GtcResourcePath"/>, <see cref="PrivacyResourcePath"/> and <see cref="ImpressResourcePath"/> from.
2022-10-03 17:55:10 +02:00
/// </summary>
private Func<IQuery> QueryProvider { get; }
2022-09-06 16:08:19 +02:00
/// <summary>
2022-10-03 17:55:10 +02:00
/// Action to update shared resources urls object exposed by main model.
2022-09-06 16:08:19 +02:00
/// </summary>
2022-10-03 17:55:10 +02:00
private Action<IResourceUrls> UpdateUrlsAction { get; }
2022-09-06 16:08:19 +02:00
/// <summary> Constructs Info view model</summary>
2022-10-03 17:55:10 +02:00
/// <param name="hostName">Name of the host to get html resources from.</param>
2023-05-09 08:47:52 +02:00
/// <param name="isSiteCachingOn">Holds value whether site caching is on or off.</param>
2023-11-21 15:26:57 +01:00
/// <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>
2023-05-09 08:47:52 +02:00
/// <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>
2023-04-19 12:14:14 +02:00
/// <param name="queryProvider">Object to query resources urls object from backend if required.</param>
2022-10-03 17:55:10 +02:00
/// <param name="updateUrlsAction">Action to update shared resources urls object</param>
2023-11-21 15:26:57 +01:00
public LegalInformationPageViewModel(
2022-09-06 16:08:19 +02:00
string hostName,
2023-11-21 15:26:57 +01:00
string gtcResourcePath,
2022-09-06 16:08:19 +02:00
string privacyResourcePath,
string impressResourcePath,
bool isSiteCachingOn,
2022-10-03 17:55:10 +02:00
Func<string, string> resourceProvider,
Func<IQuery> queryProvider,
Action<IResourceUrls> updateUrlsAction)
2022-09-06 16:08:19 +02:00
{
HostName = hostName;
2023-11-21 15:26:57 +01:00
GtcResourcePath = gtcResourcePath;
2022-09-06 16:08:19 +02:00
PrivacyResourcePath = privacyResourcePath;
ImpressResourcePath = impressResourcePath;
IsSiteCachingOn = isSiteCachingOn;
2022-10-03 17:55:10 +02:00
QueryProvider = queryProvider;
UpdateUrlsAction = updateUrlsAction;
2022-09-06 16:08:19 +02:00
2023-11-21 15:26:57 +01:00
GtcHtml = new HtmlWebViewSource { Html = "<html>Loading...</html>" };
2022-09-06 16:08:19 +02:00
ResourceProvider = resourceProvider
2023-11-21 15:26:57 +01:00
?? throw new ArgumentException($"Can not instantiate {typeof(LegalInformationPageViewModel)}-object. No resource provider centered.");
2022-09-06 16:08:19 +02:00
}
2023-11-21 15:26:57 +01:00
// Get resource urls object from backend.
public async Task<IResourceUrls> GetUrls()
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
Result<StationsAndBikesContainer> stations;
try
2022-10-03 17:55:10 +02:00
{
2023-11-21 15:26:57 +01:00
stations = await QueryProvider().GetBikesAndStationsAsync();
2022-10-03 17:55:10 +02:00
}
2023-11-21 15:26:57 +01:00
catch (Exception ex)
{
Log.ForContext<LegalInformationPageViewModel>().Error($"Getting resource urls from COPRI failed. {ex.Message}");
return new ResourceUrls();
}
return stations?.GeneralData?.ResourceUrls ?? new ResourceUrls();
}
2022-10-03 17:55:10 +02:00
2023-11-21 15:26:57 +01:00
/// <summary> Called when page is shown. </summary>
public async void OnAppearing()
{
2022-10-03 17:55:10 +02:00
// 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;
2023-11-21 15:26:57 +01:00
GtcResourcePath = urls.GtcResourcePath;
2022-10-03 17:55:10 +02:00
UpdateUrlsAction(urls); // Update main model to prevent duplicate queries.
}
2023-09-14 12:28:59 +02:00
//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>";
2022-09-06 16:08:19 +02:00
// Gets privacy info from server.
2023-11-21 15:26:57 +01:00
async Task<HtmlWebViewSource> GetPrivacy()
2022-09-06 16:08:19 +02:00
{
string GetUriText()
=> $"https://{HostName}/{PrivacyResourcePath}";
if (string.IsNullOrEmpty(PrivacyResourcePath))
{
2023-05-09 08:47:52 +02:00
// Information to access resource is missing
2022-09-06 16:08:19 +02:00
return new HtmlWebViewSource
{
2023-09-14 12:28:59 +02:00
Html = headerString + await Task.FromResult(ViewModelHelper.FromBody("No privacy resource available. Resource path is null or empty."))
2022-09-06 16:08:19 +02:00
};
}
Log.Debug($"Request to open url {GetUriText()} to get privacy info.");
return new HtmlWebViewSource
{
2023-09-14 12:28:59 +02:00
Html = headerString + await ViewModelHelper.GetSource(
2022-09-06 16:08:19 +02:00
GetUriText(), // "site/privacy.html"
IsSiteCachingOn)
};
}
// Gets impress info from server.
2023-11-21 15:26:57 +01:00
async Task<HtmlWebViewSource> GetImpress()
2022-09-06 16:08:19 +02:00
{
string GetUriText()
=> $"https://{HostName}/{ImpressResourcePath}";
if (string.IsNullOrEmpty(ImpressResourcePath))
{
2023-05-09 08:47:52 +02:00
// Information to access resource is missing
2022-09-06 16:08:19 +02:00
return new HtmlWebViewSource
{
2023-09-14 12:28:59 +02:00
Html = headerString + await Task.FromResult(ViewModelHelper.FromBody("No impress resource available. Resource path is null or empty."))
2022-09-06 16:08:19 +02:00
};
}
Log.Debug($"Request to open url {GetUriText()} to get impress info.");
return new HtmlWebViewSource
{
2023-09-14 12:28:59 +02:00
Html = headerString + await ViewModelHelper.GetSource(
2022-09-06 16:08:19 +02:00
GetUriText(), // "site/privacy.html"
IsSiteCachingOn)
};
}
2023-11-21 15:26:57 +01:00
GtcHtml = await GetGtc(HostName, GtcResourcePath, IsSiteCachingOn);
2022-09-06 16:08:19 +02:00
2023-11-21 15:26:57 +01:00
PrivacyHtml = await GetPrivacy();
2022-09-06 16:08:19 +02:00
2023-11-21 15:26:57 +01:00
ImpressHtml = await GetImpress();
2022-10-03 17:55:10 +02:00
// Set state to idle.
IsIdle = true;
2022-09-06 16:08:19 +02:00
}
/// <summary> Gets the AGBs</summary>
/// <param name="resourceProvider"></param>
/// <returns> AGBs</returns>
2023-11-21 15:26:57 +01:00
public static async Task<HtmlWebViewSource> GetGtc(
2022-09-06 16:08:19 +02:00
string hostName,
2023-11-21 15:26:57 +01:00
string gtcResourcePath,
2022-10-03 17:55:10 +02:00
bool isSiteCachingOn)
2022-09-06 16:08:19 +02:00
{
string GetUriText()
2023-11-21 15:26:57 +01:00
=> $"https://{hostName}/{gtcResourcePath}";
2022-09-06 16:08:19 +02:00
2023-09-14 12:28:59 +02:00
//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>";
2023-11-21 15:26:57 +01:00
if (string.IsNullOrEmpty(gtcResourcePath))
2022-09-06 16:08:19 +02:00
{
return new HtmlWebViewSource
{
2023-09-14 12:28:59 +02:00
Html = headerString + await Task.FromResult(ViewModelHelper.FromBody("No terms resource available. Resource path is null or empty."))
2022-09-06 16:08:19 +02:00
};
}
Log.Debug($"Request to open url {GetUriText()} to get AGB infos.");
return new HtmlWebViewSource
{
2023-09-14 12:28:59 +02:00
Html = headerString + await ViewModelHelper.GetSource(
2022-09-06 16:08:19 +02:00
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>
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource AppHtml => new HtmlWebViewSource
2022-09-06 16:08:19 +02:00
{
Html = ResourceProvider("HtmlResouces.V02.InfoLicenses.html")
2024-04-09 12:53:23 +02:00
.Replace("CURRENT_VERSION_ShareeBikeAPP", DependencyService.Get<IAppInfo>().Version.ToString())
2022-09-06 16:08:19 +02:00
.Replace("ACTIVE_APPNAME", AppInfo.Name)
.Replace("APPSUPPORTMAILADDRESS", ContactPageViewModel.APPSUPPORTMAILADDRESS)
};
2023-11-21 15:26:57 +01:00
/// <summary> PrivacyHtml text.</summary>
private HtmlWebViewSource impressHtml;
2022-09-06 16:08:19 +02:00
/// <summary> Gets the privacy related information. </summary>
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource ImpressHtml
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
get => impressHtml;
2022-09-06 16:08:19 +02:00
set
{
2023-11-21 15:26:57 +01:00
impressHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ImpressHtml)));
2022-09-06 16:08:19 +02:00
}
}
/// <summary> Agb information text.</summary>
2023-11-21 15:26:57 +01:00
private HtmlWebViewSource gtcHtml;
2022-09-06 16:08:19 +02:00
2023-11-21 15:26:57 +01:00
/// <summary> PrivacyHtml text.</summary>
private HtmlWebViewSource privacyHtml;
2022-09-06 16:08:19 +02:00
/// <summary> Agb information text.</summary>
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource GtcHtml
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
get => gtcHtml;
2022-09-06 16:08:19 +02:00
set
{
2023-11-21 15:26:57 +01:00
gtcHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(GtcHtml)));
2022-09-06 16:08:19 +02:00
}
}
/// <summary> Agb information text.</summary>
2023-11-21 15:26:57 +01:00
public HtmlWebViewSource PrivacyHtml
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
get => privacyHtml;
2022-09-06 16:08:19 +02:00
set
{
2023-11-21 15:26:57 +01:00
privacyHtml = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PrivacyHtml)));
2022-09-06 16:08:19 +02:00
}
}
}
2021-05-13 20:03:07 +02:00
}