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;
using System.ComponentModel; using System.ComponentModel;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Subjects;
using DynamicData; using DynamicData;
using DynamicData.Binding; using DynamicData.Binding;
using Splat; using Splat;
@ -27,10 +30,21 @@ public class PlanetManager : IDisposable, IEnableLogger
.Filter(planet => planet.OwnerId == Game.Player.Name) .Filter(planet => planet.OwnerId == Game.Player.Name)
.Transform(planet => new PlayerPlanetViewModel(planet)); .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() public PlanetManager()
{ {
FleetManager fleetManager = Locator.Current.GetService<FleetManager>()!; FleetManager fleetManager = Locator.Current.GetService<FleetManager>()!;
} }
/// <summary> /// <summary>
@ -50,6 +64,7 @@ public class PlanetManager : IDisposable, IEnableLogger
innerCache.AddOrUpdate(allPlanets); innerCache.AddOrUpdate(allPlanets);
} }
); );
_planetsReloadedSubject.OnNext(Unit.Default);
} }
/// <summary> /// <summary>
@ -60,6 +75,7 @@ public class PlanetManager : IDisposable, IEnableLogger
if (disposing) if (disposing)
{ {
_planets.Dispose(); _planets.Dispose();
_planetsReloadedSubject.Dispose();
} }
} }

View File

@ -15,23 +15,38 @@ public partial class BuColViewModel : ViewModelBase
private readonly ReadOnlyObservableCollection<PlayerPlanetViewModel> _playerPlanetsView; private readonly ReadOnlyObservableCollection<PlayerPlanetViewModel> _playerPlanetsView;
public ReadOnlyObservableCollection<PlayerPlanetViewModel> PlayerPlanets => _playerPlanetsView; public ReadOnlyObservableCollection<PlayerPlanetViewModel> PlayerPlanets => _playerPlanetsView;
[Reactive]
private PlayerPlanetViewModel? _selectedPlanet;
private string? _lastSelectedPlanetName;
public BuColViewModel() public BuColViewModel()
{ {
var PlanetManager = Locator.Current.GetService<PlanetManager>()!; var planetManager = Locator.Current.GetService<PlanetManager>()!;
PlanetManager.PlayerPlanetsSource planetManager.PlayerPlanetsSource
.ObserveOn(RxApp.MainThreadScheduler) .ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _playerPlanetsView) .Bind(out _playerPlanetsView)
.DisposeMany() .DisposeMany()
.Subscribe(); .Subscribe();
this.WhenAnyValue(x => x.SelectedPlanet)
.Subscribe(planet => _lastSelectedPlanetName = planet?.Name);
this.WhenActivated((CompositeDisposable disposables) => this.WhenActivated((CompositeDisposable disposables) =>
{ {
// /* handle activation */ planetManager.PlanetsReloaded
// Disposable .ObserveOn(RxApp.MainThreadScheduler)
// .Create(() => { /* handle deactivation */ }) .Subscribe(_ => RestoreSelection())
// .DisposeWith(disposables); .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 _fleetSummaryChanges = fm.FleetSummariesByDestination
.Connect() .Connect()
.Watch(Name) .Watch(Name)
.Replay(1)
.RefCount()
.Log(this, "fleetSummaryChange", change => $"{Name}: {change.Reason}: {change.Previous} => {change.Current}") .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) .ToProperty(this, vm => vm.EnRouteGermanium)
; ;
this.WhenActivated((CompositeDisposable disposables) => this.WhenActivated((CompositeDisposable disposables) =>
{ {
_fleetSummaryChanges.Subscribe().DisposeWith(disposables);
disposables.Add(_enRoutePopulationHelper); disposables.Add(_enRoutePopulationHelper);
disposables.Add(_enRouteIroniumHelper); disposables.Add(_enRouteIroniumHelper);
disposables.Add(_enRouteBoraniumHelper); disposables.Add(_enRouteBoraniumHelper);

View File

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