|
@@ -1,32 +1,93 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
+using Avalonia.Controls.ApplicationLifetimes;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using Avalonia.Media.Imaging;
|
|
|
+using Avalonia.Platform.Storage;
|
|
|
using AvaloniaHomeTry.Models;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using ReactiveUI;
|
|
|
+using Avalonia;
|
|
|
|
|
|
namespace AvaloniaHomeTry.ViewModels
|
|
|
{
|
|
|
public class UserPageViewModel : ViewModelBase
|
|
|
{
|
|
|
- UsersTable currentUser;
|
|
|
- public UsersTable CurrentUser { get => currentUser; set => this.RaiseAndSetIfChanged(ref currentUser, value); }
|
|
|
+ UsersTable? currentUser;
|
|
|
|
|
|
- public UserPageViewModel(int idUser)
|
|
|
+ Bitmap? userImage;
|
|
|
+
|
|
|
+ List<UsersTable> mainUsers;
|
|
|
+
|
|
|
+ List<UsersTable> secondUsers;
|
|
|
+
|
|
|
+
|
|
|
+ public UsersTable? CurrentUser { get => currentUser; set => this.RaiseAndSetIfChanged(ref currentUser, value); }
|
|
|
+
|
|
|
+ public Bitmap? UserImage { get => userImage; set => this.RaiseAndSetIfChanged(ref userImage, value); }
|
|
|
+
|
|
|
+ public UserPageViewModel(int idUser)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
currentUser = db.UsersTable.Include(x => x.GenderNavigation).Include(x => x.RoleNavigation).FirstOrDefault(x => x.Id == idUser);
|
|
|
- }
|
|
|
+ UserImage = currentUser.Image != null ? new Bitmap(new MemoryStream(currentUser.Image)) : new Bitmap("Assets/background.jpg");
|
|
|
+ }
|
|
|
|
|
|
public List<string> Genders => db.Genders.Select(x => x.GenderName).ToList();
|
|
|
|
|
|
public string GenderIndex { get => currentUser.GenderNavigation.GenderName; set => currentUser.IdGender = db.Genders.FirstOrDefault(x => x.GenderName == value).Id; }
|
|
|
|
|
|
public string DR { get => currentUser.Dataofbirth.ToString(); set => currentUser.Dataofbirth = DateTime.Parse(value); }
|
|
|
+ public List<UsersTable> MainUsers { get => mainUsers; set => this.RaiseAndSetIfChanged(ref mainUsers,value); }
|
|
|
+ public List<UsersTable> SecondUsers { get => secondUsers; set => this.RaiseAndSetIfChanged(ref secondUsers, value); }
|
|
|
+
|
|
|
+ public void SaveData() => db.SaveChanges();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void ExitProfile()
|
|
|
+ {
|
|
|
+
|
|
|
+ if (currentUser.IdRole == 1)
|
|
|
+ {
|
|
|
+ MainWindowViewModel.Self.UC = new AdminPage();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ MainWindowViewModel.Self.UC = new AuthPage();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task Image(int id)
|
|
|
+ {
|
|
|
+ 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[10000000];
|
|
|
+ var bytes = readStream.ReadAtLeast(buffer, 1);
|
|
|
+ Array.Resize(ref buffer, bytes);
|
|
|
+ currentUser = db.UsersTable.FirstOrDefault(x => x.Id == id);
|
|
|
+ currentUser.Image = buffer;
|
|
|
+ UserImage = new Bitmap(new MemoryStream(currentUser.Image));
|
|
|
+ db.SaveChanges();
|
|
|
+ SecondUsers = db.UsersTable.ToList();
|
|
|
+ MainUsers = SecondUsers;
|
|
|
|
|
|
- public void SaveData() => db.SaveChanges();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public void Exit() => MainWindowViewModel.Self.UC = new AuthPage();
|
|
|
|
|
|
}
|
|
|
}
|