AddSmeshViewModel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using System.Threading.Tasks;
  7. using Avalonia.Media.Imaging;
  8. using Avalonia.Platform.Storage;
  9. using AvaloniaApplication1.Models;
  10. using ReactiveUI;
  11. using Avalonia;
  12. using System.Threading;
  13. using System.ComponentModel;
  14. using System.Runtime.CompilerServices;
  15. using System.Windows.Input;
  16. using System.Collections.ObjectModel;
  17. using Avalonia.Threading;
  18. using Microsoft.EntityFrameworkCore;
  19. namespace AvaloniaApplication1.ViewModels
  20. {
  21. public class AddSmeshViewModel : ViewModelBase
  22. {
  23. string name;
  24. string animal;
  25. List<Gender> genders;
  26. List<Property> property;
  27. List<Property> property2;
  28. List<Property> smeshProperty;
  29. Property selectedProperty ;
  30. Gender selectedGender;
  31. int age;
  32. Bitmap imageSmesh;
  33. byte[] image;
  34. public ObservableCollection<Property> Items { get; } = new ObservableCollection<Property>();
  35. public ICommand AddItemCommand { get; }
  36. public string Name { get=> name; set => this.RaiseAndSetIfChanged(ref name, value); }
  37. public string Animal { get => animal; set => this.RaiseAndSetIfChanged(ref animal, value); }
  38. public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
  39. public List<Property> Property { get => property; set => this.RaiseAndSetIfChanged(ref property, value); }
  40. public List<Property> Property2 { get => property2; set => this.RaiseAndSetIfChanged(ref property2, value); }
  41. public List<Property> SmeshProperty { get => smeshProperty; set => this.RaiseAndSetIfChanged(ref smeshProperty, value); }
  42. public Gender SelectedGender { get => selectedGender; set => this.RaiseAndSetIfChanged(ref selectedGender, value); }
  43. public Property SelectedProperty { get => selectedProperty; set { selectedProperty = value; AddNewItem(); } }
  44. public int Age { get => age; set => this.RaiseAndSetIfChanged(ref age, value); }
  45. public Bitmap ImageSmesh { get => imageSmesh; set => this.RaiseAndSetIfChanged(ref imageSmesh, value); }
  46. public byte[] Image { get => image; set => this.RaiseAndSetIfChanged(ref image, value); }
  47. public AddSmeshViewModel()
  48. {
  49. property = myConnection.Properties.ToList();
  50. property2 = myConnection.Properties.ToList();
  51. selectedProperty = property.FirstOrDefault();
  52. genders = myConnection.Genders.ToList();
  53. }
  54. public async Task AddImage()
  55. {
  56. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  57. desktop.MainWindow?.StorageProvider is not { } provider)
  58. throw new NullReferenceException("Missing StorageProvider instance.");
  59. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  60. {
  61. Title = "Ôîòî ñìåøàðèêà",
  62. AllowMultiple = false
  63. });
  64. await using var readStream = await files[0].OpenReadAsync();
  65. byte[] buffer = new byte[10000000];
  66. var bytes = readStream.ReadAtLeast(buffer, 1);
  67. Array.Resize(ref buffer, bytes);
  68. Image = buffer;
  69. ImageSmesh = new Bitmap(new MemoryStream(buffer));
  70. }
  71. public void Back() => MainWindowViewModel.Self.Uc = new AdminPage();
  72. public void Save()
  73. {
  74. myConnection.Add(new Smeshariki()
  75. {
  76. Name = Name,
  77. Age = Age,
  78. Gender = SelectedGender.Id,
  79. Animal = Animal,
  80. Image = Image,
  81. IdProperties = Items
  82. });
  83. myConnection.SaveChanges();
  84. MainWindowViewModel.Self.Uc = new AdminPage();
  85. }
  86. private async void AddNewItem()
  87. {
  88. var newItem = SelectedProperty;
  89. await Dispatcher.UIThread.InvokeAsync(() =>
  90. {
  91. if(newItem != null)
  92. {
  93. Items.Add(newItem);
  94. Property = Property.Except(Items).ToList();
  95. }
  96. });
  97. }
  98. public async void DeleteItem(int id)
  99. {
  100. var newItem = await myConnection.Properties.FirstOrDefaultAsync(x=> x.Id ==id);
  101. await Dispatcher.UIThread.InvokeAsync(() =>
  102. {
  103. if (newItem != null)
  104. {
  105. Items.Remove(newItem);
  106. Property = Property2;
  107. Property = Property.Except(Items).ToList();
  108. }
  109. });
  110. }
  111. public event PropertyChangedEventHandler PropertyChanged;
  112. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  113. {
  114. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  115. }
  116. }
  117. }