From 4789904284afe37441be53d0d4e1029ea64e8bb2 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Wed, 27 Nov 2024 18:40:14 +0100 Subject: [PATCH] Intermediate, fixes a lot of update problems, keeping selection in place when updating planets still broken. --- Stars Assistant/Services/PlanetManager.cs | 18 ++++++++++++- Stars Assistant/ViewModels/BuColViewModel.cs | 27 ++++++++++++++----- .../ViewModels/PlayerPlanetViewModel.cs | 5 +++- Stars Assistant/Views/BuColView.axaml | 5 ++-- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Stars Assistant/Services/PlanetManager.cs b/Stars Assistant/Services/PlanetManager.cs index e532f20..72cd2be 100644 --- a/Stars Assistant/Services/PlanetManager.cs +++ b/Stars Assistant/Services/PlanetManager.cs @@ -1,6 +1,9 @@ using System; using System.ComponentModel; +using System.Reactive; +using System.Reactive.Disposables; using System.Reactive.Linq; +using System.Reactive.Subjects; using DynamicData; using DynamicData.Binding; using Splat; @@ -27,10 +30,21 @@ public class PlanetManager : IDisposable, IEnableLogger .Filter(planet => planet.OwnerId == Game.Player.Name) .Transform(planet => new PlayerPlanetViewModel(planet)); + /// + /// Observable subject to trigger when we update the planet cache, allows the + /// UI to react to it. + /// + private readonly Subject _planetsReloadedSubject = new(); + + /// + /// Exposes _planetReloadedSubject as IObservable. + /// + public IObservable PlanetsReloaded => _planetsReloadedSubject.AsObservable(); + + public PlanetManager() { FleetManager fleetManager = Locator.Current.GetService()!; - } /// @@ -50,6 +64,7 @@ public class PlanetManager : IDisposable, IEnableLogger innerCache.AddOrUpdate(allPlanets); } ); + _planetsReloadedSubject.OnNext(Unit.Default); } /// @@ -60,6 +75,7 @@ public class PlanetManager : IDisposable, IEnableLogger if (disposing) { _planets.Dispose(); + _planetsReloadedSubject.Dispose(); } } diff --git a/Stars Assistant/ViewModels/BuColViewModel.cs b/Stars Assistant/ViewModels/BuColViewModel.cs index 6fbdf8a..b89e1dc 100644 --- a/Stars Assistant/ViewModels/BuColViewModel.cs +++ b/Stars Assistant/ViewModels/BuColViewModel.cs @@ -15,23 +15,38 @@ public partial class BuColViewModel : ViewModelBase private readonly ReadOnlyObservableCollection _playerPlanetsView; public ReadOnlyObservableCollection PlayerPlanets => _playerPlanetsView; + [Reactive] + private PlayerPlanetViewModel? _selectedPlanet; + + private string? _lastSelectedPlanetName; + public BuColViewModel() { - var PlanetManager = Locator.Current.GetService()!; + var planetManager = Locator.Current.GetService()!; - PlanetManager.PlayerPlanetsSource + planetManager.PlayerPlanetsSource .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _playerPlanetsView) .DisposeMany() .Subscribe(); + this.WhenAnyValue(x => x.SelectedPlanet) + .Subscribe(planet => _lastSelectedPlanetName = planet?.Name); + this.WhenActivated((CompositeDisposable disposables) => { - // /* handle activation */ - // Disposable - // .Create(() => { /* handle deactivation */ }) - // .DisposeWith(disposables); + planetManager.PlanetsReloaded + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(_ => RestoreSelection()) + .DisposeWith(disposables); }); } + private void RestoreSelection() + { + if (_lastSelectedPlanetName != null) + { + SelectedPlanet = PlayerPlanets.FirstOrDefault(p => p.Name == _lastSelectedPlanetName); + } + } } diff --git a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs index 0598f80..3586d1f 100644 --- a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs +++ b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs @@ -67,6 +67,8 @@ public partial class PlayerPlanetViewModel : ViewModelBase _fleetSummaryChanges = fm.FleetSummariesByDestination .Connect() .Watch(Name) + .Replay(1) + .RefCount() .Log(this, "fleetSummaryChange", change => $"{Name}: {change.Reason}: {change.Previous} => {change.Current}") ; @@ -94,8 +96,9 @@ public partial class PlayerPlanetViewModel : ViewModelBase .ToProperty(this, vm => vm.EnRouteGermanium) ; - this.WhenActivated((CompositeDisposable disposables) => + this.WhenActivated((CompositeDisposable disposables) => { + _fleetSummaryChanges.Subscribe().DisposeWith(disposables); disposables.Add(_enRoutePopulationHelper); disposables.Add(_enRouteIroniumHelper); disposables.Add(_enRouteBoraniumHelper); diff --git a/Stars Assistant/Views/BuColView.axaml b/Stars Assistant/Views/BuColView.axaml index 7b54092..da301bf 100644 --- a/Stars Assistant/Views/BuColView.axaml +++ b/Stars Assistant/Views/BuColView.axaml @@ -10,11 +10,12 @@ - + +