Skip to content

Commit

Permalink
Track download progress
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 5, 2024
1 parent 6aa11be commit 957039f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/NexusMods.App.UI/NexusMods.App.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<ProjectReference Include="..\Abstractions\NexusMods.Abstractions.Telemetry\NexusMods.Abstractions.Telemetry.csproj" />
<ProjectReference Include="..\Extensions\NexusMods.Extensions.DynamicData\NexusMods.Extensions.DynamicData.csproj" />
<ProjectReference Include="..\Networking\NexusMods.Networking.Downloaders\NexusMods.Networking.Downloaders.csproj" />
<ProjectReference Include="..\Networking\NexusMods.Networking.NexusWebApi\NexusMods.Networking.NexusWebApi.csproj" />
<ProjectReference Include="..\NexusMods.App.BuildInfo\NexusMods.App.BuildInfo.csproj" />
<ProjectReference Include="..\NexusMods.Icons\NexusMods.Icons.csproj" />
<ProjectReference Include="..\NexusMods.Media\NexusMods.Media.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reactive.Linq;
using NexusMods.Abstractions.Jobs;
using NexusMods.Abstractions.NexusModsLibrary;
using NexusMods.App.UI.Controls;
Expand All @@ -19,7 +20,21 @@ public NexusModsFileMetadataLibraryItemModel(NexusModsFileMetadata.ReadOnly file
FormattedSize = ItemSize.ToFormattedProperty();
DownloadItemCommand = ILibraryItemWithDownloadAction.CreateCommand(this);

// ReSharper disable once NotDisposedResource
var modelActivationDisposable = this.WhenActivated(static (self, disposables) =>
{
self.DownloadJobObservable.SelectMany(job => job.ObservableStatus).CombineLatest(self.IsInLibraryObservable)
.OnUI()
.SubscribeWithErrorLogging(tuple =>
{
var (status, inLibrary) = tuple;
self.DownloadState.Value = inLibrary ? JobStatus.Completed : status;
self.DownloadButtonText.Value = ILibraryItemWithDownloadAction.GetButtonText(status: self.DownloadState.Value);
}).AddTo(disposables);
});

_modelDisposable = Disposable.Combine(
modelActivationDisposable,
Name,
Version,
ItemSize,
Expand All @@ -30,6 +45,9 @@ public NexusModsFileMetadataLibraryItemModel(NexusModsFileMetadata.ReadOnly file
);
}

public required IObservable<bool> IsInLibraryObservable { get; init; }
public required IObservable<IJob> DownloadJobObservable { get; init; }

public BindableReactiveProperty<string> Name { get; } = new(value: "-");
public BindableReactiveProperty<string> Version { get; } = new(value: "-");

Expand Down
28 changes: 27 additions & 1 deletion src/NexusMods.App.UI/Pages/NexusModsDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Diagnostics;
using System.Reactive.Linq;
using DynamicData;
using DynamicData.Aggregation;
using DynamicData.Kernel;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using NexusMods.Abstractions.Jobs;
using NexusMods.Abstractions.Library.Models;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.MnemonicDB.Attributes.Extensions;
using NexusMods.Abstractions.NexusModsLibrary;
Expand All @@ -14,17 +17,20 @@
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.DatomIterators;
using NexusMods.MnemonicDB.Abstractions.Query;
using NexusMods.Networking.NexusWebApi;

namespace NexusMods.App.UI.Pages;

[UsedImplicitly]
public class NexusModsDataProvider : ILibraryDataProvider, ILoadoutDataProvider
{
private readonly IConnection _connection;
private readonly IJobMonitor _jobMonitor;

public NexusModsDataProvider(IServiceProvider serviceProvider)
{
_connection = serviceProvider.GetRequiredService<IConnection>();
_jobMonitor = serviceProvider.GetRequiredService<IJobMonitor>();
}

public IObservable<IChangeSet<ILibraryItemModel, EntityId>> ObserveCollectionItems(
Expand All @@ -36,7 +42,27 @@ public IObservable<IChangeSet<ILibraryItemModel, EntityId>> ObserveCollectionIte
.Transform(datom => CollectionRevisionModFile.Load(_connection.Db, datom.E).NexusModFile)
.Transform(ILibraryItemModel (nexusModsFileMetadata) =>
{
var model = new NexusModsFileMetadataLibraryItemModel(nexusModsFileMetadata);
var isInLibraryObservable = _connection
.ObserveDatoms(NexusModsLibraryItem.FileMetadata, nexusModsFileMetadata)
.Transform(datom => LibraryFile.Hash.IsIn(_connection.Db, datom.E))
.IsNotEmpty();
var downloadJobObservable = _jobMonitor .GetObservableChangeSet<NexusModsDownloadJob>()
.Filter(job =>
{
var definition = job.Definition as NexusModsDownloadJob;
Debug.Assert(definition is not null);
return definition.FileMetadata.Id == nexusModsFileMetadata;
})
.QueryWhenChanged(static query => query.Items.MaxBy(job => job.Status))
.Where(static x => x is not null)
.Select(static x => x!);
var model = new NexusModsFileMetadataLibraryItemModel(nexusModsFileMetadata)
{
IsInLibraryObservable = isInLibraryObservable,
DownloadJobObservable = downloadJobObservable,
};
model.Name.Value = nexusModsFileMetadata.Name;
model.Version.Value = nexusModsFileMetadata.Version;
Expand Down

0 comments on commit 957039f

Please sign in to comment.