using System; using System.ComponentModel; using System.Threading.Tasks; using Serilog; using ShareeBike.Model; using ShareeBike.Model.Connector; using ShareeBike.Model.Services.CopriApi; using Xamarin.Forms; namespace ShareeBike.ViewModel.Help { public class HelpPageViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; /// /// Holds the current ui two letter ISO language name. /// private string UiIsoLanguageName { get; } /// Holds value whether site caching is on or off. bool IsSiteCachingOn { get; } /// /// Relative path to fees resources of empty if value was not yet querried from backend. /// private string TariffsResourcePath { get; set; } /// /// Relative path to bike info resources of empty if value was not yet querried from backend. /// private string ManualResourcePath { get; set; } private bool _IsIdle = false; /// /// Is true if no action is pending, false otherwise. /// public bool IsIdle { get => _IsIdle; private set { if (_IsIdle == value) { // Nothing to do. return; } _IsIdle = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsIdle))); return; } } /// /// Object to query resources urls object from backend if required. /// This object is used to update resources path values , and from. /// private Func QueryProvider { get; } /// /// Action to update shared resources urls object exposed by main model. /// private Action UpdateUrlsAction { get; } /// Constructs view model. /// Set of user permissions /// Delegate to get embedded html resource. Used as fallback if download from web page does not work and cache is empty. /// Object to query resources path values if required. public HelpPageViewModel( string hostName, string tariffsResourcePath, string manualResourcePath, bool isSiteCachingOn, string uiIsoLangugageName, Func queryProvider, Action updateUrlsAction) { HostName = hostName; TariffsResourcePath = tariffsResourcePath; ManualResourcePath = manualResourcePath; IsSiteCachingOn = isSiteCachingOn; QueryProvider = queryProvider; UpdateUrlsAction = updateUrlsAction; UiIsoLanguageName = uiIsoLangugageName; } /// Holds the name of the host. private string HostName { get; } // Get resource urls object from backend. public async Task GetUrls() { Result stations; try { stations = await QueryProvider().GetBikesAndStationsAsync(); } catch (Exception ex) { Log.ForContext().Error($"Getting resource urls from COPRI failed. {ex.Message}"); return new ResourceUrls(); } return stations?.GeneralData?.ResourceUrls ?? new ResourceUrls(); } /// Called when page is shown. public async void OnAppearing() { // Set state to busy. IsIdle = false; if (string.IsNullOrEmpty(TariffsResourcePath)) { var urls = await GetUrls(); TariffsResourcePath = urls.TariffsResourcePath; ManualResourcePath = urls.ManualResourcePath; UpdateUrlsAction(urls); // Update main model to prevent duplicate queries. } //set font size for html content (necessary for iOS) string headerString = "
"; ManualHtml = new HtmlWebViewSource { Html = !string.IsNullOrEmpty(ManualResourcePath) ? headerString + await ViewModelHelper.GetSource($"https://{HostName}/{ManualResourcePath}" /*"site/bike_info_....html"*/, IsSiteCachingOn) : headerString + await Task.FromResult(ViewModelHelper.FromBody("No manual resource available. Resource path is null or empty.")) }; TariffsHtml = new HtmlWebViewSource { Html = !string.IsNullOrEmpty(TariffsResourcePath) ? headerString + await ViewModelHelper.GetSource($"https://{HostName}/{TariffsResourcePath}" /*"site/tarif_info_....html"*/, IsSiteCachingOn) : headerString + await Task.FromResult(ViewModelHelper.FromBody("No tariffs resource available. Resource path is null or empty.")) }; FaqHtml = new HtmlWebViewSource { Html = UiIsoLanguageName == "de" ? await ViewModelHelper.GetSource($"https://sharee.bike/faqHtml/fahrrad-nutzung/", IsSiteCachingOn) : await ViewModelHelper.GetSource($"https://sharee.bike/faqHtml/bike-usage/", IsSiteCachingOn) }; // Set state to idle. IsIdle = true; } private HtmlWebViewSource manualHtml; private HtmlWebViewSource tariffsHtml; private HtmlWebViewSource faqHtml; public HtmlWebViewSource ManualHtml { get => manualHtml; set { manualHtml = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ManualHtml))); } } public HtmlWebViewSource TariffsHtml { get => tariffsHtml; set { tariffsHtml = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TariffsHtml))); } } public HtmlWebViewSource FaqHtml { get => faqHtml; set { faqHtml = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FaqHtml))); } } } }