Skip to content

Commit

Permalink
Show external files
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 14, 2024
1 parent ec18f4d commit 71099b4
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public partial class NexusModsLibrary
);

var collectionRevisionInfo = apiResult.Data?.CollectionRevision;
if (collectionRevisionInfo is null) throw new NotSupportedException($"API call returned no data for `{slug}` `{revisionNumber}`");
if (collectionRevisionInfo is null) throw new NotSupportedException($"API call returned no data for collection slug `{slug}` revision `{revisionNumber}`");

using var tx = _connection.BeginTransaction();
var db = _connection.Db;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using NexusMods.Abstractions.NexusModsLibrary.Models;
using NexusMods.Abstractions.NexusWebApi;
using NexusMods.Abstractions.NexusWebApi.Types;
using NexusMods.Abstractions.Resources;
using NexusMods.Abstractions.Telemetry;
using NexusMods.App.UI.Controls;
using NexusMods.App.UI.Extensions;
using NexusMods.App.UI.Pages.LibraryPage;
Expand Down Expand Up @@ -96,13 +94,25 @@ public CollectionDownloadViewModel(
.AddTo(disposables);

TreeDataGridAdapter.MessageSubject.SubscribeAwait(
onNextAsync: (message, cancellationToken) => DownloadOrOpenPage(message.Item.AsT0, cancellationToken),
onNextAsync: (message, cancellationToken) =>
{
return message.Item.Match(
f0: x => DownloadOrOpenPage(x, cancellationToken),
f1: x => DownloadExternalItem(x, cancellationToken)
);
},
awaitOperation: AwaitOperation.Parallel,
configureAwait: false
).AddTo(disposables);
});
}

private async ValueTask DownloadExternalItem(ExternalItem externalItem, CancellationToken cancellationToken)
{
// TODO:
await Task.Yield();
}

private async ValueTask DownloadOrOpenPage(NexusModsFileMetadata.ReadOnly fileMetadata, CancellationToken cancellationToken)
{
if (_loginManager.IsPremium)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using NexusMods.Abstractions.Jobs;
using NexusMods.Abstractions.NexusModsLibrary.Models;
using NexusMods.App.UI.Controls;
using NexusMods.App.UI.Extensions;
using NexusMods.App.UI.Pages.LibraryPage;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.Paths;
using R3;

namespace NexusMods.App.UI.Pages.CollectionDownload;

public class ExternalDownloadItemModel : TreeDataGridItemModel<ILibraryItemModel, EntityId>,
ILibraryItemWithName,
ILibraryItemWithSize,
ILibraryItemWithDownloadAction
{
public ExternalDownloadItemModel(CollectionDownloadExternal.ReadOnly externalDownload)
{
DownloadableItem = new DownloadableItem(new ExternalItem(externalDownload.Uri, externalDownload.Size, externalDownload.Md5));
FormattedSize = ItemSize.ToFormattedProperty();
DownloadItemCommand = ILibraryItemWithDownloadAction.CreateCommand(this);

// ReSharper disable once NotDisposedResource
var modelActivationDisposable = this.WhenActivated(static (self, disposables) =>
{
self.IsInLibraryObservable.ObserveOnUIThreadDispatcher()
.Subscribe(self, static (inLibrary, self) =>
{
self.DownloadState.Value = inLibrary ? JobStatus.Completed : JobStatus.None;
self.DownloadButtonText.Value = ILibraryItemWithDownloadAction.GetButtonText(status: self.DownloadState.Value);
}).AddTo(disposables);
});

_modelDisposable = Disposable.Combine(
modelActivationDisposable,
Name,
ItemSize,
FormattedSize,
DownloadItemCommand,
DownloadState,
DownloadButtonText
);
}

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

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

public ReactiveProperty<Size> ItemSize { get; } = new();
public BindableReactiveProperty<string> FormattedSize { get; }

public DownloadableItem DownloadableItem { get; }

public ReactiveCommand<Unit, DownloadableItem> DownloadItemCommand { get; }

public BindableReactiveProperty<JobStatus> DownloadState { get; } = new();

public BindableReactiveProperty<string> DownloadButtonText { get; } = new(value: ILibraryItemWithDownloadAction.GetButtonText(status: JobStatus.None));

private bool _isDisposed;
private readonly IDisposable _modelDisposable;

protected override void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
_modelDisposable.Dispose();
}

_isDisposed = true;
}

base.Dispose(disposing);
}

public override string ToString() => $"External Download: {Name.Value}";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using NexusMods.Abstractions.Jobs;
using NexusMods.Abstractions.MnemonicDB.Attributes;
using NexusMods.Abstractions.NexusModsLibrary;
using NexusMods.App.UI.Controls;
using NexusMods.Paths;
using OneOf;
using R3;

Expand Down Expand Up @@ -66,9 +68,11 @@ public static string GetButtonText(int numInstalled, int numTotal, bool isExpand
}
}

public class DownloadableItem : OneOfBase<NexusModsFileMetadata.ReadOnly>
public record struct ExternalItem(Uri Uri, Size Size, Md5HashValue Md5);

public class DownloadableItem : OneOfBase<NexusModsFileMetadata.ReadOnly, ExternalItem>
{
public DownloadableItem(OneOf<NexusModsFileMetadata.ReadOnly> input) : base(input) { }
public DownloadableItem(OneOf<NexusModsFileMetadata.ReadOnly, ExternalItem> input) : base(input) { }
}

public interface ILibraryItemWithDownloadAction : ILibraryItemWithAction
Expand Down
30 changes: 26 additions & 4 deletions src/NexusMods.App.UI/Pages/NexusModsDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using DynamicData.Kernel;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using NexusMods.Abstractions.Collections;
using NexusMods.Abstractions.Jobs;
using NexusMods.Abstractions.Library.Models;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.MnemonicDB.Attributes.Extensions;
using NexusMods.Abstractions.NexusModsLibrary;
using NexusMods.Abstractions.NexusModsLibrary.Models;
using NexusMods.App.UI.Extensions;
using NexusMods.App.UI.Pages.CollectionDownload;
using NexusMods.App.UI.Pages.LibraryPage;
using NexusMods.App.UI.Pages.LoadoutPage;
using NexusMods.Extensions.BCL;
Expand Down Expand Up @@ -43,7 +45,7 @@ public IObservable<IChangeSet<ILibraryItemModel, EntityId>> ObserveCollectionIte
.ObserveDatoms(CollectionDownloadEntity.CollectionRevision, revisionMetadata)
.AsEntityIds()
.Transform(datom => CollectionDownloadEntity.Load(_connection.Db, datom.E))
.Filter(static downloadEntity => downloadEntity.IsCollectionDownloadNexusMods())// TODO: || downloadEntity.IsCollectionDownloadExternal())
.Filter(static downloadEntity => downloadEntity.IsCollectionDownloadNexusMods() || downloadEntity.IsCollectionDownloadExternal())
.Transform(ILibraryItemModel (downloadEntity) =>
{
if (downloadEntity.TryGetAsCollectionDownloadNexusMods(out var nexusModsDownload))
Expand Down Expand Up @@ -106,14 +108,34 @@ private ILibraryItemModel ToLibraryItemModel(CollectionDownloadNexusMods.ReadOnl

model.Name.Value = nexusModsDownload.FileMetadata.Name;
model.Version.Value = nexusModsDownload.FileMetadata.Version;

if (NexusModsFileMetadata.Size.TryGet(nexusModsDownload.FileMetadata, out var size)) model.ItemSize.Value = size;
model.ItemSize.Value = nexusModsDownload.FileMetadata.Size;
return model;
}

private ILibraryItemModel ToLibraryItemModel(CollectionDownloadExternal.ReadOnly externalDownload)
{
throw new NotImplementedException();
var isInLibraryObservable = _connection
.ObserveDatoms(DirectDownloadLibraryFile.Md5)
.Transform(datom => DirectDownloadLibraryFile.Md5.ReadValue(datom.ValueSpan, datom.Prefix.ValueTag, _connection.AttributeResolver))
.Filter(hash => hash == externalDownload.Md5)
.IsNotEmpty()
.ToObservable()
.Prepend((_connection, externalDownload.Md5), static state =>
{
var (connection, hash) = state;
var libraryItems = DirectDownloadLibraryFile.FindByMd5(connection.Db, hash);
return libraryItems.Count > 0;
});

var model = new ExternalDownloadItemModel(externalDownload)
{
IsInLibraryObservable = isInLibraryObservable,
// DownloadJobObservable = ,
};

model.Name.Value = externalDownload.AsCollectionDownload().Name;
model.ItemSize.Value = externalDownload.Size;
return model;
}

public IObservable<IChangeSet<ILibraryItemModel, EntityId>> ObserveFlatLibraryItems(LibraryFilter libraryFilter)
Expand Down

0 comments on commit 71099b4

Please sign in to comment.