continue fleet summary, factor out helper class.

This commit is contained in:
Torben Nehmer 2024-10-09 20:18:26 +02:00
parent a383f63a61
commit d32ecdcead
No known key found for this signature in database
2 changed files with 65 additions and 24 deletions

View File

@ -0,0 +1,14 @@
using System;
namespace StarsAssistant.Model;
public class FleetSummary
{
public string Destination { get; set; } = string.Empty;
public int TotalIronium { get; set; }
public int TotalBoranium { get; set; }
public int TotalGermanium { get; set; }
public int TotalColonists { get; set; }
public override string ToString() => $"To {Destination}: {TotalIronium}I/{TotalBoranium}B/{TotalGermanium}G/{TotalColonists}C";
}

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
@ -15,40 +16,39 @@ using StarsAssistant.ViewModels;
namespace StarsAssistant.Services;
public class FleetManager : IEnableLogger
public class FleetManager : IEnableLogger, IDisposable
{
protected Services.Game Game = Locator.Current.GetService<Services.Game>()!;
/// <summary>
/// SourceCache for DynamicData views
/// SourceList for fleets read from the game files
/// </summary>
private SourceList<Fleet> _fleets = new();
public FleetManager() {}
/// <summary>
/// Fleet data summarized by destination, will be chaned to _fleets
/// </summary>
private SourceCache<FleetSummary, string> _fleetSummaries = new(fs => fs.Destination);
public class FleetSummary
/// <summary>
/// Public accessor to the continously updated fleet summaries.
/// </summary>
public IObservableCache<FleetSummary, string> FleetSummaries => _fleetSummaries.AsObservableCache();
/// <summary>
/// Disposal tracking
/// </summary>
private IDisposable _fleetSummariesSubscription;
public FleetManager()
{
public string Destination { get; set; } = string.Empty;
public int TotalIronium { get; set; }
public int TotalBoranium { get; set; }
public int TotalGermanium { get; set; }
public int TotalColonists { get; set; }
public override string ToString() => $"To {Destination}: {TotalIronium}I/{TotalBoranium}B/{TotalGermanium}G/{TotalColonists}C";
CreateFleetSummariesLink();
}
private IObservable<IChangeSet<FleetSummary>>? _fleetSummaries;
private ReadOnlyObservableCollection<FleetSummary>? summaries;
private IDisposable? d1;
private IDisposable? d2;
public void test()
[MemberNotNull(nameof(_fleetSummariesSubscription))]
protected void CreateFleetSummariesLink()
{
_fleetSummaries = _fleets.Connect()
_fleetSummariesSubscription = _fleets.Connect()
.Filter(fleet => fleet.TrueDestination != "-- ")
.GroupOn(fleet => fleet.TrueDestination)
.Log(this, $"{DateTime.Now.ToLongTimeString()} fleetWatcher", grp => $"{grp.TotalChanges} detected")
@ -60,15 +60,20 @@ public class FleetManager : IEnableLogger
TotalGermanium = group.List.Items.Sum(f => f.Germanium),
TotalColonists = group.List.Items.Sum(f => f.Colonists)
})
.AddKey(fs => fs.Destination)
.Log(this, "FleetManager _fleetSummaries update", changes =>
$"{changes.Adds} adds, {changes.Updates} updates, {changes.Removes} removes"
)
.PopulateInto(_fleetSummaries)
;
/*
// Demo only
var sourceCache = new SourceCache<FleetSummary, string>(fs => fs.Destination);
var tmp = _fleetSummaries
var tmp = fleetSummaries
.AddKey(fs => fs.Destination)
.PopulateInto(sourceCache);
_fleetSummaries
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out summaries)
@ -93,6 +98,7 @@ public class FleetManager : IEnableLogger
}
}
);
*/
}
/// <summary>
@ -151,4 +157,25 @@ public class FleetManager : IEnableLogger
db.SaveChanges();
}
/// <summary>
/// Handle disposal of all subscriptions and dependencies.
/// </summary>/
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_fleetSummariesSubscription.Dispose();
_fleetSummaries.Dispose();
_fleets.Dispose();
}
}
/// <summary>
/// Boilerplate disposal
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}