From a394918064112bba7a0d8d038b2791fc69890383 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Wed, 25 Sep 2024 22:27:35 +0200 Subject: [PATCH] Intermediate, add dynamic data, not yet w/o errors/warnings --- Stars Assistant/Services/Game.cs | 1 + Stars Assistant/Services/PlanetManager.cs | 36 ++++++++++++++++++++ Stars Assistant/ViewModels/BuColViewModel.cs | 17 +++++++-- Stars Assistant/Views/BuColView.axaml | 7 ++-- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 Stars Assistant/Services/PlanetManager.cs diff --git a/Stars Assistant/Services/Game.cs b/Stars Assistant/Services/Game.cs index e44edfc..9651926 100644 --- a/Stars Assistant/Services/Game.cs +++ b/Stars Assistant/Services/Game.cs @@ -123,6 +123,7 @@ public class Game GameEngine.Game = this; Locator.CurrentMutable.RegisterConstant(new CSVDataLoader(), typeof(CSVDataLoader)); Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase)); + Locator.CurrentMutable.RegisterConstant(new PlanetManager(), typeof(Services.PlanetManager)); // TESTING HELPER if (__doCreateTestData) diff --git a/Stars Assistant/Services/PlanetManager.cs b/Stars Assistant/Services/PlanetManager.cs new file mode 100644 index 0000000..83cc7de --- /dev/null +++ b/Stars Assistant/Services/PlanetManager.cs @@ -0,0 +1,36 @@ +using System; +using System.ComponentModel; +using System.Reactive.Linq; +using DynamicData; +using DynamicData.Binding; +using Splat; +using StarsAssistant.Model; +using StarsAssistant.ViewModels; + +namespace StarsAssistant.Services; + +public class PlanetManager +{ + protected Services.Game Game = Locator.Current.GetService()!; + + private SourceCache _planets = new(p => p.Name); + + public IObservable> PlayerPlanetsSource + => _planets + .Connect() + .Filter(planet => planet.OwnerId == Game.Player.Name) + .Transform(planet => new PlayerPlanetViewModel(planet)); + + public PlanetManager() + { + } + + public void __test__Init() + { + using var db = Locator.Current.GetService()!; + { + var playerPlanets = db.Planet.ToList(); + _planets.AddOrUpdate(playerPlanets); + } + } +} diff --git a/Stars Assistant/ViewModels/BuColViewModel.cs b/Stars Assistant/ViewModels/BuColViewModel.cs index e5c3177..7e11abe 100644 --- a/Stars Assistant/ViewModels/BuColViewModel.cs +++ b/Stars Assistant/ViewModels/BuColViewModel.cs @@ -1,17 +1,31 @@ using System.Collections.ObjectModel; using System.Reactive.Disposables; using System.Reactive.Linq; - +using DynamicData; using ReactiveUI; using ReactiveUI.SourceGenerators; using Splat; +using StarsAssistant.Model; +using StarsAssistant.Services; namespace StarsAssistant.ViewModels; public partial class BuColViewModel : ViewModelBase { + private readonly ReadOnlyObservableCollection _playerPlanetsView; + public ReadOnlyObservableCollection PlayerPlanets => _playerPlanetsView; + public BuColViewModel() { + var PlanetManager = Locator.Current.GetService()!; + PlanetManager.__test__Init(); + + PlanetManager.PlayerPlanetsSource + .ObserveOn(RxApp.MainThreadScheduler) + .Bind(out _playerPlanetsView) + .DisposeMany() + .Subscribe(); + this.WhenActivated((CompositeDisposable disposables) => { // /* handle activation */ @@ -21,5 +35,4 @@ public partial class BuColViewModel : ViewModelBase }); } - public ObservableCollection Planets { get; } = PlanetViewModel.LoadAll(); } diff --git a/Stars Assistant/Views/BuColView.axaml b/Stars Assistant/Views/BuColView.axaml index 36da30f..fca7883 100644 --- a/Stars Assistant/Views/BuColView.axaml +++ b/Stars Assistant/Views/BuColView.axaml @@ -9,9 +9,10 @@ @@ -21,8 +22,8 @@ - - + +