mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
246 lines
9.1 KiB
C#
246 lines
9.1 KiB
C#
|
using MonkeyCache.FileStore;
|
|||
|
using Serilog;
|
|||
|
using System;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using TINK.Model.Repository.Exception;
|
|||
|
using TINK.Model.Device;
|
|||
|
using TINK.Model.State;
|
|||
|
using TINK.Model.Station;
|
|||
|
using TINK.Model.User;
|
|||
|
using Xamarin.Forms;
|
|||
|
|
|||
|
using TINK.Model.Bikes.Bike.BC;
|
|||
|
using TINK.Model.Repository;
|
|||
|
using TINK.Repository.Exception;
|
|||
|
using System.Net;
|
|||
|
using TINK.MultilingualResources;
|
|||
|
|
|||
|
namespace TINK.ViewModel
|
|||
|
{
|
|||
|
public static class ViewModelHelper
|
|||
|
{
|
|||
|
/// <summary> First part of text making up a station name.</summary>
|
|||
|
private const string USER_FIENDLY_STATIONNUMBER_PREFIX = "Station";
|
|||
|
|
|||
|
/// <summary>Holds the color which marks link to TINK- app pages, web sites, ...</summary>
|
|||
|
public static Color LINK_COLOR = Color.Blue;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Gets station name from station object.
|
|||
|
/// </summary>
|
|||
|
/// <param name="p_oStation">Station to get id from</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GetStationName(this IStation p_oStation)
|
|||
|
{
|
|||
|
if (p_oStation == null)
|
|||
|
{
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(p_oStation.StationName))
|
|||
|
{
|
|||
|
return $"{p_oStation.StationName}, Nr. {p_oStation.Id}.";
|
|||
|
}
|
|||
|
|
|||
|
return GetStationName(p_oStation.Id);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Gets station name from station object.
|
|||
|
/// </summary>
|
|||
|
/// <param name="p_oStation">Station to get id from</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GetStationName(int p_iStationId)
|
|||
|
{
|
|||
|
return string.Format("{0} {1}", USER_FIENDLY_STATIONNUMBER_PREFIX, p_iStationId);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Gets station id from station name.
|
|||
|
/// </summary>
|
|||
|
/// <param name="p_oStation">Station to get id from</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static int GetStationId(string p_strStationName)
|
|||
|
{
|
|||
|
return int.Parse(p_strStationName.Replace(USER_FIENDLY_STATIONNUMBER_PREFIX, "").Trim());
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Get the display name of a bike. </summary>
|
|||
|
/// <param name="bike">bike to get name for.</param>
|
|||
|
/// <returns>Display name of bike.</returns>
|
|||
|
public static string GetDisplayName(this IBikeInfoMutable bike)
|
|||
|
{
|
|||
|
var l_oId = bike.Id;
|
|||
|
var l_oIsDemo = bike.IsDemo;
|
|||
|
|
|||
|
// Not known how many whells cargo bike has.
|
|||
|
return $"{(!string.IsNullOrEmpty(bike.Description) ? $"{bike.Description}, " : string.Empty)}Nr. {l_oId}";
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Maps state to color.
|
|||
|
/// </summary>
|
|||
|
/// <param name="p_eState"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Color GetColor(this InUseStateEnum p_eState)
|
|||
|
{
|
|||
|
switch (p_eState)
|
|||
|
{
|
|||
|
case InUseStateEnum.Disposable:
|
|||
|
return Color.Default;
|
|||
|
|
|||
|
case InUseStateEnum.Reserved:
|
|||
|
return Color.Orange;
|
|||
|
|
|||
|
case InUseStateEnum.Booked:
|
|||
|
return Color.Green;
|
|||
|
default:
|
|||
|
return Color.Default;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Gets message that logged in user has not booked any bikes. </summary>
|
|||
|
public static string GetShortErrorInfoText(this Exception exception)
|
|||
|
{
|
|||
|
if (exception == null)
|
|||
|
{
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
|
|||
|
// An error occurred getting bikes information.
|
|||
|
switch (exception)
|
|||
|
{
|
|||
|
case WebConnectFailureException:
|
|||
|
return AppResources.ActivityTextErrorWebConnectFailureException;
|
|||
|
case InvalidResponseException:
|
|||
|
return AppResources.ActivityTextErrorInvalidResponseException;
|
|||
|
case WebForbiddenException:
|
|||
|
return AppResources.ActivityTextErrorWebForbiddenException;
|
|||
|
case DeserializationException:
|
|||
|
return AppResources.ActivityTextErrorDeserializationException;
|
|||
|
case WebException webException:
|
|||
|
return string.Format(AppResources.ActivityTextErrorWebException, webException.Status);
|
|||
|
default:
|
|||
|
return AppResources.ActivityTextErrorException;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Gets message that logged in user has not booked any bikes. </summary>
|
|||
|
public static FormattedString GetErrorInfoText(this Exception p_oException)
|
|||
|
{
|
|||
|
if (p_oException == null)
|
|||
|
{
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
|
|||
|
FormattedString l_oError;
|
|||
|
// An error occurred getting bikes information.
|
|||
|
if (p_oException is WebConnectFailureException)
|
|||
|
{
|
|||
|
|
|||
|
l_oError = new FormattedString();
|
|||
|
l_oError.Spans.Add(new Span { Text = "Information!\r\n", FontAttributes = FontAttributes.Bold });
|
|||
|
l_oError.Spans.Add(new Span { Text = $"{p_oException.Message}\r\n{WebConnectFailureException.GetHintToPossibleExceptionsReasons}" });
|
|||
|
return l_oError;
|
|||
|
|
|||
|
}
|
|||
|
else if (p_oException is InvalidResponseException)
|
|||
|
{
|
|||
|
l_oError = new FormattedString();
|
|||
|
l_oError.Spans.Add(new Span { Text = "Fehler, ungültige Serverantwort!\r\n", FontAttributes = FontAttributes.Bold });
|
|||
|
l_oError.Spans.Add(new Span { Text = $"{p_oException.Message}" });
|
|||
|
return l_oError;
|
|||
|
}
|
|||
|
else if (p_oException is WebForbiddenException)
|
|||
|
{
|
|||
|
l_oError = new FormattedString();
|
|||
|
l_oError.Spans.Add(new Span { Text = "Beschäftigt... Einen Moment bitte!" });
|
|||
|
return l_oError;
|
|||
|
}
|
|||
|
|
|||
|
l_oError = new FormattedString();
|
|||
|
l_oError.Spans.Add(new Span { Text = "Allgemeiner Fehler!\r\n", FontAttributes = FontAttributes.Bold });
|
|||
|
l_oError.Spans.Add(new Span { Text = $"{p_oException}" });
|
|||
|
return l_oError;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary> User tabbed a URI. </summary>
|
|||
|
/// <param name="p_oSender">Sender of the event.</param>
|
|||
|
/// <param name="p_eEventArgs">Event arguments</param>
|
|||
|
public static void OnNavigating(object p_oSender, WebNavigatingEventArgs p_eEventArgs)
|
|||
|
{
|
|||
|
|
|||
|
if (!p_eEventArgs.Url.ToUpper().StartsWith("HTTP"))
|
|||
|
{
|
|||
|
// An internal link was detected.
|
|||
|
// Stay inside WebView
|
|||
|
p_eEventArgs.Cancel = false;
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// Do not navigate outside the document.
|
|||
|
p_eEventArgs.Cancel = true;
|
|||
|
|
|||
|
DependencyService.Get<IExternalBrowserService>().OpenUrl(p_eEventArgs.Url);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary> Gets the user group if a user friendly name.</summary>
|
|||
|
/// <param name="p_oUser"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GetUserGroupDisplayName(this User p_oUser)
|
|||
|
{
|
|||
|
return string.Join(" & ", p_oUser.Group);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary> Called when page is shown. </summary>
|
|||
|
/// <param name="resourceUrl">Url to load data from.</param>
|
|||
|
/// <param name="isSiteCachingOn">Holds value wether site caching is on or off.</param>
|
|||
|
/// <param name="resourceProvider"> Provides resource from embedded ressources.</param>
|
|||
|
public static async Task<string> GetSource(
|
|||
|
string resourceUrl,
|
|||
|
bool isSiteCachingOn,
|
|||
|
Func<string> resourceProvider = null)
|
|||
|
{
|
|||
|
if (!Barrel.Current.IsExpired(resourceUrl) && isSiteCachingOn)
|
|||
|
{
|
|||
|
// Cached html is still valid and caching is on.
|
|||
|
return Barrel.Current.Get<string>(resourceUrl);
|
|||
|
}
|
|||
|
|
|||
|
string htmlContent = string.Empty;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
// Get info from web server.
|
|||
|
htmlContent = await CopriCallsHttps.Get(resourceUrl);
|
|||
|
}
|
|||
|
catch (Exception l_oException)
|
|||
|
{
|
|||
|
// Getting html failed.
|
|||
|
Log.Error($"Requesting {resourceUrl} failed. {l_oException.Message}");
|
|||
|
}
|
|||
|
|
|||
|
switch (string.IsNullOrEmpty(htmlContent))
|
|||
|
{
|
|||
|
case true:
|
|||
|
// An error occurred getting resource from web
|
|||
|
htmlContent = Barrel.Current.Exists(resourceUrl)
|
|||
|
? Barrel.Current.Get<string>(key: resourceUrl) // Get from MonkeyCache
|
|||
|
: resourceProvider != null ? resourceProvider() : $"<DOCTYPE html>Error loading {resourceUrl}."; // Get build in ressource.
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
// Add resource to cache.
|
|||
|
Barrel.Current.Add(key: resourceUrl, data: htmlContent, expireIn: isSiteCachingOn ? TimeSpan.FromDays(1) : TimeSpan.FromMilliseconds(1) );
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
return htmlContent ?? string.Format("<!DOCTYPE html><html lang=\"de\"><body>An error occurred loading html- ressource.</body>");
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|