From 7e1928c3cb93e8d2e9de2d3dc79b2d9d3c40f3ec Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Mon, 2 Feb 2026 20:52:05 +0100 Subject: [PATCH] Fixes the dependency loop. --- Gamenight.Ui/Gamenight.Ui/App.axaml.cs | 6 ++++- Gamenight.Ui/Gamenight.Ui/AppBootstrapper.cs | 26 +++++++++++++++++++ .../ViewModels/GamenightsViewModel.cs | 5 ---- .../Gamenight.Ui/ViewModels/MainViewModel.cs | 12 +++++---- .../ViewModels/SideBarViewModel.cs | 16 ++++++------ .../Gamenight.Ui/Views/GamenightsView.axaml | 9 ++++--- .../Gamenight.Ui/Views/MainView.axaml | 2 +- 7 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 Gamenight.Ui/Gamenight.Ui/AppBootstrapper.cs diff --git a/Gamenight.Ui/Gamenight.Ui/App.axaml.cs b/Gamenight.Ui/Gamenight.Ui/App.axaml.cs index 5adb465..3e1e7f7 100644 --- a/Gamenight.Ui/Gamenight.Ui/App.axaml.cs +++ b/Gamenight.Ui/Gamenight.Ui/App.axaml.cs @@ -27,6 +27,9 @@ public partial class App : Application { // Register all the services needed for the application to run var collection = new ServiceCollection(); + + new AppBootstrapper(collection, new RoutingState()); + var state = new GamenightState(); collection.AddSingleton(state); @@ -38,7 +41,6 @@ public partial class App : Application ); collection.AddSingleton(gamenightApi); collection.AddSingleton(); - collection.AddSingleton(sp => sp.GetRequiredService()); collection.AddSingleton(); collection.AddSingleton(); collection.AddSingleton(); @@ -51,6 +53,8 @@ public partial class App : Application collection.AddSingleton(); } + RxApp.SuspensionHost.GetAppState(); + ServiceProvider = collection.BuildServiceProvider(); var mainViewModel = ServiceProvider.GetRequiredService(); diff --git a/Gamenight.Ui/Gamenight.Ui/AppBootstrapper.cs b/Gamenight.Ui/Gamenight.Ui/AppBootstrapper.cs new file mode 100644 index 0000000..fe9495a --- /dev/null +++ b/Gamenight.Ui/Gamenight.Ui/AppBootstrapper.cs @@ -0,0 +1,26 @@ +using ReactiveUI; +using Splat; +using System; +using System.Collections.Generic; +using System.Diagnostics.Metrics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; + +namespace Gamenight.Ui +{ + public class AppBootstrapper : ReactiveObject, IScreen + { + public AppBootstrapper(ServiceCollection collection, RoutingState router) + { + Router = router ?? new RoutingState(); + + collection.AddSingleton(this); + } + + + + public RoutingState Router { get; } + } +} diff --git a/Gamenight.Ui/Gamenight.Ui/ViewModels/GamenightsViewModel.cs b/Gamenight.Ui/Gamenight.Ui/ViewModels/GamenightsViewModel.cs index a4a60ef..a5b863c 100644 --- a/Gamenight.Ui/Gamenight.Ui/ViewModels/GamenightsViewModel.cs +++ b/Gamenight.Ui/Gamenight.Ui/ViewModels/GamenightsViewModel.cs @@ -16,9 +16,4 @@ public class GamenightsViewModel : ReactiveObject, IRoutableViewModel GamenightApi = gamenightApi; HostScreen = hostScreen; } - - public GamenightsViewModel() - { - throw new NotImplementedException(); - } } \ No newline at end of file diff --git a/Gamenight.Ui/Gamenight.Ui/ViewModels/MainViewModel.cs b/Gamenight.Ui/Gamenight.Ui/ViewModels/MainViewModel.cs index 47c1e9e..84267f3 100644 --- a/Gamenight.Ui/Gamenight.Ui/ViewModels/MainViewModel.cs +++ b/Gamenight.Ui/Gamenight.Ui/ViewModels/MainViewModel.cs @@ -6,7 +6,7 @@ using ReactiveUI.SourceGenerators; namespace Gamenight.Ui.ViewModels; -public partial class MainViewModel : ReactiveObject, IScreen +public partial class MainViewModel : ReactiveObject { [ObservableAsProperty] private string _greeting = "Welcome to Avalonia!"; @@ -14,17 +14,19 @@ public partial class MainViewModel : ReactiveObject, IScreen public HeaderViewModel HeaderViewModel { get; } public SideBarViewModel SideBarViewModel { get; } - public RoutingState Router { get; } = new RoutingState(); - [ObservableAsProperty] private IReactiveCommand _loginCommand; - public MainViewModel(IGamenightApi gamenightApi, HeaderViewModel headerViewModel, SideBarViewModel sideBarViewModel) + [ObservableAsProperty] + private IScreen _screen; + + public MainViewModel(IGamenightApi gamenightApi, IScreen screen, HeaderViewModel headerViewModel, SideBarViewModel sideBarViewModel) { GamenightApi = gamenightApi; + _screen = screen; + HeaderViewModel = headerViewModel; SideBarViewModel = sideBarViewModel; - SideBarViewModel.Screen = this; _loginCommand = ReactiveCommand.Create(Test); } diff --git a/Gamenight.Ui/Gamenight.Ui/ViewModels/SideBarViewModel.cs b/Gamenight.Ui/Gamenight.Ui/ViewModels/SideBarViewModel.cs index d6eba10..0b0b3da 100644 --- a/Gamenight.Ui/Gamenight.Ui/ViewModels/SideBarViewModel.cs +++ b/Gamenight.Ui/Gamenight.Ui/ViewModels/SideBarViewModel.cs @@ -6,6 +6,13 @@ namespace Gamenight.Ui.ViewModels; public partial class SideBarViewModel : ReactiveObject { + public SideBarViewModel(IScreen screen, GamenightsViewModel gamenightsViewModel) + { + Screen = screen; + PushViewModel = ReactiveCommand.CreateFromObservable((IRoutableViewModel x) => Screen.Router.Navigate.Execute(x)); + GamenightsViewModel = gamenightsViewModel; + } + public IReactiveCommand PushViewModel { get; @@ -18,12 +25,5 @@ public partial class SideBarViewModel : ReactiveObject set => this.RaiseAndSetIfChanged(ref field, value); } - public IScreen Screen { get; set; } - - - public SideBarViewModel(GamenightsViewModel gamenightsViewModel) - { - PushViewModel = ReactiveCommand.CreateFromObservable((IRoutableViewModel x) => Screen.Router.Navigate.Execute(x)); - GamenightsViewModel = gamenightsViewModel; - } + private IScreen Screen { get; } } \ No newline at end of file diff --git a/Gamenight.Ui/Gamenight.Ui/Views/GamenightsView.axaml b/Gamenight.Ui/Gamenight.Ui/Views/GamenightsView.axaml index b8e4125..efafd45 100644 --- a/Gamenight.Ui/Gamenight.Ui/Views/GamenightsView.axaml +++ b/Gamenight.Ui/Gamenight.Ui/Views/GamenightsView.axaml @@ -6,9 +6,10 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Gamenight.Ui.Views.GamenightsView" x:DataType="vm:GamenightsViewModel"> - - - - + + + \ No newline at end of file diff --git a/Gamenight.Ui/Gamenight.Ui/Views/MainView.axaml b/Gamenight.Ui/Gamenight.Ui/Views/MainView.axaml index ec1c3fa..b155522 100644 --- a/Gamenight.Ui/Gamenight.Ui/Views/MainView.axaml +++ b/Gamenight.Ui/Gamenight.Ui/Views/MainView.axaml @@ -26,7 +26,7 @@ - +