Files
GreadyPoang/GreadyPoang.ViewModelLayer/ViewModelClasses/ParticipantViewModel.cs

190 lines
5.2 KiB
C#

using Common.Library;
using CommunityToolkit.Maui;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GreadyPoang.Core;
using GreadyPoang.EntityLayer;
using GreadyPoang.Services;
using System.Collections.ObjectModel;
namespace GreadyPoang.ViewModelLayer;
public partial class ParticipantViewModel : BaseViewModel
{
#region Constructors
public ParticipantViewModel() : base()
{
}
public ParticipantViewModel(
IRepository<Participant> repo,
IMethodSharingService<Participant> sharingService,
IPopupService popupService,
IPopupEventHub popupEvent
//,
//InfoPopupViewModel infoPopupViewModel
) : base()
{
_Repository = repo;
_sharingService = sharingService;
_popupService = popupService;
_popupEvent = popupEvent;
//_infoPopupViewModel = infoPopupViewModel;
ParticipantObject = new Participant();
ParticipantList = new ObservableCollection<Participant>();
IsSaveCommandEnabled = true;
PopupVisad = false;
_popupEvent.InfoPopupCloseRequested += infoPopupViewModel_ClosePopupRequested;
}
#endregion
#region Private Variables
[ObservableProperty]
private Participant? participantObject;
[ObservableProperty]
private ObservableCollection<Participant> participantList;
private readonly IRepository<Participant>? _Repository;
private readonly IMethodSharingService<Participant> _sharingService;
private readonly IPopupService _popupService;
private readonly IPopupEventHub _popupEvent;
private readonly InfoPopupViewModel _infoPopupViewModel;
private string _activePopupId;
#endregion
[ObservableProperty]
private bool isSaveCommandEnabled;
public bool PopupVisad { get; set; }
#region Get Method
public ObservableCollection<Participant> Get()
{
ParticipantList = _sharingService.Get();
if (!PopupVisad)
{
var queryAttributes = new Dictionary<string, object>
{
[nameof(InfoPopupViewModel.Title)] = "Deltagar bildens infopopup",
[nameof(InfoPopupViewModel.Message)] = "Deltagare laddade",
[nameof(InfoPopupViewModel.Name)] = " ",
};
_popupService.ShowPopup<InfoPopupViewModel>(
Shell.Current,
options: PopupOptions.Empty,
shellParameters: queryAttributes);
}
return ParticipantList;
}
#endregion
#region Get(id) Method
public Participant? Get(int id)
{
try
{
ParticipantObject = _Repository?.Get(id).GetAwaiter().GetResult();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
}
return ParticipantObject;
}
#endregion
[RelayCommand(CanExecute = nameof(IsSaveCommandEnabled))]
private async Task Save()
{
await RunAsyncCommand(async () =>
{
if (_Repository == null || ParticipantObject == null)
{
return;
}
await Task.Delay(3600); // Simulerar en fördröjning för att visa laddningsindikatorn
var tmpTask = _Repository.Save(ParticipantObject);
int tmp = await tmpTask;
if (tmp != -1)
{
ParticipantObject = new Participant();
this.Get();
await Shell.Current.GoToAsync("..");
}
}, loadingMessage: "Sparar deltagare...", errorMessage: "Fel vid sparande av deltagare");
}
//[RelayCommand(CanExecute = nameof(IsSaveCommandEnabled))]
//private async Task<bool> Save()
//{
// if (_Repository == null || ParticipantObject == null)
// {
// return false;
// }
// var tmpTask = _Repository.Save(ParticipantObject);
// int tmp = tmpTask.GetAwaiter().GetResult();
// if (tmp != -1)
// {
// ParticipantObject = new Participant();
// this.Get();
// await Shell.Current.GoToAsync("..");
// }
// return tmp != -1;
//}
[RelayCommand]
private void DeleteAsync(Participant pp)
{
Console.WriteLine($"Valt från ViewModel: {pp.FullName}");
if (_Repository == null || pp == null || pp.ParticipantId <= 0)
{
return;
}
var tmpTask = _Repository.Delete(pp);
this.Get();
Shell.Current.GoToAsync("..");
}
[RelayCommand]
private async Task LoadDataAsync()
{
await RunAsyncCommand(async () =>
{
await Task.Delay(1500); // Simulerar laddning
Console.WriteLine("Data laddad!");
}, loadingMessage: "Laddar data...");
}
//private void ClosePopup()
//{
// _popupService.ClosePopupAsync(Shell.Current).GetAwaiter().GetResult();
//}
private async void infoPopupViewModel_ClosePopupRequested(object? sender, PopupCloseEventArgs e)
{
if (e.PopupId != _activePopupId)
{
return;
}
PopupVisad = true;
await _popupService.ClosePopupAsync(Shell.Current);
}
}