From 7a978be6ca3506e9249a964eada07a327488422b Mon Sep 17 00:00:00 2001 From: Ionite Date: Sat, 11 Nov 2023 01:18:00 -0500 Subject: [PATCH] Add type parameter overload for NavigateTo --- .../Services/INavigationService.cs | 12 ++++- .../Services/NavigationService.cs | 50 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/StabilityMatrix.Avalonia/Services/INavigationService.cs b/StabilityMatrix.Avalonia/Services/INavigationService.cs index 8c8ee417..3f7d0885 100644 --- a/StabilityMatrix.Avalonia/Services/INavigationService.cs +++ b/StabilityMatrix.Avalonia/Services/INavigationService.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using FluentAvalonia.UI.Controls; using FluentAvalonia.UI.Media.Animation; using StabilityMatrix.Avalonia.Models; @@ -6,7 +7,7 @@ using StabilityMatrix.Avalonia.ViewModels.Base; namespace StabilityMatrix.Avalonia.Services; -public interface INavigationService +public interface INavigationService<[SuppressMessage("ReSharper", "UnusedTypeParameter")] T> { event EventHandler? TypedNavigation; @@ -24,6 +25,15 @@ public interface INavigationService ) where TViewModel : ViewModelBase; + /// + /// Navigate to the view of the given view model type. + /// + void NavigateTo( + Type viewModelType, + NavigationTransitionInfo? transitionInfo = null, + object? param = null + ); + /// /// Navigate to the view of the given view model. /// diff --git a/StabilityMatrix.Avalonia/Services/NavigationService.cs b/StabilityMatrix.Avalonia/Services/NavigationService.cs index c22a5eb4..79341c70 100644 --- a/StabilityMatrix.Avalonia/Services/NavigationService.cs +++ b/StabilityMatrix.Avalonia/Services/NavigationService.cs @@ -76,6 +76,56 @@ public class NavigationService : INavigationService ); } + /// + public void NavigateTo( + Type viewModelType, + NavigationTransitionInfo? transitionInfo = null, + object? param = null + ) + { + if (!viewModelType.IsAssignableFrom(typeof(ViewModelBase))) + { + // ReSharper disable once LocalizableElement + throw new ArgumentException("Type must be a ViewModelBase.", nameof(viewModelType)); + } + + if (_frame is null) + { + throw new InvalidOperationException("SetFrame was not called before NavigateTo."); + } + + if (App.Services.GetService(typeof(ISettingsManager)) is ISettingsManager settingsManager) + { + // Handle animation scale + switch (transitionInfo) + { + // If the transition info is null or animation scale is 0, suppress the transition + case null: + case BaseTransitionInfo when settingsManager.Settings.AnimationScale == 0f: + transitionInfo = new SuppressNavigationTransitionInfo(); + break; + case BaseTransitionInfo baseTransitionInfo: + baseTransitionInfo.Duration *= settingsManager.Settings.AnimationScale; + break; + } + } + + _frame.NavigateToType( + viewModelType, + param, + new FrameNavigationOptions + { + IsNavigationStackEnabled = true, + TransitionInfoOverride = transitionInfo ?? new SuppressNavigationTransitionInfo() + } + ); + + TypedNavigation?.Invoke( + this, + new TypedNavigationEventArgs { ViewModelType = viewModelType } + ); + } + /// public void NavigateTo(ViewModelBase viewModel, NavigationTransitionInfo? transitionInfo = null) {