using System; using System.Diagnostics.Tracing; using DialogHostAvalonia; using InABox.Avalonia.Components; using InABox.Avalonia.Router; namespace InABox.Avalonia; public interface IViewModelBase { Task Activate(); Task Deactivate(); bool BackButtonVisible { get; set; } AvaloniaMenuItemCollection PrimaryMenu { get; set; } AvaloniaMenuItemCollection SecondaryMenu { get; set; } } public interface IPopupViewModel { bool IsClosed { get; } } public interface IPopupViewModel : IPopupViewModel { TResult? GetResult(); } public static class Navigation { private static readonly HistoryRouter _router; static Navigation() { _router = new HistoryRouter(x => Activator.CreateInstance(x) as IViewModelBase); _router.CurrentViewModelChanging += (@base, direction) => CurrentViewModelChanging?.Invoke(@base, direction); _router.CurrentViewModelChanged += @base => CurrentViewModelChanged?.Invoke(@base); } public static void Back() { _router.Back(); } public static void Navigate(Action? configure = null) where T : IViewModelBase { _router.GoTo(configure); } public static async Task Popup(Action? configure = null, bool canTapAway = true) where T : IViewModelBase, IPopupViewModel { var viewModel = _router.InstantiateViewModel(configure); return await Popup(viewModel, canTapAway); } public static async Task Popup(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 Popup(Action? configure = null, bool canTapAway = true) where T : IViewModelBase, IPopupViewModel { var viewModel = _router.InstantiateViewModel(configure); return await Popup(viewModel, canTapAway); } public static async Task Popup(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 viewModel.GetResult(); } public static void Reset(Action? configure = null) where T : IViewModelBase { _router.Reset(configure); } public static event Action? CurrentViewModelChanging; public static event Action? CurrentViewModelChanged; }