1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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<Gender> gendersList;
- Bitmap imageUser;
- public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
- public User Current_user { get => current_user; set => current_user = value; }
- public List<Gender> GendersList { get => gendersList; set => gendersList = value; }
- public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); }
- public UserPageViewModel(int id)
- {
- Current_user = myContext.Users.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");
- }
- public void SaveAndChange()
- {
- 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();
- }
- }
- }
|