mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 10:36:30 +01:00
282 lines
10 KiB
C#
282 lines
10 KiB
C#
using Plugin.Messaging;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using TINK.Model.Services.CopriApi.ServerUris;
|
|
using TINK.MultilingualResources;
|
|
using TINK.View;
|
|
using Xamarin.Essentials;
|
|
using Xamarin.Forms;
|
|
|
|
namespace TINK.ViewModel.Info
|
|
{
|
|
/// <summary> View model for contact page.</summary>
|
|
public class ContactPageViewModel
|
|
{
|
|
/// <summary> Reference on view service to show modal notifications and to perform navigation. </summary>
|
|
private IViewService ViewService { get; }
|
|
|
|
Uri ActiveUri { get; }
|
|
|
|
/// <summary> Holds a reference to the external trigger service. </summary>
|
|
private Action OpenUrlInExternalBrowser { get; }
|
|
|
|
Func<string> CreateAttachment { get; }
|
|
|
|
/// <summary> Constructs a contact page view model. </summary>
|
|
/// <param name="openUrlInExternalBrowser">Action to open an external browser.</param>
|
|
/// <param name="viewService">View service to notify user.</param>
|
|
public ContactPageViewModel(
|
|
Uri activeUri,
|
|
Func<string> createAttachment,
|
|
Action openUrlInExternalBrowser,
|
|
IViewService viewService)
|
|
{
|
|
ActiveUri = activeUri
|
|
?? throw new ArgumentException("Can not instantiate contact page view model- object. No active uri available.");
|
|
|
|
CreateAttachment = createAttachment
|
|
?? throw new ArgumentException("Can not instantiate contact page view model- object. No create attachment provider available.");
|
|
|
|
ViewService = viewService
|
|
?? throw new ArgumentException("Can not instantiate contact page view model- object. No user view service available.");
|
|
|
|
OpenUrlInExternalBrowser = openUrlInExternalBrowser
|
|
?? throw new ArgumentException("Can not instantiate contact page view model- object. No user external browse service available.");
|
|
}
|
|
|
|
/// <summary> Command object to bind login button to view model. </summary>
|
|
public ICommand OnMailRequest
|
|
=> new Command(
|
|
async () => await DoSendMail(),
|
|
() => IsSendMailAvailable);
|
|
|
|
|
|
/// <summary>True if sending mail is possible.</summary>
|
|
public bool IsSendMailAvailable =>
|
|
CrossMessaging.Current.EmailMessenger.CanSendEmail;
|
|
|
|
|
|
/// <summary>cTrue if doing a phone call is possible.</summary>
|
|
public bool IsDoPhoncallAvailable
|
|
=> CrossMessaging.Current.PhoneDialer.CanMakePhoneCall;
|
|
|
|
/// <summary>Holds the mail address to mail to.</summary>
|
|
public string MailAddressText
|
|
{
|
|
get
|
|
{
|
|
switch (ActiveUri.AbsoluteUri)
|
|
{
|
|
case CopriServerUriList.TINK_DEVEL:
|
|
case CopriServerUriList.TINK_LIVE:
|
|
return "tink@fahrradspezialitaeten.com";
|
|
|
|
case CopriServerUriList.SHAREE_DEVEL:
|
|
case CopriServerUriList.SHAREE_LIVE:
|
|
return "hotline@sharee.bike";
|
|
|
|
default:
|
|
return "post@sharee.bike";
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Holds the mail address to send mail to.</summary>
|
|
public string PhoneNumberText
|
|
{
|
|
get
|
|
{
|
|
switch (ActiveUri.AbsoluteUri)
|
|
{
|
|
case CopriServerUriList.TINK_DEVEL:
|
|
case CopriServerUriList.TINK_LIVE:
|
|
return "+49 7531 - 3694389";
|
|
|
|
case CopriServerUriList.SHAREE_DEVEL:
|
|
case CopriServerUriList.SHAREE_LIVE:
|
|
return "+49 761 - 45370097";
|
|
|
|
default:
|
|
return "+49 761 5158912";
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary> Request to do a phone call. </summary>
|
|
public async Task DoSendMail()
|
|
{
|
|
try
|
|
{
|
|
if (!IsSendMailAvailable)
|
|
{
|
|
// Nothing to do because email can not be sent.
|
|
return;
|
|
}
|
|
|
|
var tinkMail = await ViewService.DisplayAlert(
|
|
AppResources.QuestionTitle,
|
|
string.Format(AppResources.QuestionSupportmailSubject, GetAppName(ActiveUri)),
|
|
string.Format(AppResources.QuestionSupportmailAnswerOperator, GetAppName(ActiveUri)),
|
|
string.Format(AppResources.QuestionSupportmailAnswerApp, GetAppName(ActiveUri)));
|
|
|
|
if (tinkMail)
|
|
{
|
|
// Send TINK- related support mail.
|
|
await Email.ComposeAsync(new EmailMessage { To = new List<string> { MailAddressText }, Subject = $"{GetAppName(ActiveUri)} Anfrage" });
|
|
return;
|
|
}
|
|
|
|
// Send a tink app related mail
|
|
var appendFile = false;
|
|
appendFile = await ViewService.DisplayAlert(
|
|
AppResources.QuestionTitle,
|
|
AppResources.QuestionSupportmailAttachment,
|
|
AppResources.QuestionAnswerYes,
|
|
AppResources.QuestionAnswerNo);
|
|
|
|
var message = new EmailMessage { To = new List<string> { MailAddressText }, Subject = $"{GetAppName(ActiveUri)}-App Anfrage" };
|
|
|
|
if (appendFile == false)
|
|
{
|
|
// Send a tink app related mail
|
|
await Email.ComposeAsync(message);
|
|
return;
|
|
}
|
|
|
|
var logFileName = string.Empty;
|
|
try
|
|
{
|
|
logFileName = CreateAttachment();
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.MessageWaring,
|
|
AppResources.ErrorSupportmailCreateAttachment,
|
|
l_oException.Message,
|
|
AppResources.MessageAnswerOk);
|
|
Log.Error("An error occurred creating attachment. {@l_oException)", l_oException);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(logFileName))
|
|
{
|
|
message.Attachments.Add(new Xamarin.Essentials.EmailAttachment(logFileName));
|
|
}
|
|
|
|
// Send a tink app related mail
|
|
await Email.ComposeAsync(message);
|
|
}
|
|
catch (Exception p_oException)
|
|
{
|
|
Log.Error("An unexpected error occurred sending mail. {@Exception}", p_oException);
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.MessageWaring,
|
|
AppResources.ErrorSupportmailMailingFailed,
|
|
p_oException.Message,
|
|
AppResources.MessageAnswerOk);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary> Command object to bind login button to view model. </summary>
|
|
public ICommand OnPhoneRequest
|
|
=> new Command(
|
|
async () => await DoPhoneCall(),
|
|
() => IsDoPhoncallAvailable);
|
|
|
|
/// <summary> Request to do a phone call. </summary>
|
|
public async Task DoPhoneCall()
|
|
{
|
|
try
|
|
{
|
|
// Make Phone Call
|
|
if (IsDoPhoncallAvailable)
|
|
{
|
|
CrossMessaging.Current.PhoneDialer.MakePhoneCall(PhoneNumberText);
|
|
}
|
|
}
|
|
catch (Exception p_oException)
|
|
{
|
|
Log.Error("An unexpected error occurred doing a phone call. {@Exception}", p_oException);
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.MessageWaring,
|
|
AppResources.ErrorSupportmailPhoningFailed,
|
|
p_oException.Message,
|
|
AppResources.MessageAnswerOk);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary> Text providing mail address and possilbe reasons to contact. </summary>
|
|
public FormattedString MaliAddressAndMotivationsText
|
|
{
|
|
get
|
|
{
|
|
var hint = new FormattedString();
|
|
hint.Spans.Add(new Span { Text = AppResources.MessageContactMail });
|
|
return hint;
|
|
}
|
|
}
|
|
|
|
/// <summary> Invitation to rate app.</summary>
|
|
public FormattedString LikeTinkApp
|
|
{
|
|
get
|
|
{
|
|
var l_oHint = new FormattedString();
|
|
l_oHint.Spans.Add(new Span { Text = string.Format(AppResources.MessageRateMail, GetAppName(ActiveUri)) });
|
|
return l_oHint;
|
|
}
|
|
}
|
|
|
|
/// <summary> User clicks rate button.</summary>
|
|
public ICommand OnRateRequest
|
|
=> new Command(() => RegisterRequest());
|
|
|
|
/// <summary> Opens login page.</summary>
|
|
public void RegisterRequest()
|
|
{
|
|
try
|
|
{
|
|
OpenUrlInExternalBrowser();
|
|
}
|
|
catch (Exception p_oException)
|
|
{
|
|
Log.Error("Ein unerwarteter Fehler ist auf der Login Seite beim Öffnen eines Browsers, Seite {url}, aufgetreten. {@Exception}", p_oException);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary> Invitation to rate app.</summary>
|
|
public FormattedString PhoneContactText
|
|
{
|
|
get
|
|
{
|
|
var l_oHint = new FormattedString();
|
|
l_oHint.Spans.Add(new Span { Text = string.Format(AppResources.MessagePhoneMail, GetAppName(ActiveUri)) });
|
|
return l_oHint;
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets the application name.</summary>
|
|
public static string GetAppName(Uri activeUri)
|
|
{
|
|
switch (activeUri.AbsoluteUri)
|
|
{
|
|
case CopriServerUriList.TINK_DEVEL:
|
|
case CopriServerUriList.TINK_LIVE:
|
|
return "TINK";
|
|
|
|
case CopriServerUriList.SHAREE_DEVEL:
|
|
case CopriServerUriList.SHAREE_LIVE:
|
|
return "sharee.bike";
|
|
|
|
default:
|
|
return "Teilrad";
|
|
}
|
|
}
|
|
}
|
|
}
|