sharee.bike-App/TINKLib/ViewModel/MiniSurvey/MiniSurveyViewModel.cs
Anja Müller-Meißner 573fe77e12 Version 3.0.337
2022-08-30 15:42:25 +02:00

153 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Serilog;
using TINK.Model.Connector;
using TINK.Model.MiniSurvey;
using TINK.MultilingualResources;
using TINK.Repository.Exception;
using TINK.View;
using TINK.ViewModel.MiniSurvey.Question;
namespace TINK.ViewModel.MiniSurvey
{
public class MiniSurveyViewModel : ObservableCollection<IMiniSurveyQuestion>
{
/// <summary> Delegate to retrieve connected state. </summary>
protected Func<bool> IsConnectedDelegate { get; }
/// <summary> Provides a connector object.</summary>
private Func<bool, IConnector> ConnectorFactory { get; }
/// <summary>
/// Reference on view service to show modal notifications and to perform navigation.
/// </summary>
private IViewService ViewService { get; }
private MiniSurveyModel MiniSurvey { get; }
/// <summary> Constructs mini survey view model.</summary>
/// <param name="connectorFactory">Connects system to copri for purposes of requesting a bike/ cancel request.</param>
/// <param name="viewService">Interface to actuate methodes on GUI.</param>
public MiniSurveyViewModel(
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
IViewService viewService)
{
IsConnectedDelegate = isConnectedDelegate
?? throw new ArgumentException($"Can not instantiate {nameof(MiniSurveyViewModel)}-object. No connector available.");
ConnectorFactory = connectorFactory
?? throw new ArgumentException($"Can not instantiate {nameof(MiniSurveyViewModel)}-object. No connector available.");
ViewService = viewService
?? throw new ArgumentException($"Can not instantiate {nameof(MiniSurveyViewModel)}-object. No view available.");
MiniSurvey = new MiniSurveyModel
{
Title = "Bitte unterstützen Sie unsere Begleitforschung",
Subtitle = "Ihre drei Antworten werden anonym gespeichert.",
Footer = "Herzlichen Dank und viel Spaß bei der nächsten Fahrt!"
};
MiniSurvey.Questions.Add(
"q1",
new QuestionModel
{
Text = "1. Was war der Hauptzweck dieser Ausleihe?",
Type = MiniSurveyModel.Type.SingleAnswer
});
MiniSurvey.Questions.Add(
"q2",
new QuestionModel
{
Text = "2. Welches Verkehrsmittel hätten Sie ansonsten benutzt?",
Type = MiniSurveyModel.Type.SingleAnswer
});
MiniSurvey.Questions.Add(
"q3",
new QuestionModel
{
Text = "3. Haben Sie Anmerkungen oder Anregungen?",
Type = MiniSurveyModel.Type.CustomText
});
MiniSurvey.Questions["q1"].PossibleAnswers.Add("opt1", "a. Einkauf");
MiniSurvey.Questions["q1"].PossibleAnswers.Add("opt2", "b. Kinderbeförderung");
MiniSurvey.Questions["q1"].PossibleAnswers.Add("opt3", "c. Lastentransport");
MiniSurvey.Questions["q1"].PossibleAnswers.Add("opt4", "d. Freizeit");
MiniSurvey.Questions["q1"].PossibleAnswers.Add("opt5", "e. Ausprobieren");
MiniSurvey.Questions["q1"].PossibleAnswers.Add("opt6", "f. Sonstiges");
MiniSurvey.Questions["q2"].PossibleAnswers.Add("opt1", "a. Auto");
MiniSurvey.Questions["q2"].PossibleAnswers.Add("opt2", "b. Motorrad oder Motorroller");
MiniSurvey.Questions["q2"].PossibleAnswers.Add("opt3", "c. Bus oder Bahn");
MiniSurvey.Questions["q2"].PossibleAnswers.Add("opt4", "d. Eigenes Fahrrad");
MiniSurvey.Questions["q2"].PossibleAnswers.Add("opt5", "e. Zu Fuß");
MiniSurvey.Questions["q2"].PossibleAnswers.Add("opt6", "f. Keines (ich hätte die Fahrt sonst nicht gemacht)");
Title = MiniSurvey.Title;
Subtitle = MiniSurvey.Subtitle;
Footer = MiniSurvey.Footer;
foreach (var question in MiniSurvey.Questions)
{
switch (question.Value.Type)
{
case MiniSurveyModel.Type.SingleAnswer:
Add(new CheckOneViewModel(
new KeyValuePair<string, string>(question.Key, question.Value.Text),
question.Value.PossibleAnswers));
break;
case MiniSurveyModel.Type.CustomText:
Add(new FreeTextViewModel(new KeyValuePair<string, string>(question.Key, question.Value.Text)));
break;
default:
break;
}
}
}
public string Title { get; set; }
public string Subtitle { get; set; }
public string Footer { get; set; }
/// <summary> Processes request to perform a copri action (reserve bike and cancel reservation). </summary>
public System.Windows.Input.ICommand OnButtonClicked => new Xamarin.Forms.Command(async () =>
{
Log.ForContext<MiniSurveyViewModel>().Information($"User result {this[0].Answer.Value}, {this[1].Answer.Value}");
var isConnected = IsConnectedDelegate();
try
{
await ConnectorFactory(isConnected).Command.DoSubmitMiniSurvey(this.ToDictionary(x => x.Question.Key, x => x.Answer.Key));
}
catch (Exception exception)
{
if (exception is ResponseException copriException)
{
// Copri server is not reachable.
Log.ForContext<MiniSurveyViewModel>().Information("Submitting mini survay answer failed. COPRI returned an error.");
}
else
{
Log.ForContext<MiniSurveyViewModel>().Error("Submitting mini survay answer failed. {@l_oException}", exception);
}
await ViewService.DisplayAlert(
AppResources.ErrorReturnSubmitFeedbackTitle,
AppResources.ErrorReturnSubmitFeedbackMessage,
AppResources.MessageAnswerOk);
}
await ViewService.PopModalAsync();
});
}
}