Skip to content

Commit

Permalink
Merge pull request #2695 from Nexus-Mods/feat/readonly-collections-de…
Browse files Browse the repository at this point in the history
…lete

Add readonly mode for View Mod Contents Filetree view
  • Loading branch information
Al12rs authored Feb 20, 2025
2 parents 5ef3d49 + 7f39e17 commit 772165b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public LoadoutLeftMenuViewModel(
Context = new ItemContentsFileTreePageContext
{
GroupId = group.LoadoutItemGroupId,
IsReadOnly = false,
},
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public CollectionDownloadViewModel(
{
FileId = collectionJsonFile.AsLibraryFile().LibraryFileId,
FilePath = collectionJsonFile.AsLibraryFile().FileName,
IsReadOnly = true,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace NexusMods.App.UI.Pages.ItemContentsFileTree;
public record ItemContentsFileTreePageContext : IPageFactoryContext
{
public required LoadoutItemGroupId GroupId { get; init; }

public required bool IsReadOnly { get; init; }
}

[UsedImplicitly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public ItemContentsFileTreeViewModel(
{
FileId = loadoutFile.LoadoutFileId,
FilePath = loadoutFile.AsLoadoutItemWithTargetPath().TargetPath.Item3,
IsReadOnly = Context!.IsReadOnly,
},
};

Expand All @@ -75,10 +76,16 @@ public ItemContentsFileTreeViewModel(
})
.AddTo(ref _disposables);

RemoveCommand = this.ObservePropertyChanged(vm => vm.SelectedItem)
.WhereNotNull()
.Select(_ => true)
.ToReactiveCommand<R3.Unit>(async (_, cancellationToken) =>

// ReSharper disable once InvokeAsExtensionMethod
RemoveCommand = Observable.CombineLatest(
this.ObservePropertyChanged(vm => vm.Context)
.Select(context => context is { IsReadOnly: false }),
this.ObservePropertyChanged(vm => vm.SelectedItem)
.Select(item => item is not null),
resultSelector: (isWritable, hasSelection) => isWritable && hasSelection
)
.ToReactiveCommand<Unit>(async (_, _) =>
{
var gamePath = SelectedItem!.Key;
var group = LoadoutItemGroup.Load(connection.Db, Context!.GroupId);
Expand All @@ -87,22 +94,22 @@ public ItemContentsFileTreeViewModel(
.OfTypeLoadoutItemWithTargetPath()
.Where(item => item.TargetPath.Item2.Equals(gamePath.LocationId) && item.TargetPath.Item3.StartsWith(gamePath.Path))
.ToArray();

if (loadoutItemsToDelete.Length == 0)
{
logger.LogError("Unable to find Loadout files with path `{Path}` in group `{Group}`", gamePath, group.AsLoadoutItem().Name);
return;
}

using var tx = connection.BeginTransaction();

foreach (var loadoutItem in loadoutItemsToDelete)
{
tx.Delete(loadoutItem, recursive: false);
}

await tx.Commit();

// Refresh the file tree, currently by re-creating it which isn't super great
FileTreeViewModel = new LoadoutItemGroupFileTreeViewModel(group.Rebase());
}
Expand Down
6 changes: 6 additions & 0 deletions src/NexusMods.App.UI/Pages/LoadoutPage/LoadoutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DynamicData;
using DynamicData.Kernel;
using Microsoft.Extensions.DependencyInjection;
using NexusMods.Abstractions.Collections;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.Loadouts.Extensions;
using NexusMods.Abstractions.UI.Extensions;
Expand Down Expand Up @@ -105,12 +106,17 @@ public LoadoutViewModel(IWindowManager windowManager, IServiceProvider servicePr
var group = viewModFilesArgumentsSubject.Value;
if (!group.HasValue) return;

var isReadonly = group.Value.AsLoadoutItem()
.GetThisAndParents()
.Any(item => NexusCollectionItemLoadoutGroup.IsRequired.TryGetValue(item, out var isRequired) && isRequired);

var pageData = new PageData
{
FactoryId = ItemContentsFileTreePageFactory.StaticId,
Context = new ItemContentsFileTreePageContext
{
GroupId = group.Value.Id,
IsReadOnly = isReadonly,
},
};
var workspaceController = GetWorkspaceController();
Expand Down
13 changes: 13 additions & 0 deletions src/NexusMods.App.UI/Pages/Sorting/LoadOrdersWIPPage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using NexusMods.Abstractions.Games;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.Serialization.Attributes;
using NexusMods.App.UI.Windows;
using NexusMods.App.UI.WorkspaceSystem;
using NexusMods.Icons;
using NexusMods.MnemonicDB.Abstractions;

namespace NexusMods.App.UI.Pages.Sorting;

Expand All @@ -17,8 +19,11 @@ public record LoadOrdersWIPPageContext : IPageFactoryContext
[UsedImplicitly]
public class LoadOrdersWIPPageFactory : APageFactory<ILoadOrdersWIPPageViewModel, LoadOrdersWIPPageContext>
{
private readonly IConnection _connection;

public LoadOrdersWIPPageFactory(IServiceProvider serviceProvider) : base(serviceProvider)
{
_connection = serviceProvider.GetRequiredService<IConnection>();
}

public static readonly PageFactoryId StaticId = PageFactoryId.From(Guid.Parse("5192B4BE-4DEF-4C99-BDB9-32AEAF70D9A8"));
Expand All @@ -33,6 +38,14 @@ public override ILoadOrdersWIPPageViewModel CreateViewModel(LoadOrdersWIPPageCon
public override IEnumerable<PageDiscoveryDetails?> GetDiscoveryDetails(IWorkspaceContext workspaceContext)
{
if (workspaceContext is not LoadoutContext loadoutContext) yield break;

var loadout = Loadout.Load(_connection.Db, loadoutContext.LoadoutId);
var numSortableItemProviders = loadout
.InstallationInstance
.GetGame()
.SortableItemProviderFactories.Length;

if (numSortableItemProviders == 0) yield break;

yield return new PageDiscoveryDetails
{
Expand Down
3 changes: 1 addition & 2 deletions src/NexusMods.App.UI/Pages/TextEdit/TextEditorPage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using NexusMods.Abstractions.GameLocators;
using NexusMods.Abstractions.Library.Models;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.Loadouts.Files;
using NexusMods.Abstractions.Serialization.Attributes;
using NexusMods.App.UI.WorkspaceSystem;
using NexusMods.Paths;
Expand All @@ -16,6 +14,7 @@ public record TextEditorPageContext : IPageFactoryContext
{
public required OneOf<LoadoutFileId, LibraryFileId> FileId { get; init; }
public required RelativePath FilePath { get; init; }
public required bool IsReadOnly { get; init; }
}

[UsedImplicitly]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public TextEditorPageView()
var copyCommand = ReactiveCommand.Create(() => TextEditor.Copy(), this.WhenAnyValue(view => view.TextEditor.CanCopy));
CopyButton.Command = copyCommand;

var pasteCommand = ReactiveCommand.Create(() => TextEditor.Paste(), this.WhenAnyValue(view => view.TextEditor.CanPaste));
var pasteCommand = ReactiveCommand.Create(() => TextEditor.Paste(),
this.WhenAnyValue(
view => view.TextEditor.CanPaste,
view => view.ViewModel!.IsReadOnly,
(canPaste, isReadOnly) => canPaste && !isReadOnly
)
);
PasteButton.Command = pasteCommand;

TextEditor.Options = new TextEditorOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public TextEditorPageViewModel(
};

Document = document;
IsReadOnly = context.FileId.IsT1;
IsReadOnly = context.IsReadOnly || context.FileId.IsT1;
})
.DisposeWith(disposables);

Expand Down

0 comments on commit 772165b

Please sign in to comment.