Browse Source

Added popups

Kenric Nugteren 4 months ago
parent
commit
cb4e1d0b98
2 changed files with 53 additions and 1 deletions
  1. 52 0
      InABox.Avalonia/Navigation/Navigation.cs
  2. 1 1
      InABox.Avalonia/Router/Router.cs

+ 52 - 0
InABox.Avalonia/Navigation/Navigation.cs

@@ -1,4 +1,6 @@
 using System;
+using System.Diagnostics.Tracing;
+using DialogHostAvalonia;
 using InABox.Avalonia.Components;
 using InABox.Avalonia.Router;
 
@@ -14,6 +16,16 @@ public interface IViewModelBase
     AvaloniaMenuItemCollection SecondaryMenu { get; set; }
 }
 
+public interface IPopupViewModel
+{
+    bool IsClosed { get; }
+}
+
+public interface IPopupViewModel<TResult> : IPopupViewModel
+{
+    TResult? GetResult();
+}
+
 public static class Navigation
 {
     private static readonly HistoryRouter<IViewModelBase> _router;
@@ -35,6 +47,46 @@ public static class Navigation
         _router.GoTo<T>(configure);
     }
 
+    public static async Task<object?> Popup<T>(Action<T>? configure = null, bool canTapAway = true)
+        where T : IViewModelBase, IPopupViewModel
+    {
+        var viewModel = _router.InstantiateViewModel<T>(configure);
+        return await Popup(viewModel, canTapAway);
+    }
+
+    public static async Task<object?> Popup<T>(T viewModel, bool canTapAway = true)
+        where T : IViewModelBase, IPopupViewModel
+    {
+        var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
+        {
+            if(!canTapAway && !viewModel.IsClosed)
+            {
+                eventArgs.Cancel();
+            }
+        });
+        return _result;
+    }
+
+    public static async Task<TResult?> Popup<T, TResult>(Action<T>? configure = null, bool canTapAway = true)
+        where T : IViewModelBase, IPopupViewModel<TResult>
+    {
+        var viewModel = _router.InstantiateViewModel<T>(configure);
+        return await Popup<T, TResult>(viewModel, canTapAway);
+    }
+
+    public static async Task<TResult?> Popup<T, TResult>(T viewModel, bool canTapAway = true)
+        where T : IViewModelBase, IPopupViewModel<TResult>
+    {
+        var _result = await DialogHostAvalonia.DialogHost.Show(viewModel, (object sender, DialogClosingEventArgs eventArgs) =>
+        {
+            if(!canTapAway && !viewModel.IsClosed)
+            {
+                eventArgs.Cancel();
+            }
+        });
+        return viewModel.GetResult();
+    }
+
     public static void Reset<T>(Action<T>? configure = null) where T : IViewModelBase
     {
         _router.Reset<T>(configure);

+ 1 - 1
InABox.Avalonia/Router/Router.cs

@@ -36,7 +36,7 @@ public class Router<TViewModelBase> where TViewModelBase:class
         return viewModel;
     }
 
-    protected T InstantiateViewModel<T>(Action<T>? configure) where T:TViewModelBase
+    public T InstantiateViewModel<T>(Action<T>? configure) where T:TViewModelBase
     {
         var result = (T)Convert.ChangeType(CreateViewModel(typeof(T)), typeof(T));
         configure?.Invoke(result);