|
@@ -0,0 +1,83 @@
|
|
|
+using Avalonia.Media.Imaging;
|
|
|
+using HanumanInstitute.MvvmDialogs;
|
|
|
+using ReactiveUI;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using VeloeAvaloniaKemonoPartyApp.Models;
|
|
|
+
|
|
|
+namespace VeloeAvaloniaKemonoPartyApp.ViewModels
|
|
|
+{
|
|
|
+ public class ImageZoomViewModel : ViewModelBase, IModalDialogViewModel, IViewClosing, IViewLoaded, ICloseable
|
|
|
+ {
|
|
|
+ private readonly Attachment _attachment;
|
|
|
+
|
|
|
+ public event EventHandler? RequestClose;
|
|
|
+ public bool? DialogResult => true;
|
|
|
+
|
|
|
+ public ReactiveCommand<System.Reactive.Unit, System.Reactive.Unit> Close { get; }
|
|
|
+
|
|
|
+ public ImageZoomViewModel(Attachment attachment)
|
|
|
+ {
|
|
|
+ _attachment = attachment;
|
|
|
+ Close = ReactiveCommand.Create(CloseImpl);
|
|
|
+ LoadAvatar();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Bitmap? _cover;
|
|
|
+
|
|
|
+ public Bitmap? Cover
|
|
|
+ {
|
|
|
+ get => _cover;
|
|
|
+ private set => this.RaiseAndSetIfChanged(ref _cover, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public UVtools.AvaloniaControls.AdvancedImageBox.SizeModes SizeModeValue => UVtools.AvaloniaControls.AdvancedImageBox.SizeModes.Fit;
|
|
|
+
|
|
|
+ public async Task LoadAvatar()
|
|
|
+ {
|
|
|
+ await using (var imageStream = await _attachment.LoadCoverBitmapAsync())
|
|
|
+ {
|
|
|
+ if (imageStream is null) return;
|
|
|
+ Cover = await Task.Run(() =>
|
|
|
+ {
|
|
|
+ Bitmap? bitmap;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ bitmap = new Bitmap(imageStream);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ bitmap = null;
|
|
|
+ }
|
|
|
+ return bitmap;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnLoaded()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnClosing(CancelEventArgs e)
|
|
|
+ {
|
|
|
+ e.Cancel = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CloseImpl()
|
|
|
+ {
|
|
|
+ RequestClose?.Invoke(this, EventArgs.Empty);
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task OnClosingAsync(CancelEventArgs e)
|
|
|
+ {
|
|
|
+ //_cancellationTokenSource?.Cancel();
|
|
|
+ e.Cancel = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|