diff --git a/StabilityMatrix.Avalonia/Controls/DropTargetTemplatedControlBase.cs b/StabilityMatrix.Avalonia/Controls/DropTargetTemplatedControlBase.cs index 8bd10e05..daa54ab2 100644 --- a/StabilityMatrix.Avalonia/Controls/DropTargetTemplatedControlBase.cs +++ b/StabilityMatrix.Avalonia/Controls/DropTargetTemplatedControlBase.cs @@ -1,10 +1,9 @@ -using Avalonia.Controls.Primitives; -using Avalonia.Input; +using Avalonia.Input; using StabilityMatrix.Avalonia.ViewModels; namespace StabilityMatrix.Avalonia.Controls; -public abstract class DropTargetTemplatedControlBase : TemplatedControl +public abstract class DropTargetTemplatedControlBase : TemplatedControlBase { protected DropTargetTemplatedControlBase() { diff --git a/StabilityMatrix.Avalonia/Controls/ImageFolderCard.axaml.cs b/StabilityMatrix.Avalonia/Controls/ImageFolderCard.axaml.cs index bdaf351b..b36e1fe1 100644 --- a/StabilityMatrix.Avalonia/Controls/ImageFolderCard.axaml.cs +++ b/StabilityMatrix.Avalonia/Controls/ImageFolderCard.axaml.cs @@ -1,31 +1,9 @@ -using AsyncAwaitBestPractices; -using Avalonia.Controls; -using Avalonia.Input; -using Avalonia.Interactivity; -using Avalonia.Threading; -using StabilityMatrix.Avalonia.ViewModels.Base; +using Avalonia.Input; namespace StabilityMatrix.Avalonia.Controls; public class ImageFolderCard : DropTargetTemplatedControlBase { - /// - protected override void OnLoaded(RoutedEventArgs e) - { - base.OnLoaded(e); - - if (DataContext is ViewModelBase vm) - { - vm.OnLoaded(); - Dispatcher.UIThread - .InvokeAsync(async () => - { - await vm.OnLoadedAsync(); - }) - .SafeFireAndForget(); - } - } - /// protected override void DropHandler(object? sender, DragEventArgs e) { diff --git a/StabilityMatrix.Avalonia/Controls/TemplatedControlBase.cs b/StabilityMatrix.Avalonia/Controls/TemplatedControlBase.cs new file mode 100644 index 00000000..4d8fb94d --- /dev/null +++ b/StabilityMatrix.Avalonia/Controls/TemplatedControlBase.cs @@ -0,0 +1,40 @@ +using AsyncAwaitBestPractices; +using Avalonia.Controls.Primitives; +using Avalonia.Interactivity; +using Avalonia.Threading; +using StabilityMatrix.Avalonia.ViewModels.Base; + +namespace StabilityMatrix.Avalonia.Controls; + +public abstract class TemplatedControlBase : TemplatedControl +{ + /// + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + + if (DataContext is not ViewModelBase viewModel) + return; + + // Run synchronous load then async load + viewModel.OnLoaded(); + + // Can't block here so we'll run as async on UI thread + Dispatcher.UIThread.InvokeAsync(viewModel.OnLoadedAsync).SafeFireAndForget(); + } + + /// + protected override void OnUnloaded(RoutedEventArgs e) + { + base.OnUnloaded(e); + + if (DataContext is not ViewModelBase viewModel) + return; + + // Run synchronous load then async load + viewModel.OnUnloaded(); + + // Can't block here so we'll run as async on UI thread + Dispatcher.UIThread.InvokeAsync(viewModel.OnUnloadedAsync).SafeFireAndForget(); + } +} diff --git a/StabilityMatrix.Avalonia/ViewModels/Inference/ImageFolderCardViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Inference/ImageFolderCardViewModel.cs index 532b4bf5..e8097834 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Inference/ImageFolderCardViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Inference/ImageFolderCardViewModel.cs @@ -6,9 +6,6 @@ using AsyncAwaitBestPractices; using AsyncImageLoader; using Avalonia; using Avalonia.Controls.Notifications; -using Avalonia.Controls.Primitives; -using Avalonia.Input; -using Avalonia.Layout; using Avalonia.Platform.Storage; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; @@ -17,7 +14,6 @@ using DynamicData; using DynamicData.Binding; using FuzzySharp; using FuzzySharp.PreProcess; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SkiaSharp; using StabilityMatrix.Avalonia.Controls; @@ -27,8 +23,6 @@ using StabilityMatrix.Avalonia.Models; using StabilityMatrix.Avalonia.Services; using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.ViewModels.Dialogs; -using StabilityMatrix.Avalonia.Views; -using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Models.Database; using StabilityMatrix.Core.Models.FileInterfaces;