Intermediate, add dynamic data, not yet w/o errors/warnings

This commit is contained in:
Torben Nehmer 2024-09-25 22:27:35 +02:00
parent cafa45bed3
commit a394918064
4 changed files with 56 additions and 5 deletions

View File

@ -123,6 +123,7 @@ public class Game
GameEngine.Game = this; GameEngine.Game = this;
Locator.CurrentMutable.RegisterConstant(new CSVDataLoader(), typeof(CSVDataLoader)); Locator.CurrentMutable.RegisterConstant(new CSVDataLoader(), typeof(CSVDataLoader));
Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase)); Locator.CurrentMutable.Register(() => new StarsDatabase(DatabaseFileName), typeof(StarsDatabase));
Locator.CurrentMutable.RegisterConstant(new PlanetManager(), typeof(Services.PlanetManager));
// TESTING HELPER // TESTING HELPER
if (__doCreateTestData) if (__doCreateTestData)

View File

@ -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<Services.Game>()!;
private SourceCache<Planet, string> _planets = new(p => p.Name);
public IObservable<IChangeSet<PlayerPlanetViewModel, string>> 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<StarsDatabase>()!;
{
var playerPlanets = db.Planet.ToList();
_planets.AddOrUpdate(playerPlanets);
}
}
}

View File

@ -1,17 +1,31 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using DynamicData;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.SourceGenerators; using ReactiveUI.SourceGenerators;
using Splat; using Splat;
using StarsAssistant.Model;
using StarsAssistant.Services;
namespace StarsAssistant.ViewModels; namespace StarsAssistant.ViewModels;
public partial class BuColViewModel : ViewModelBase public partial class BuColViewModel : ViewModelBase
{ {
private readonly ReadOnlyObservableCollection<PlayerPlanetViewModel> _playerPlanetsView;
public ReadOnlyObservableCollection<PlayerPlanetViewModel> PlayerPlanets => _playerPlanetsView;
public BuColViewModel() public BuColViewModel()
{ {
var PlanetManager = Locator.Current.GetService<PlanetManager>()!;
PlanetManager.__test__Init();
PlanetManager.PlayerPlanetsSource
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _playerPlanetsView)
.DisposeMany()
.Subscribe();
this.WhenActivated((CompositeDisposable disposables) => this.WhenActivated((CompositeDisposable disposables) =>
{ {
// /* handle activation */ // /* handle activation */
@ -21,5 +35,4 @@ public partial class BuColViewModel : ViewModelBase
}); });
} }
public ObservableCollection<PlayerPlanetViewModel> Planets { get; } = PlanetViewModel.LoadAll();
} }

View File

@ -9,9 +9,10 @@
<DataGrid x:Name="PlanetsGrid" <DataGrid x:Name="PlanetsGrid"
ItemsSource="{Binding Planets}" ItemsSource="{Binding PlayerPlanets}"
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}" />
@ -21,7 +22,7 @@
<!-- Pop To Ship --> <!-- Pop To Ship -->
<DataGridTextColumn Header="Growth" Binding="{Binding PopulationGrowth}" /> <DataGridTextColumn Header="Growth" Binding="{Binding PopulationGrowth}" />
<!-- Pop en route --> <!-- Pop en route -->
<DataGridTextColumn Header="Pop Tgt %" Binding="{Binding PopulationTargetPercent}" /> <DataGridTextColumn Header="Pop T%" Binding="{Binding PopulationTargetPercent}" IsReadOnly="false"/>
<DataGridTextColumn Header="Pop %" Binding="{Binding CapacityPercent}" /> <DataGridTextColumn Header="Pop %" Binding="{Binding CapacityPercent}" />
<!-- Terra Delta assumed --> <!-- Terra Delta assumed -->
<DataGridTextColumn Header="Value" Binding="{Binding Value}" /> <DataGridTextColumn Header="Value" Binding="{Binding Value}" />