Max Mannstein 0e7db09d33 Update terms acceptance logic and improve accessibility
- Initialize `_acceptedTermsMangopay` to `true` in
  `AddPaymentUserViewModel.cs` and `BecomeWalkerViewModel.cs`.
- Set `AcceptedTermsMangopay` to `true` on refresh.
- Add `AutomationId` attributes to email and password
  `Entry` elements in `LoginFormView.xaml` for better
  accessibility.
- Comment out a `Grid` element and remove the `Switch`
  control for `AcceptedTermsMangopay` in
  `AddPaymentUserView.xaml` and `BecomeWalkerView.xaml`.
2025-07-20 13:50:02 +02:00

237 lines
8.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Extensions;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using gehGassi.Dto;
using gehGassiApp.Core.Interfaces;
using gehGassiApp.Core.Messaging;
using gehGassiApp.Domain.Common;
using gehGassiApp.Domain.Users;
using gehGassiApp.Helper;
using gehGassiApp.Resources;
using gehGassiApp.Services;
using gehGassiApp.Views;
namespace gehGassiApp.ViewModels.More
{
/// <summary>
/// Viewmodel wenn ein Hundebesitzer auch ein Walker werden möchte
/// </summary>
public partial class BecomeWalkerViewmodel : MenuViewModel
{
private readonly IUserService _userService;
private readonly ICountryService _countryService;
private readonly IWalkerProfileService _walkerProfileService;
private readonly IDialogService _dialogService;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="userService">Instanz eines IUserService</param>
/// <param name="countryService">Instanz eines ICountryService</param>
/// <param name="walkerProfileService">Instanz eines IWalkerProfileService</param>
/// <param name="dialogService">Instanz eines IDialogService</param>
public BecomeWalkerViewmodel(IUserService userService, ICountryService countryService, IWalkerProfileService walkerProfileService, IDialogService dialogService)
{
_userService = userService;
_countryService = countryService;
_walkerProfileService = walkerProfileService;
_dialogService = dialogService;
Title = Text.View_Title_BecomeWalker;
//Event wenn Refresh für diesen View gesetzt werden soll
if (WeakReferenceMessenger.Default.IsRegistered<RefreshViewModelMessage>(this) == false)
WeakReferenceMessenger.Default.Register<RefreshViewModelMessage>(this, (recipient, message) =>
{
if (message.Value is Core.Messaging.Messages.Refresh_BecomeWalker or Core.Messaging.Messages.Refresh_All)
{
_refresh = true;
}
});
}
/// <summary>
/// Aktueller Benutzer
/// </summary>
[ObservableProperty]
private AppUser _appUser;
/// <summary>
/// Liste der Länder für Nationalität
/// </summary>
[ObservableProperty]
private ObservableCollection<Country> _nationalitites;
/// <summary>
/// Gewähltes Land für Nationalität
/// </summary>
[ObservableProperty]
private Country _selectedNationality;
/// <summary>
/// Liste der Länder für Hauptwohnsitz
/// </summary>
[ObservableProperty]
private ObservableCollection<Country> _mainResidences;
/// <summary>
/// Gewähltes Land für Hauptwohnsitz
/// </summary>
[ObservableProperty]
private Country _selectedMainResidence;
/// <summary>
/// Hat der Benutzer die MangoPay-AGB akzeptiert?
/// </summary>
[ObservableProperty]
private bool _acceptedTermsMangopay = true;
#region Commands
/// <summary>
/// Command für die Anzeige der AGB
/// </summary>
[RelayCommand]
public async Task ShowTermsMangoPay()
{
var menu = MenuSelected.More.ToString();
var title = Text.View_Title_Terms;
await Shell.Current.GoToAsync($"{nameof(ShowTextPageView)}?code={Core.Common.Constants.PageTermsMangoPay}&menu={menu}&title={title}", Core.Common.Constants.AnimateNavigation);
}
/// <summary>
/// Speichern
/// </summary>
/// <returns>Task</returns>
[RelayCommand]
public async Task Save()
{
IsSaving = true;
try
{
var nationality = SelectedNationality != null ? SelectedNationality.Iso2 : string.Empty;
var mainResidence = SelectedMainResidence != null ? SelectedMainResidence.Iso2 : string.Empty;
using var cts = new CancellationTokenSource(Core.Common.Constants.CreateTimeout);
var result = await _userService.SetAppUserTypeAsync(App.CurrentAppUser.Id, AppUserType.Both, nationality, mainResidence, paymentTermsAccepted: true, App.CurrentUser.AccessToken, cts.Token);
if (result.Success && result.Value)
{
//SetAppUserTypeAsync speichert die Änderungen auch in die lokale DB. Daher muss hier nur noch der aktuelle Benutzer neu geladen werden
var appUser = await _userService.GetAppUserAsync();
App.CurrentAppUser = appUser;
CurrentAppUser = appUser;
var profile = await _walkerProfileService.GetAsync(App.CurrentUser.AccessToken, cts.Token, true);
App.CurrentWalkerProfile = profile;
//Info rausgeben und weiterleiten auf "More"
var options = ResourceHelper.GetSnackbarSuccessOptions();
await _dialogService.SnackBarAsync(Resources.Text.BecomeWalker_Success, null, Resources.Text.Common_Ok, TimeSpan.FromSeconds(10), options, SnackbarAnchor);
await Shell.Current.GoToAsync($"..", Core.Common.Constants.AnimateNavigation);
}
else if (result.Success == false && result.ErrorCode == CommunicationErrors.ServerNoConnection)
{
await _dialogService.ShowAlertAsync(Resources.Text.Common_Error, Resources.Text.Error_OnlineOnly, Resources.Text.Common_Ok);
}
else
{
await _dialogService.ShowAlertAsync(Resources.Text.Common_Error, Resources.Text.Error_TryAgain, Resources.Text.Common_Ok);
}
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); }
IsSaving = false;
}
#endregion
/// <summary>
/// Laden der initialen Daten
/// </summary>
/// <returns>Task</returns>
private async Task LoadAsync()
{
var nationalities = await _countryService.GetNationalitiesAsync();
Nationalitites = nationalities.ToObservableCollection();
var residences = await _countryService.GetCountriesAsync();
MainResidences = residences.ToObservableCollection();
using var cts = new CancellationTokenSource(Core.Common.Constants.ListTimeout);
var appUser = await _userService.GetAppUserAsync(App.CurrentUser.AccessToken, cts.Token, false);
AppUser = appUser;
SelectedNationality = !string.IsNullOrWhiteSpace(AppUser.NationalityCode) ? Nationalitites.FirstOrDefault(c => c.Iso2 == AppUser.NationalityCode) : null;
SelectedMainResidence = !string.IsNullOrWhiteSpace(AppUser.MainResidenceCode) ? MainResidences.FirstOrDefault(c => c.Iso2 == AppUser.MainResidenceCode) : null;
}
#region Initialisierung
/// <summary>
/// Initialisieren des Viewmodels.
/// Soll überschrieben werden um lazy loading in Viewmodels ermöglicht wird
/// </summary>
/// <returns>Task</returns>
public override async Task InitializeAsync(object sender)
{
await base.InitializeAsync(sender);
IsBusy = false;
HasError = false;
ErrorMessage = string.Empty;
await TrackPageViewAsync("BecomeWalker");
if (_refresh)
{
SelectedNationality = null;
SelectedMainResidence = null;
AcceptedTermsMangopay = true;
IsBusy = true;
await Task.Delay(Core.Common.Constants.AnimationDelayDetails);
await LoadAsync();
IsBusy = false;
_refresh = false;
}
}
/// <summary>
/// Entladen des Viewmodels.
/// Soll überschrieben werden
/// </summary>
/// <returns>Task</returns>
public override Task DisappearingAsync(object sender)
{
IsBusy = false;
HasError = false;
ErrorMessage = string.Empty;
return base.DisappearingAsync(sender);
}
#endregion
#region Resumed
/// <summary>
/// Eventhandler wenn die App aus dem Sleepmode kommt
/// </summary>
/// <returns></returns>
internal override Task AppResumed()
{
return base.AppResumed();
}
#endregion
}
}