using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using Seyounth.Core.Extensions; using Seyounth.Hyosung.Data.Entities; using Seyounth.Hyosung.Data.Models; using Seyounth.Hyosung.Data.Services; namespace Seyounth.Hyosung.ViewModels; public partial class VarietyViewModel : ObservableObject { [ObservableProperty] private ObservableCollection _varieties; [ObservableProperty] private ObservableCollection _pallets; public IEnumerable NeedTypeItems { get; } private readonly IVarietyService _varietyService; public VarietyViewModel(PalletManagerViewModel palletManagerViewModel, IVarietyService varietyService) { Pallets = new ObservableCollection(palletManagerViewModel.Pallets); _varietyService = varietyService; _varieties = new ObservableCollection(varietyService.GetAll()); var needTypes = EnumHelper.GetValues(); NeedTypeItems = needTypes.Select(nt => new NeedTypeEnumItem { Value = nt, Description = nt.GetDescription() }); } public class SavePalletCompletedMessage { } [RelayCommand] private void OnDeletePallet(object obj) { if (obj is Variety variety) { Varieties.Remove(variety); _varietyService.DeleteVarietyAsync(variety); } else { Console.Write("Object is not a Pallet"); } } [RelayCommand] private void OnSavePallet(object obj) { if (obj is Variety variety) { if (variety.Id == 0) { _varietyService.AddVarietyAsync(variety); } else _varietyService.UpdateVarietyAsync(variety); } WeakReferenceMessenger.Default.Send(new PalletManagerViewModel.SavePalletCompletedMessage()); } }