Intermediate, fixes a lot of update problems, keeping selection in place when updating planets still broken.

This commit is contained in:
Torben Nehmer 2024-11-27 18:40:14 +01:00
parent e79a308243
commit 4789904284
No known key found for this signature in database
4 changed files with 45 additions and 10 deletions

View File

@ -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));
/// <summary>
/// Observable subject to trigger when we update the planet cache, allows the
/// UI to react to it.
/// </summary>
private readonly Subject<Unit> _planetsReloadedSubject = new();
/// <summary>
/// Exposes _planetReloadedSubject as IObservable.
/// </summary>
public IObservable<Unit> PlanetsReloaded => _planetsReloadedSubject.AsObservable();
public PlanetManager()
{
FleetManager fleetManager = Locator.Current.GetService<FleetManager>()!;
}
/// <summary>
@ -50,6 +64,7 @@ public class PlanetManager : IDisposable, IEnableLogger
innerCache.AddOrUpdate(allPlanets);
}
);
_planetsReloadedSubject.OnNext(Unit.Default);
}
/// <summary>
@ -60,6 +75,7 @@ public class PlanetManager : IDisposable, IEnableLogger
if (disposing)
{
_planets.Dispose();
_planetsReloadedSubject.Dispose();
}
}

View File

@ -15,23 +15,38 @@ public partial class BuColViewModel : ViewModelBase
private readonly ReadOnlyObservableCollection<PlayerPlanetViewModel> _playerPlanetsView;
public ReadOnlyObservableCollection<PlayerPlanetViewModel> PlayerPlanets => _playerPlanetsView;
[Reactive]
private PlayerPlanetViewModel? _selectedPlanet;
private string? _lastSelectedPlanetName;
public BuColViewModel()
{
var PlanetManager = Locator.Current.GetService<PlanetManager>()!;
var planetManager = Locator.Current.GetService<PlanetManager>()!;
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);
}
}
}

View File

@ -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);

View File

@ -10,11 +10,12 @@
<DataGrid x:Name="PlanetsGrid"
ItemsSource="{Binding PlayerPlanets}"
SelectedItem="{Binding SelectedPlanet}"
GridLinesVisibility="All"
FrozenColumnCount="1"
BorderThickness="1" BorderBrush="Gray">
<DataGrid.Columns>
<DataGrid.Columns>
<DataGridTextColumn Header="Planet" Binding="{Binding Name}" />
<!-- Class -->
<!-- Info -->