diff --git a/Stars Assistant/Services/PlanetManager.cs b/Stars Assistant/Services/PlanetManager.cs index 72cd2be..72ac62b 100644 --- a/Stars Assistant/Services/PlanetManager.cs +++ b/Stars Assistant/Services/PlanetManager.cs @@ -30,6 +30,11 @@ public class PlanetManager : IDisposable, IEnableLogger .Filter(planet => planet.OwnerId == Game.Player.Name) .Transform(planet => new PlayerPlanetViewModel(planet)); + /// + /// Cache with all player planets, indexed by planet name. + /// + private SourceCache _playerPlanets = new(p => p.Planet.Name); + /// /// Observable subject to trigger when we update the planet cache, allows the /// UI to react to it. @@ -64,6 +69,27 @@ public class PlanetManager : IDisposable, IEnableLogger innerCache.AddOrUpdate(allPlanets); } ); + + var allPlayerPlanets = allPlanets.Where(planet => planet.OwnerId == Game.Player.Name); + var allPlayerCacheKeys = _playerPlanets.Keys.ToList(); + var playerPlanetNames = from p in allPlayerPlanets select p.Name; + var playerPlanetsToDelete = allPlayerCacheKeys.Except(playerPlanetNames); + + _playerPlanets.Edit(innerCache => + { + foreach (var name in playerPlanetsToDelete) + innerCache.RemoveKey(name); + foreach (var planet in allPlayerPlanets) + { + var playerPlanet = innerCache.Lookup(planet.Name); + if (playerPlanet.HasValue) + playerPlanet.Value.Planet = planet; + else + innerCache.Add(new PlayerPlanetViewModel(planet)); + } + } + ); + _planetsReloadedSubject.OnNext(Unit.Default); } diff --git a/Stars Assistant/ViewModels/BuColViewModel.cs b/Stars Assistant/ViewModels/BuColViewModel.cs index b89e1dc..38e6115 100644 --- a/Stars Assistant/ViewModels/BuColViewModel.cs +++ b/Stars Assistant/ViewModels/BuColViewModel.cs @@ -18,8 +18,6 @@ public partial class BuColViewModel : ViewModelBase [Reactive] private PlayerPlanetViewModel? _selectedPlanet; - private string? _lastSelectedPlanetName; - public BuColViewModel() { var planetManager = Locator.Current.GetService()!; @@ -28,25 +26,7 @@ public partial class BuColViewModel : ViewModelBase .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _playerPlanetsView) .DisposeMany() - .Subscribe(); - - this.WhenAnyValue(x => x.SelectedPlanet) - .Subscribe(planet => _lastSelectedPlanetName = planet?.Name); + .Subscribe(); - this.WhenActivated((CompositeDisposable 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 3586d1f..08ffd6d 100644 --- a/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs +++ b/Stars Assistant/ViewModels/PlayerPlanetViewModel.cs @@ -13,7 +13,20 @@ namespace StarsAssistant.ViewModels; public partial class PlayerPlanetViewModel : ViewModelBase { - private readonly Model.Planet Planet; + [Reactive] + private Model.Planet _planet; + + /* TODO + public Model.Planet Planet + { + get => _planet; + set + { + this.RaiseAndSetIfChanged(ref _planet, value); + RaisePropertyChangedForAll(); + } + } + */ private readonly Services.Game Game; @@ -269,6 +282,57 @@ public partial class PlayerPlanetViewModel : ViewModelBase , 0); } + private void RaisePropertyChangedForAll() +{ + OnPropertyChanged(nameof(Name)); + OnPropertyChanged(nameof(Owner)); + OnPropertyChanged(nameof(StarbaseType)); + OnPropertyChanged(nameof(Population)); + OnPropertyChanged(nameof(Value)); + OnPropertyChanged(nameof(Mines)); + OnPropertyChanged(nameof(Factories)); + OnPropertyChanged(nameof(DefPercent)); + OnPropertyChanged(nameof(SurfaceIronium)); + OnPropertyChanged(nameof(SurfaceBoranium)); + OnPropertyChanged(nameof(SurfaceGermanium)); + OnPropertyChanged(nameof(MRIronium)); + OnPropertyChanged(nameof(MRBoranium)); + OnPropertyChanged(nameof(MRGermanium)); + OnPropertyChanged(nameof(MCIronium)); + OnPropertyChanged(nameof(MCBoranium)); + OnPropertyChanged(nameof(MCGermanium)); + OnPropertyChanged(nameof(Resources)); + OnPropertyChanged(nameof(Gravity)); + OnPropertyChanged(nameof(Temperature)); + OnPropertyChanged(nameof(Radiation)); + OnPropertyChanged(nameof(GravityOrig)); + OnPropertyChanged(nameof(TemperatureOrig)); + OnPropertyChanged(nameof(RadiationOrig)); + OnPropertyChanged(nameof(MaxTerraforming)); + OnPropertyChanged(nameof(CapacityPercent)); + OnPropertyChanged(nameof(ScanRange)); + OnPropertyChanged(nameof(PenScanRange)); + OnPropertyChanged(nameof(Driver)); + OnPropertyChanged(nameof(DriverWarp)); + OnPropertyChanged(nameof(RouteTarget)); + OnPropertyChanged(nameof(GateRange)); + OnPropertyChanged(nameof(GateMass)); + OnPropertyChanged(nameof(PctDamage)); + OnPropertyChanged(nameof(EffectiveValue)); + OnPropertyChanged(nameof(MaxPopulation)); + OnPropertyChanged(nameof(PopulationGrowth)); + OnPropertyChanged(nameof(PopulationT1)); + OnPropertyChanged(nameof(BuildableFactories)); + OnPropertyChanged(nameof(BuildableMines)); + OnPropertyChanged(nameof(OperableFactories)); + OnPropertyChanged(nameof(OperableMines)); + OnPropertyChanged(nameof(ResourcesFromPopulation)); + OnPropertyChanged(nameof(ResourcesFromFactories)); + OnPropertyChanged(nameof(AvailableIronium)); + OnPropertyChanged(nameof(AvailableBoranium)); + OnPropertyChanged(nameof(AvailableGermanium)); +} + #endregion } \ No newline at end of file