diff --git a/Stars Assistant/Model/FleetSummary.cs b/Stars Assistant/Model/FleetSummary.cs new file mode 100644 index 0000000..1db39dd --- /dev/null +++ b/Stars Assistant/Model/FleetSummary.cs @@ -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"; +} diff --git a/Stars Assistant/Services/FleetManager.cs b/Stars Assistant/Services/FleetManager.cs index 946b7dc..ca04f4c 100644 --- a/Stars Assistant/Services/FleetManager.cs +++ b/Stars Assistant/Services/FleetManager.cs @@ -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()!; /// - /// SourceCache for DynamicData views + /// SourceList for fleets read from the game files /// private SourceList _fleets = new(); - public FleetManager() {} + /// + /// Fleet data summarized by destination, will be chaned to _fleets + /// + private SourceCache _fleetSummaries = new(fs => fs.Destination); - public class FleetSummary + /// + /// Public accessor to the continously updated fleet summaries. + /// + public IObservableCache FleetSummaries => _fleetSummaries.AsObservableCache(); + + /// + /// Disposal tracking + /// + 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>? _fleetSummaries; - - private ReadOnlyObservableCollection? 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(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 } } ); + */ } /// @@ -151,4 +157,25 @@ public class FleetManager : IEnableLogger db.SaveChanges(); } + /// + /// Handle disposal of all subscriptions and dependencies. + /// / + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _fleetSummariesSubscription.Dispose(); + _fleetSummaries.Dispose(); + _fleets.Dispose(); + } + } + + /// + /// Boilerplate disposal + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } }