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 repo, IMethodSharingService sharingService, IPopupService popupService, IPopupEventHub popupEvent //, //InfoPopupViewModel infoPopupViewModel ) : base() { _Repository = repo; _sharingService = sharingService; _popupService = popupService; _popupEvent = popupEvent; //_infoPopupViewModel = infoPopupViewModel; ParticipantObject = new Participant(); ParticipantList = new ObservableCollection(); IsSaveCommandEnabled = true; _popupEvent.InfoPopupCloseRequested += infoPopupViewModel_ClosePopupRequested; PopupVisad = false; } #endregion #region Private Variables [ObservableProperty] private Participant? participantObject; [ObservableProperty] private ObservableCollection participantList; private readonly IRepository? _Repository; private readonly IMethodSharingService _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 Get() { ParticipantList = _sharingService.Get(); if (!PopupVisad) { var queryAttributes = new Dictionary { [nameof(InfoPopupViewModel.Title)] = "Deltagar bildens infopopup", [nameof(InfoPopupViewModel.Message)] = "Deltagare laddade", [nameof(InfoPopupViewModel.Name)] = "Urban", }; _popupService.ShowPopup( 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 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, EventArgs e) { PopupVisad = true; await _popupService.ClosePopupAsync(Shell.Current); } }