Nu fungerar Popupen och den går att stänga

This commit is contained in:
2025-10-17 11:52:01 +02:00
parent 21eb0d5498
commit bb8f4bd5ed
12 changed files with 272 additions and 23 deletions

View File

@ -0,0 +1,50 @@
using CommunityToolkit.Maui;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GreadyPoang.Services;
namespace GreadyPoang.ViewModelLayer;
public partial class InfoPopupViewModel : ObservableObject, IQueryAttributable
{
private readonly IPopupService _popupService;
private readonly IPopupEventHub _popupEvent;
public event EventHandler ClosePopupRequested;
[ObservableProperty]
private string title;
[ObservableProperty]
private string message;
[ObservableProperty]
private string name;
public InfoPopupViewModel(IPopupService popupService, IPopupEventHub popupEvent)
{
_popupService = popupService;
_popupEvent = popupEvent;
}
[RelayCommand]
private async Task Cancel()
{
//await _popupService.ClosePopupAsync(Shell.Current);
_popupEvent.RaiseInfoPopupClose();
}
[RelayCommand(CanExecute = nameof(CanSave))]
void OnSave()
{
}
bool CanSave() => string.IsNullOrWhiteSpace(Name) is false;
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
Title = (string)query[nameof(InfoPopupViewModel.Title)];
Message = (string)query[nameof(InfoPopupViewModel.Message)];
Name = (string)query[nameof(InfoPopupViewModel.Name)];
}
}

View File

@ -1,13 +1,16 @@
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 : ObservableObject
public partial class ParticipantViewModel : BaseViewModel
{
#region Constructors
public ParticipantViewModel() : base()
@ -15,16 +18,29 @@ public partial class ParticipantViewModel : ObservableObject
}
public ParticipantViewModel(IRepository<Participant> repo, IMethodSharingService<Participant> sharingService) : 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;
_popupEvent.InfoPopupCloseRequested += infoPopupViewModel_ClosePopupRequested;
PopupVisad = false;
}
#endregion
#endregion
#region Private Variables
[ObservableProperty]
@ -33,17 +49,40 @@ public partial class ParticipantViewModel : ObservableObject
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;
#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)] = "Urban",
};
_popupService.ShowPopup<InfoPopupViewModel>(
Shell.Current,
options: PopupOptions.Empty,
shellParameters: queryAttributes);
}
return ParticipantList;
}
@ -68,25 +107,47 @@ public partial class ParticipantViewModel : ObservableObject
#endregion
[RelayCommand(CanExecute = nameof(IsSaveCommandEnabled))]
private async Task<bool> Save()
private async Task Save()
{
if (_Repository == null || ParticipantObject == null)
await RunAsyncCommand(async () =>
{
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;
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)
{
@ -99,4 +160,26 @@ public partial class ParticipantViewModel : ObservableObject
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, EventArgs e)
{
PopupVisad = true;
await _popupService.ClosePopupAsync(Shell.Current);
}
}