2024-09-22 20:12:46 +00:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Reactive.Disposables;
|
|
|
|
using System.Reactive.Linq;
|
2024-09-25 20:27:35 +00:00
|
|
|
using DynamicData;
|
2024-09-22 20:12:46 +00:00
|
|
|
using ReactiveUI;
|
|
|
|
using ReactiveUI.SourceGenerators;
|
|
|
|
using Splat;
|
2024-09-25 20:27:35 +00:00
|
|
|
using StarsAssistant.Model;
|
|
|
|
using StarsAssistant.Services;
|
2024-09-22 20:12:46 +00:00
|
|
|
|
|
|
|
namespace StarsAssistant.ViewModels;
|
|
|
|
|
2024-09-23 20:40:18 +00:00
|
|
|
public partial class BuColViewModel : ViewModelBase
|
2024-09-22 20:12:46 +00:00
|
|
|
{
|
2024-09-25 20:27:35 +00:00
|
|
|
private readonly ReadOnlyObservableCollection<PlayerPlanetViewModel> _playerPlanetsView;
|
|
|
|
public ReadOnlyObservableCollection<PlayerPlanetViewModel> PlayerPlanets => _playerPlanetsView;
|
|
|
|
|
2024-11-27 17:40:14 +00:00
|
|
|
[Reactive]
|
|
|
|
private PlayerPlanetViewModel? _selectedPlanet;
|
|
|
|
|
|
|
|
private string? _lastSelectedPlanetName;
|
|
|
|
|
2024-09-22 20:12:46 +00:00
|
|
|
public BuColViewModel()
|
|
|
|
{
|
2024-11-27 17:40:14 +00:00
|
|
|
var planetManager = Locator.Current.GetService<PlanetManager>()!;
|
2024-09-25 20:27:35 +00:00
|
|
|
|
2024-11-27 17:40:14 +00:00
|
|
|
planetManager.PlayerPlanetsSource
|
2024-09-25 20:27:35 +00:00
|
|
|
.ObserveOn(RxApp.MainThreadScheduler)
|
|
|
|
.Bind(out _playerPlanetsView)
|
|
|
|
.DisposeMany()
|
|
|
|
.Subscribe();
|
|
|
|
|
2024-11-27 17:40:14 +00:00
|
|
|
this.WhenAnyValue(x => x.SelectedPlanet)
|
|
|
|
.Subscribe(planet => _lastSelectedPlanetName = planet?.Name);
|
|
|
|
|
2024-09-22 20:12:46 +00:00
|
|
|
this.WhenActivated((CompositeDisposable disposables) =>
|
|
|
|
{
|
2024-11-27 17:40:14 +00:00
|
|
|
planetManager.PlanetsReloaded
|
|
|
|
.ObserveOn(RxApp.MainThreadScheduler)
|
|
|
|
.Subscribe(_ => RestoreSelection())
|
|
|
|
.DisposeWith(disposables);
|
2024-09-22 20:12:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-27 17:40:14 +00:00
|
|
|
private void RestoreSelection()
|
|
|
|
{
|
|
|
|
if (_lastSelectedPlanetName != null)
|
|
|
|
{
|
|
|
|
SelectedPlanet = PlayerPlanets.FirstOrDefault(p => p.Name == _lastSelectedPlanetName);
|
|
|
|
}
|
|
|
|
}
|
2024-09-22 20:12:46 +00:00
|
|
|
}
|