using System; using System.Collections.Generic; using System.Linq; using OwlCore; namespace StrixMusic.Sdk.Services.Navigation { /// /// An implementation. /// [Obsolete("This pattern is being phased out and this class should not be used. Use the Messenger pattern instead.")] public sealed class NavigationService : INavigationService { private readonly Dictionary _registeredPages = new Dictionary(); /// public event EventHandler>? NavigationRequested; /// public event EventHandler? BackRequested; /// public void NavigateTo(Type type, bool overlay = false, params object[] args) { _ = Threading.OnPrimaryThread(() => NavigateToType(type, overlay, args)); } /// public void NavigateTo(T page, bool overlay = false) { _ = Threading.OnPrimaryThread(() => NavigateToInstance(page, overlay)); } /// public void RegisterCommonPage(Type type) { _ = Threading.OnPrimaryThread(() => _registeredPages.Add(type, (T)Activator.CreateInstance(type))); } /// public void RegisterCommonPage(T type) { if (type != null) { _registeredPages.Add(type.GetType(), type); } } /// public void GoBack() { BackRequested?.Invoke(this, null); } private void NavigateToInstance(T page, bool overlay = false) { var eventArgs = new NavigateEventArgs(page, overlay); NavigationRequested?.Invoke(this, eventArgs); } private void NavigateToType(Type type, bool overlay = false, params object[] args) { T page; if (_registeredPages.ContainsKey(type)) { page = _registeredPages[type]; } else { if (args.Any()) { page = (T)Activator.CreateInstance(type, args); } else { page = (T)Activator.CreateInstance(type); } } var eventArgs = new NavigateEventArgs(page, overlay); NavigationRequested?.Invoke(this, eventArgs); } } }