Skip to content

Commit

Permalink
Merge pull request #2033 from Nexus-Mods/fix/switch-to-new-views-2
Browse files Browse the repository at this point in the history
Fix left menu collection duplicated entries
  • Loading branch information
Al12rs authored Sep 12, 2024
2 parents 3115e1c + 6daa5ec commit 8b7f014
Show file tree
Hide file tree
Showing 7 changed files with 1,185 additions and 457 deletions.
50 changes: 35 additions & 15 deletions src/NexusMods.App.UI/LeftMenu/Loadout/LoadoutLeftMenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData;
using DynamicData.Binding;
using DynamicData.Kernel;
using Microsoft.Extensions.DependencyInjection;
using NexusMods.Abstractions.Diagnostics;
using NexusMods.Abstractions.Loadouts;
using NexusMods.Abstractions.Settings;
using NexusMods.App.UI.Controls.LoadoutCard;
using NexusMods.App.UI.Controls.Navigation;
using NexusMods.App.UI.LeftMenu.Items;
using NexusMods.App.UI.Pages.Diagnostics;
Expand All @@ -17,7 +15,6 @@
using NexusMods.App.UI.WorkspaceSystem;
using NexusMods.Icons;
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Query;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

Expand All @@ -28,7 +25,9 @@ public class LoadoutLeftMenuViewModel : AViewModel<ILoadoutLeftMenuViewModel>, I
public IApplyControlViewModel ApplyControlViewModel { get; }

private readonly SourceList<ILeftMenuItemViewModel> _items = new();
private ReadOnlyObservableCollection<ILeftMenuItemViewModel> _finalCollection = new(new ObservableCollection<ILeftMenuItemViewModel>());
private ReadOnlyObservableCollection<ILeftMenuItemViewModel> _finalCollection = new([]);

private readonly SourceList<ILeftMenuItemViewModel> _collectionGroupItems = new();

public ReadOnlyObservableCollection<ILeftMenuItemViewModel> Items => _finalCollection;
public WorkspaceId WorkspaceId { get; }
Expand All @@ -44,18 +43,36 @@ public LoadoutLeftMenuViewModel(
var diagnosticManager = serviceProvider.GetRequiredService<IDiagnosticManager>();
var conn = serviceProvider.GetRequiredService<IConnection>();

var loadout = Abstractions.Loadouts.Loadout.Load(conn.Db, loadoutContext.LoadoutId);
var game = loadout.InstallationInstance.Game;

var settingsManager = serviceProvider.GetRequiredService<ISettingsManager>();

WorkspaceId = workspaceId;
ApplyControlViewModel = new ApplyControlViewModel(loadoutContext.LoadoutId, serviceProvider);


var installedModsItem = new IconViewModel
{
Name = Language.LoadoutView_Title_Installed_Mods,
RelativeOrder = 1,
Icon = IconValues.Mods,
NavigateCommand = ReactiveCommand.Create<NavigationInformation>(info =>
{
var pageData = new PageData
{
FactoryId = LoadoutPageFactory.StaticId,
Context = new LoadoutPageContext
{
LoadoutId = loadoutContext.LoadoutId,
GroupScope = Optional<LoadoutItemGroupId>.None,
},
};
var behavior = workspaceController.GetOpenPageBehavior(pageData, info);
workspaceController.OpenPage(WorkspaceId, pageData, behavior);
}),
};


var libraryItem = new IconViewModel
{
Name = Language.LibraryPageTitle,
RelativeOrder = 1,
RelativeOrder = 3,
Icon = IconValues.ModLibrary,
NavigateCommand = ReactiveCommand.Create<NavigationInformation>(info =>
{
Expand All @@ -78,7 +95,7 @@ public LoadoutLeftMenuViewModel(
var diagnosticItem = new IconViewModel
{
Name = Language.LoadoutLeftMenuViewModel_LoadoutLeftMenuViewModel_Diagnostics,
RelativeOrder = 2,
RelativeOrder = 4,
Icon = IconValues.Stethoscope,
NavigateCommand = ReactiveCommand.Create<NavigationInformation>(info =>
{
Expand All @@ -95,10 +112,12 @@ public LoadoutLeftMenuViewModel(
workspaceController.OpenPage(WorkspaceId, pageData, behavior);
}),
};



var tools = new ILeftMenuItemViewModel[]
{
installedModsItem,
libraryItem,
diagnosticItem,
};
Expand All @@ -107,13 +126,14 @@ public LoadoutLeftMenuViewModel(

this.WhenActivated(disposable =>
{
_collectionGroupItems.Clear();
CollectionGroup.ObserveAll(conn)
.Filter(f => f.AsLoadoutItemGroup().AsLoadoutItem().LoadoutId == loadoutContext.LoadoutId)
.SortBy(itm => itm.IsReadOnly)
.Transform(itm => MakeLoadoutItemGroupViewModel(workspaceController, itm, serviceProvider))
.Subscribe(s =>
{
_items.Edit(x => {
_collectionGroupItems.Edit(x => {
foreach (var change in s)
{
if (change.Reason == ChangeReason.Add)
Expand All @@ -131,6 +151,7 @@ public LoadoutLeftMenuViewModel(
.DisposeWith(disposable);
_items.Connect()
.Merge(_collectionGroupItems.Connect())
.Sort(new LeftMenuComparer())
.Bind(out _finalCollection)
.Subscribe()
Expand Down Expand Up @@ -178,6 +199,7 @@ private ILeftMenuItemViewModel MakeLoadoutItemGroupViewModel(IWorkspaceControlle
CollectionGroupId = itm.CollectionGroupId,
Name = itm.AsLoadoutItemGroup().AsLoadoutItem().Name,
Icon = IconValues.Collections,
RelativeOrder = 2,
NavigateCommand = ReactiveCommand.Create<NavigationInformation>(info =>
{
var pageData = new PageData
Expand Down Expand Up @@ -212,8 +234,6 @@ public int Compare(ILeftMenuItemViewModel? x, ILeftMenuItemViewModel? y)
return (x, y) switch
{
(LeftMenuCollectionViewModel a, LeftMenuCollectionViewModel b) => a.CollectionGroupId.Value.CompareTo(b.CollectionGroupId.Value),
(LeftMenuCollectionViewModel _, IconViewModel _) => -1,
(IconViewModel _, LeftMenuCollectionViewModel _) => 1,
(IconViewModel a, IconViewModel b) => a.RelativeOrder.CompareTo(b.RelativeOrder),
_ => 0,
};
Expand Down
4 changes: 4 additions & 0 deletions src/NexusMods.App.UI/Pages/LoadoutPage/ILoadoutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public interface ILoadoutViewModel : IPageViewModelInterface
LoadoutTreeDataGridAdapter Adapter { get; }

R3.ReactiveCommand<R3.Unit> SwitchViewCommand { get; }

R3.ReactiveCommand<NavigationInformation> ViewLibraryCommand { get; }

string EmptyStateTitleText { get; }

R3.ReactiveCommand<NavigationInformation> ViewFilesCommand { get; }

Expand Down
1 change: 1 addition & 0 deletions src/NexusMods.App.UI/Pages/LoadoutPage/LoadoutView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@


<controls:EmptyState Grid.Row="1" x:Name="EmptyState">

<controls:EmptyState.Subtitle>
<StackPanel>
<TextBlock Text="{x:Static resources:Language.LoadoutGrid_EmptyModlistSubtitle_Add_from_library}" />
Expand Down
6 changes: 6 additions & 0 deletions src/NexusMods.App.UI/Pages/LoadoutPage/LoadoutView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public LoadoutView()
this.BindCommand(ViewModel, vm => vm.ViewFilesCommand, view => view.ViewFilesButton)
.AddTo(disposables);
this.BindCommand(ViewModel, vm => vm.ViewLibraryCommand, view => view.ViewLibraryButton)
.AddTo(disposables);
this.BindCommand(ViewModel, vm => vm.RemoveItemCommand, view => view.DeleteButton)
.AddTo(disposables);
Expand All @@ -32,6 +35,9 @@ public LoadoutView()
this.OneWayBind(ViewModel, vm => vm.Adapter.IsSourceEmpty.Value, view => view.EmptyState.IsActive)
.AddTo(disposables);
this.OneWayBind(ViewModel, vm => vm.EmptyStateTitleText, view => view.EmptyState.Header)
.AddTo(disposables);
});
}
}
Expand Down
39 changes: 35 additions & 4 deletions src/NexusMods.App.UI/Pages/LoadoutPage/LoadoutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NexusMods.App.UI.Controls.Trees;
using NexusMods.App.UI.Extensions;
using NexusMods.App.UI.Pages.ItemContentsFileTree;
using NexusMods.App.UI.Pages.LibraryPage;
using NexusMods.App.UI.Resources;
using NexusMods.App.UI.Windows;
using NexusMods.App.UI.WorkspaceSystem;
Expand All @@ -28,8 +29,9 @@ public class LoadoutViewModel : APageViewModel<ILoadoutViewModel>, ILoadoutViewM
private readonly IConnection _connection;

public ReactiveCommand<Unit> SwitchViewCommand { get; }

public string EmptyStateTitleText { get; }
public ReactiveCommand<NavigationInformation> ViewFilesCommand { get; }
public ReactiveCommand<NavigationInformation> ViewLibraryCommand { get; }
public ReactiveCommand<Unit> RemoveItemCommand { get; }

public LoadoutTreeDataGridAdapter Adapter { get; }
Expand All @@ -49,11 +51,22 @@ public LoadoutViewModel(IWindowManager windowManager, IServiceProvider servicePr
};

Adapter = new LoadoutTreeDataGridAdapter(serviceProvider, ticker, loadoutFilter);

_connection = serviceProvider.GetRequiredService<IConnection>();

TabTitle = Language.LoadoutViewPageTitle;
TabIcon = IconValues.Collections;

_connection = serviceProvider.GetRequiredService<IConnection>();
if (collectionGroupId.HasValue)
{
var collectionGroup = LoadoutItem.Load(_connection.Db, collectionGroupId.Value);
TabTitle = collectionGroup.Name;
TabIcon = IconValues.Collections;
}
else
{
TabTitle = Language.LoadoutViewPageTitle;
TabIcon = IconValues.Mods;
}


SwitchViewCommand = new ReactiveCommand<Unit>(_ => { Adapter.ViewHierarchical.Value = !Adapter.ViewHierarchical.Value; });
ticker.Connect();
Expand All @@ -63,6 +76,24 @@ public LoadoutViewModel(IWindowManager windowManager, IServiceProvider servicePr
.Select(count => count > 0);

var viewModFilesArgumentsSubject = new BehaviorSubject<Optional<LoadoutItemGroup.ReadOnly>>(Optional<LoadoutItemGroup.ReadOnly>.None);

var loadout = Loadout.Load(_connection.Db, loadoutId);
EmptyStateTitleText = string.Format(Language.LoadoutGridViewModel_EmptyModlistTitleString, loadout.InstallationInstance.Game.Name);
ViewLibraryCommand = new ReactiveCommand<NavigationInformation>(info =>
{
var pageData = new PageData
{
FactoryId = LibraryPageFactory.StaticId,
Context = new LibraryPageContext
{
LoadoutId = loadoutId,
},
};
var workspaceController = GetWorkspaceController();
var behavior = workspaceController.GetOpenPageBehavior(pageData, info);
workspaceController.OpenPage(workspaceController.ActiveWorkspaceId, pageData, behavior);
}
);

ViewFilesCommand = viewModFilesArgumentsSubject
.Select(viewModFilesArguments => viewModFilesArguments.HasValue)
Expand Down
Loading

0 comments on commit 8b7f014

Please sign in to comment.