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()!; /// /// SourceCache for DynamicData views /// private SourceCache _planets = new(p => p.Name); /// /// Observable changeset showing all player planets converted to view models. /// public IObservable> PlayerPlanetsSource => _planets .Connect() .Filter(planet => planet.OwnerId == Game.Player.Name) .Transform(planet => new PlayerPlanetViewModel(planet)); public PlanetManager() {} /// /// Load the planet records from the database and push them into our source cache. /// public void InitFromDatabase() { using var db = Locator.Current.GetService()!; var allPlanets = db.Planet.ToList(); var cacheKeys = _planets.Keys.ToList(); var planetNames = from p in allPlanets select p.Name; var planetsToDelete = cacheKeys.Except(planetNames); _planets.Edit(innerCache => { foreach (var name in planetsToDelete) innerCache.RemoveKey(name); innerCache.AddOrUpdate(allPlanets); } ); } }