using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Media.Imaging; using Avalonia.Platform.Storage; using AvaloniaApplicationTestNew.Models; using AvaloniaApplicationTestNew.Views; using Microsoft.EntityFrameworkCore; using ReactiveUI; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace AvaloniaApplicationTestNew.ViewModels { public class UserPageViewModel : ViewModelBase { string message; User current_user; List gendersList; Bitmap imageUser; bool isReadyToDelete = false; Trait selectTrait; Trait selectItem; List withoutTraits; List withTraits; public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); } public User Current_user { get => current_user; set => current_user = value; } public List GendersList { get => gendersList; set => gendersList = value; } public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); } public Trait SelectTrait { get => selectTrait; set => this.RaiseAndSetIfChanged(ref selectTrait, value); } public Trait SelectItem { get => selectItem; set => this.RaiseAndSetIfChanged(ref selectItem, value); } public List WithoutTraits { get => withoutTraits; set => this.RaiseAndSetIfChanged(ref withoutTraits, value); } public List WithTraits { get => withTraits; set => this.RaiseAndSetIfChanged(ref withTraits, value); } public bool IsReadyToDelete { get => isReadyToDelete; set => this.RaiseAndSetIfChanged(ref isReadyToDelete, value); } public UserPageViewModel(int id) { Current_user = myContext.Users.Include(x => x.IdTraits).Include(x => x.IdGenderNavigation).FirstOrDefault(x => x.IdUser == id); GendersList = myContext.Genders.ToList(); imageUser = Current_user.Image != null ? new Bitmap(new MemoryStream(Current_user.Image)) : new Bitmap("Assets\\ava.png"); WithoutTraits = myContext.Traits.ToList().Except(Current_user.IdTraits).ToList(); WithTraits = Current_user.IdTraits.ToList(); } public void SaveAndChange() { if (string.IsNullOrWhiteSpace(Current_user.Name) || Current_user.IdGenderNavigation == null || Current_user.BirthDate == null) { Message = "Заполните все поля"; } else if (DateTime.Today <= Current_user.BirthDate) { Message = "Пользователь родился в будущем? Исправьте дату"; Current_user.BirthDate = null; } else { myContext.SaveChanges(); Message = "Данные сохранены"; } } public void Back() { MainWindowViewModel.Self.AuthorizationVM = new AuthorizationViewModel(); MainWindowViewModel.Self.Page = new Authorization(); } public async Task Image() { if(Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop || desktop.MainWindow?.StorageProvider is not { } provider) throw new NullReferenceException("Missing StorageProvider instance."); var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions() { Title = "Выберите фото для аватарки", AllowMultiple = false }); await using var readStream = await files[0].OpenReadAsync(); byte[] buffer = new byte[1000000]; var bytes = readStream.ReadAtLeast(buffer, 1); Array.Resize(ref buffer, bytes); Current_user.Image = buffer; ImageUser = new Bitmap(new MemoryStream(current_user.Image)); myContext.SaveChanges(); MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser); MainWindowViewModel.Self.Page = new UserPage(); } public void DeleteImage() { MemoryStream stream = new MemoryStream(); byte[] byteArray; ImageUser = new Bitmap("Assets\\ava.png"); ImageUser.Save(stream); Current_user.Image = stream.ToArray(); myContext.SaveChanges(); MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser); MainWindowViewModel.Self.Page = new UserPage(); } public void AddTrait() { if(SelectTrait != null) { Current_user.IdTraits.Add(SelectTrait); SaveAndChange(); MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser); MainWindowViewModel.Self.Page = new UserPage(); } } public void DeleteTrait() { if(SelectItem != null) { Current_user.IdTraits.Remove(SelectItem); SaveAndChange(); MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser); MainWindowViewModel.Self.Page = new UserPage(); } else { IsReadyToDelete = true; } } } }