ui(manager): improve search bindings
This commit is contained in:
38
ModpackUpdater.Apps.Manager/Ui/Models/DynamicDataView.cs
Normal file
38
ModpackUpdater.Apps.Manager/Ui/Models/DynamicDataView.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using DynamicData;
|
||||
|
||||
namespace ModpackUpdater.Apps.Manager.Ui.Models;
|
||||
|
||||
public class DynamicDataView<T> : INotifyPropertyChanged where T : notnull
|
||||
{
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private string? searchText;
|
||||
private readonly Subject<string?> searchTextSubject = new();
|
||||
|
||||
public SourceList<T> List { get; } = new();
|
||||
public ReadOnlyObservableCollection<T> View { get; }
|
||||
|
||||
public DynamicDataView(Func<string?, Func<T, bool>> predicate)
|
||||
{
|
||||
List.Connect()
|
||||
.Filter(searchTextSubject/*.Throttle(TimeSpan.FromMilliseconds(250))*/.Select(predicate))
|
||||
.Bind(out var view)
|
||||
.Subscribe();
|
||||
searchTextSubject?.OnNext(searchText);
|
||||
View = view;
|
||||
}
|
||||
|
||||
public string? SearchText
|
||||
{
|
||||
get => searchText;
|
||||
set
|
||||
{
|
||||
searchText = value;
|
||||
searchTextSubject.OnNext(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,13 @@ public class MainWindowViewModel : INotifyPropertyChanged
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private ObservableCollection<MainWindowTreeNode>? currentTreeNodes;
|
||||
private ObservableCollection<MainWindowGridRow>? currentGridRows;
|
||||
private MainWindowTreeNode? selectedTreeNode;
|
||||
private IWorkspace? currentWorkspace;
|
||||
|
||||
public bool IsUpdate => selectedTreeNode is ActionSetTreeNode node && node.Infos is UpdateInfo;
|
||||
public ProgressInfos Progress { get; } = new();
|
||||
public MainWindowGridRow? SelectedGridRow { get; set; }
|
||||
public DynamicDataView<MainWindowGridRow> CurrentGridRows { get; } = new(FilterGridRows);
|
||||
|
||||
[AlsoNotifyFor(nameof(CurrentTreeNodes))]
|
||||
public IWorkspace? CurrentWorkspace
|
||||
@@ -48,24 +48,23 @@ public class MainWindowViewModel : INotifyPropertyChanged
|
||||
}
|
||||
}
|
||||
|
||||
[AlsoNotifyFor(nameof(CurrentGridRows))]
|
||||
public MainWindowTreeNode? SelectedTreeNode
|
||||
{
|
||||
get => selectedTreeNode;
|
||||
set
|
||||
{
|
||||
currentGridRows = null;
|
||||
selectedTreeNode = value;
|
||||
CurrentGridRows.List.Edit(list =>
|
||||
{
|
||||
list.Clear();
|
||||
if (CurrentWorkspace?.InstallInfos != null && selectedTreeNode is ActionSetTreeNode node)
|
||||
list.AddRange(node.Infos.Actions.Select(n => new MainWindowGridRow(n, CurrentWorkspace.InstallInfos)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<MainWindowGridRow>? CurrentGridRows
|
||||
private static Func<MainWindowGridRow, bool> FilterGridRows(string? searchText)
|
||||
{
|
||||
get
|
||||
{
|
||||
if (currentGridRows == null && CurrentWorkspace?.InstallInfos != null && selectedTreeNode is ActionSetTreeNode node)
|
||||
currentGridRows = [.. node.Infos.Actions.Select(n => new MainWindowGridRow(n, CurrentWorkspace.InstallInfos))];
|
||||
return currentGridRows;
|
||||
}
|
||||
return n => string.IsNullOrWhiteSpace(searchText) || true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using DynamicData;
|
||||
using PropertyChanged;
|
||||
|
||||
namespace ModpackUpdater.Apps.Manager.Ui.Models.UpdatesCollectorViewMode;
|
||||
@@ -9,6 +13,10 @@ public class UpdatesCollectorViewModel : INotifyPropertyChanged
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
public ProgressInfos Progress { get; } = new();
|
||||
public string? SearchText { get; set; }
|
||||
public ObservableCollection<ModUpdateInfo> Updates { get; } = [];
|
||||
public DynamicDataView<ModUpdateInfo> Updates { get; } = new(FilterUpdates);
|
||||
|
||||
private static Func<ModUpdateInfo, bool> FilterUpdates(string? searchText)
|
||||
{
|
||||
return n => string.IsNullOrWhiteSpace(searchText) || (n.Origin.Name != null && n.Origin.Name.Contains(searchText, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user