fixup fleet summaries
This commit is contained in:
		@@ -28,79 +28,93 @@ public class FleetManager : IEnableLogger, IDisposable
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Fleet data summarized by destination, will be chaned to _fleets
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    private SourceCache<FleetSummary, string> _fleetSummaries = new(fs => fs.Destination);
 | 
			
		||||
    private SourceCache<FleetSummaryByDestination, string> _fleetSummariesByDestination = new(fs => fs.Destination);
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Extract a readonly Cache for all fleet summaries tracked by the
 | 
			
		||||
    /// fleet manager.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public IObservableCache<FleetSummaryByDestination, string> FleetSummariesByDestination
 | 
			
		||||
        => _fleetSummariesByDestination.AsObservableCache();
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Public accessor to the continously updated fleet summaries.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public IObservableCache<FleetSummary, string> FleetSummaries => _fleetSummaries.AsObservableCache();
 | 
			
		||||
    // public IObservableCache<FleetSummaryByDestination, string> FleetSummariesByDestination => _fleetSummaries.AsObservableCache();
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Disposal tracking
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    private IDisposable _fleetSummariesSubscription;
 | 
			
		||||
    // private IDisposable _fleetSummariesSubscription;
 | 
			
		||||
 | 
			
		||||
    public FleetManager() 
 | 
			
		||||
    {
 | 
			
		||||
        CreateFleetSummariesLink();
 | 
			
		||||
        // CreateFleetSummariesLink();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [MemberNotNull(nameof(_fleetSummariesSubscription))]
 | 
			
		||||
    protected void CreateFleetSummariesLink()
 | 
			
		||||
    {
 | 
			
		||||
        _fleetSummariesSubscription = _fleets.Connect()
 | 
			
		||||
            .Filter(fleet => fleet.TrueDestination != "-- ")
 | 
			
		||||
            .GroupOn(fleet => fleet.TrueDestination)
 | 
			
		||||
            .Log(this, $"{DateTime.Now.ToLongTimeString()} fleetWatcher", grp => $"{grp.TotalChanges} detected")
 | 
			
		||||
            .Transform(group => new FleetSummary
 | 
			
		||||
                {   
 | 
			
		||||
                    Destination = group.GroupKey,
 | 
			
		||||
                    TotalIronium = group.List.Items.Sum(f => f.Ironium),
 | 
			
		||||
                    TotalBoranium = group.List.Items.Sum(f => f.Boranium),
 | 
			
		||||
                    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)
 | 
			
		||||
        ;
 | 
			
		||||
    // [MemberNotNull(nameof(_fleetSummariesSubscription))]
 | 
			
		||||
    // protected void CreateFleetSummariesLink()
 | 
			
		||||
    // {
 | 
			
		||||
    //     /*
 | 
			
		||||
    //     _fleetSummariesSubscription = _fleets.Connect()
 | 
			
		||||
    //         .Filter(fleet => fleet.TrueDestination != "-- ")
 | 
			
		||||
    //         .Log(this, $"{DateTime.Now.ToLongTimeString()} fleetWatcher filter", fleet => $"{fleet}")            
 | 
			
		||||
    //         // .AutoRefreshOnObservable(fleet => fleet.WhenAnyValue(
 | 
			
		||||
    //         //     f => f.Ironium, f => f.Boranium, f => f.Germanium, f => f.Colonists))
 | 
			
		||||
    //         .Log(this, $"{DateTime.Now.ToLongTimeString()} fleetWatcher refresh", fleet => $"{fleet}")
 | 
			
		||||
    //         .GroupOn(fleet => fleet.TrueDestination)
 | 
			
		||||
    //         .Log(this, $"{DateTime.Now.ToLongTimeString()} fleetWatcher group", grp => $"{grp.TotalChanges} detected")
 | 
			
		||||
    //         .Transform(group => new FleetSummaryByDestination
 | 
			
		||||
    //             {   
 | 
			
		||||
    //                 Destination = group.GroupKey,
 | 
			
		||||
    //                 TotalIronium = group.List.Items.Sum(f => f.Ironium),
 | 
			
		||||
    //                 TotalBoranium = group.List.Items.Sum(f => f.Boranium),
 | 
			
		||||
    //                 TotalGermanium = group.List.Items.Sum(f => f.Germanium),
 | 
			
		||||
    //                 TotalColonists = group.List.Items.Sum(f => f.Colonists)
 | 
			
		||||
    //             })
 | 
			
		||||
 | 
			
		||||
        /*
 | 
			
		||||
        // Demo only
 | 
			
		||||
        var sourceCache = new SourceCache<FleetSummary, string>(fs => fs.Destination);
 | 
			
		||||
        var tmp = fleetSummaries
 | 
			
		||||
            .AddKey(fs => fs.Destination)
 | 
			
		||||
            .PopulateInto(sourceCache);
 | 
			
		||||
    //         .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<FleetSummaryByDestination, string>(fs => fs.Destination);
 | 
			
		||||
    //     var tmp = fleetSummaries
 | 
			
		||||
    //         .AddKey(fs => fs.Destination)
 | 
			
		||||
    //         .PopulateInto(sourceCache);
 | 
			
		||||
        
 | 
			
		||||
        _fleetSummaries
 | 
			
		||||
            .ObserveOn(RxApp.MainThreadScheduler)
 | 
			
		||||
            .Bind(out summaries)
 | 
			
		||||
            .DisposeMany()
 | 
			
		||||
            .Subscribe();
 | 
			
		||||
    //     _fleetSummaries
 | 
			
		||||
    //         .ObserveOn(RxApp.MainThreadScheduler)
 | 
			
		||||
    //         .Bind(out summaries)
 | 
			
		||||
    //         .DisposeMany()
 | 
			
		||||
    //         .Subscribe();
 | 
			
		||||
 | 
			
		||||
        d1 = summaries
 | 
			
		||||
            .Subscribe(Observer.Create<FleetSummary>(f => 
 | 
			
		||||
                {
 | 
			
		||||
                    this.Log().Debug($"FleetSummary observed: {f}");
 | 
			
		||||
                }
 | 
			
		||||
            ));
 | 
			
		||||
 | 
			
		||||
        d2 = _fleetSummaries
 | 
			
		||||
            .ObserveOn(RxApp.MainThreadScheduler)
 | 
			
		||||
            .Subscribe(x => 
 | 
			
		||||
            {
 | 
			
		||||
                var lst = x.ToList();
 | 
			
		||||
                foreach (var f in lst)
 | 
			
		||||
                {
 | 
			
		||||
                    this.Log().Debug($"Reason {f.Reason}, Type {f.Type}: {f.Item.Current}");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            );
 | 
			
		||||
        */
 | 
			
		||||
    }
 | 
			
		||||
    //     d1 = summaries
 | 
			
		||||
    //         .Subscribe(Observer.Create<FleetSummaryByDestination>(f => 
 | 
			
		||||
    //             {
 | 
			
		||||
    //                 this.Log().Debug($"FleetSummaryByDestination observed: {f}");
 | 
			
		||||
    //             }
 | 
			
		||||
    //         ));
 | 
			
		||||
 | 
			
		||||
    //     d2 = _fleetSummaries
 | 
			
		||||
    //         .ObserveOn(RxApp.MainThreadScheduler)
 | 
			
		||||
    //         .Subscribe(x => 
 | 
			
		||||
    //         {
 | 
			
		||||
    //             var lst = x.ToList();
 | 
			
		||||
    //             foreach (var f in lst)
 | 
			
		||||
    //             {
 | 
			
		||||
    //                 this.Log().Debug($"Reason {f.Reason}, Type {f.Type}: {f.Item.Current}");
 | 
			
		||||
    //             }
 | 
			
		||||
    //         }
 | 
			
		||||
    //         );
 | 
			
		||||
    //     */
 | 
			
		||||
    // }
 | 
			
		||||
    
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Load the fleet records from the database and push them into our source cache.
 | 
			
		||||
    /// If the data has been freshly imported, call PostProcessImportedData first.
 | 
			
		||||
@@ -108,6 +122,8 @@ public class FleetManager : IEnableLogger, IDisposable
 | 
			
		||||
    public void InitFromDatabase()
 | 
			
		||||
    {
 | 
			
		||||
        using var db = Locator.Current.GetService<StarsDatabase>()!;
 | 
			
		||||
 | 
			
		||||
        // Load the full list
 | 
			
		||||
        var allFleets = db.Fleet.ToList();
 | 
			
		||||
        _fleets.Edit(innerCache =>
 | 
			
		||||
            {
 | 
			
		||||
@@ -115,6 +131,25 @@ public class FleetManager : IEnableLogger, IDisposable
 | 
			
		||||
                innerCache.Add(allFleets);
 | 
			
		||||
            }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        var summaries = from f in allFleets
 | 
			
		||||
            group f by f.TrueDestination into grp
 | 
			
		||||
            select new FleetSummaryByDestination 
 | 
			
		||||
            {
 | 
			
		||||
                Destination = grp.Key,
 | 
			
		||||
                TotalIronium = grp.Sum(f => f.Ironium),
 | 
			
		||||
                TotalBoranium = grp.Sum(f => f.Boranium),
 | 
			
		||||
                TotalGermanium = grp.Sum(f => f.Germanium),
 | 
			
		||||
                TotalColonists = grp.Sum(f => f.Colonists)
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
        var cacheKeys = _fleetSummariesByDestination.Keys.ToList();
 | 
			
		||||
        var summariesToDelete = cacheKeys.Except(summaries.Select(sum => sum.Destination));
 | 
			
		||||
        _fleetSummariesByDestination.Edit(innerCache => {
 | 
			
		||||
                innerCache.RemoveKeys(summariesToDelete);
 | 
			
		||||
                innerCache.AddOrUpdate(summaries);
 | 
			
		||||
            }
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
@@ -166,8 +201,8 @@ public class FleetManager : IEnableLogger, IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        if (disposing)
 | 
			
		||||
        {
 | 
			
		||||
            _fleetSummariesSubscription.Dispose();
 | 
			
		||||
            _fleetSummaries.Dispose();
 | 
			
		||||
            // _fleetSummariesSubscription.Dispose();
 | 
			
		||||
            _fleetSummariesByDestination.Dispose();
 | 
			
		||||
            _fleets.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user